Skip to content

Commit 0dd5429

Browse files
authored
Improve support for file changes detection when running UI mode in Docker (#217)
* regenerate package-lock.json * improve UI mode detection so that it also works when running outside of docker * wip detect wsl version for docker-desktop * wip detect wsl version for docker-desktop 2 * start docs for file change detection on docker * remove console log from playwright config because they trigger more than just at startup * Update docker README * Update docker README 2 * Update references to README section
1 parent 071a043 commit 0dd5429

7 files changed

+461
-298
lines changed

demos/docker/README.md

+104-23
Large diffs are not rendered by default.

demos/docker/npm-pwsh-scripts/playwright.ps1

+41-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ param (
77
[switch] $useHostWebServer = $false,
88
[string] $grep = "",
99
[ValidateSet("auto", "install", "mount")]
10-
[string] $installNpmPackagesMode = "auto"
10+
[string] $installNpmPackagesMode = "auto",
11+
[ValidateSet("auto", "supported", "unsupported")]
12+
[string] $fileChangesDetectionSupportMode = "auto"
1113
)
1214

1315
function GetPlaywrightVersion() {
@@ -54,6 +56,33 @@ function NeedsToInstallNpmPackages() {
5456
return $needsToInstallNpmPackages;
5557
}
5658

59+
function IsFileChangesDetectionSupported() {
60+
if ($fileChangesDetectionSupportMode -eq "auto") {
61+
$isDockerDesktopOnWindowsUsingWsl2 = IsDockerDesktopOnWindowsUsingWsl2
62+
if($isDockerDesktopOnWindowsUsingWsl2) {
63+
Write-Host "Detected Docker Desktop running on WSL2. FILE_CHANGES_DETECTION_SUPPORTED=false environment variable will be added to the docker run command." -ForegroundColor Cyan
64+
}
65+
66+
return !$isDockerDesktopOnWindowsUsingWsl2;
67+
} elseif ($fileChangesDetectionSupportMode -eq "supported") {
68+
return $true
69+
} elseif ($fileChangesDetectionSupportMode -eq "unsupported") {
70+
return $false
71+
}
72+
73+
throw "Unknown '-fileChangesDetectionSupportMode' option. Received: $fileChangesDetectionSupportMode. Available options are: 'auto | supported | unsupported'"
74+
}
75+
76+
function IsDockerDesktopOnWindowsUsingWsl2() {
77+
if(!$IsWindows) {
78+
return $false
79+
}
80+
81+
# See https://docs.docker.com/desktop/settings/windows/
82+
$dockerDesktopSettings = Get-Content "$env:USERPROFILE\AppData\Roaming\Docker\settings.json" | ConvertFrom-Json
83+
return $dockerDesktopSettings.wslEngineEnabled
84+
}
85+
5786
function StartPlaywrightTests {
5887
Write-Host "Starting playwright tests run in docker container...`n" -ForegroundColor Cyan
5988
Write-Host "options:" -ForegroundColor DarkYellow
@@ -102,17 +131,25 @@ function StartPlaywrightTests {
102131
Exit $LASTEXITCODE # see https://stackoverflow.com/questions/32348794/how-to-get-status-of-invoke-expression-successful-or-failed
103132
}
104133

105-
106134
function StartPlaywrightUI() {
107135
Write-Host "Starting playwright tests with ui in docker container...`n"
108136
Write-Host "options:" -ForegroundColor DarkYellow
109137
Write-Host "-useHostWebServer=$useHostWebServer" -ForegroundColor DarkYellow
110-
Write-Host "-installNpmPackagesMode=$installNpmPackagesMode`n" -ForegroundColor DarkYellow
138+
Write-Host "-installNpmPackagesMode=$installNpmPackagesMode" -ForegroundColor DarkYellow
139+
Write-Host "-fileChangesDetectionSupportMode=$fileChangesDetectionSupportMode`n" -ForegroundColor DarkYellow
111140

112141
if ($useHostWebServer) {
113142
$useHostWebServerOption = "--add-host=host.docker.internal:host-gateway --env USE_DOCKER_HOST_WEBSERVER=true"
114143
}
115144

145+
# For more info on the reason for the FILE_CHANGES_DETECTION_SUPPORTED environment variable
146+
# see the section 'File changes aren't triggering an application rebuild when testing with UI mode' of the README at /demos/docker/README.md
147+
if(IsFileChangesDetectionSupported) {
148+
$fileChangesDetectionSupportedEnv = "--env FILE_CHANGES_DETECTION_SUPPORTED=true"
149+
} else {
150+
$fileChangesDetectionSupportedEnv = "--env FILE_CHANGES_DETECTION_SUPPORTED=false"
151+
}
152+
116153
# Must use a random port or else there will be issues with the UI app where sometimes the tests don't load/refresh properly.
117154
# I believe it has something to do with some websockets that the UI app uses. When the same port is used and the UI app is
118155
# restarted, the tests don't load properly until the websockets timeout and then a new connection is established.
@@ -127,7 +164,7 @@ function StartPlaywrightUI() {
127164
$startCommand = "/bin/bash -c 'npm ci && $startCommand'" # see https://stackoverflow.com/questions/28490874/docker-run-image-multiple-commands
128165
}
129166

130-
$dockerRunCommand = "docker run -it --rm --ipc=host $useHostWebServerOption --workdir=/app -p ${playwrightUiPort}:${playwrightUiPort} -v '${PWD}:/app' $nodeModulesMount mcr.microsoft.com/playwright:v$playwrightVersion-jammy $startCommand"
167+
$dockerRunCommand = "docker run -it --rm --ipc=host $useHostWebServerOption $fileChangesDetectionSupportedEnv --workdir=/app -p ${playwrightUiPort}:${playwrightUiPort} -v '${PWD}:/app' $nodeModulesMount mcr.microsoft.com/playwright:v$playwrightVersion-jammy $startCommand"
131168
if ($installNpmPackages) {
132169
Write-Host "NPM packages will be installed in the docker container." -ForegroundColor Cyan
133170
}

0 commit comments

Comments
 (0)