-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
webdriver.ps1
203 lines (181 loc) · 5.77 KB
/
webdriver.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<#
Installs and starts a Webdriver server for Edge.
Installs and starts NGINX as reverse proxy for remote Webdriver connections.
Installs and starts MJPEGServer and FFmpeg for screen recordings.
Updates the Windows hosts file with custom entries.
Copyright 2019, Sebastian Tschan
https://blueimp.net
Licensed under the MIT license:
https://opensource.org/licenses/MIT
Resources:
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
https://nginx.org/en/docs/windows.html
https://github.com/blueimp/mjpeg-server
https://www.ffmpeg.org/
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', ''
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingPositionalParameters', ''
)]
param()
$ErrorActionPreference = 'Stop' # Stop if a cmdlet fails
$ffmpegOptions = @{
fps = '15'
quality = '2' # Value between 2 (best) and 31 (worst)
}
$versions = @{
nginx = '1.21.3'
ffmpeg = 'autobuild-2021-10-09-12-22/' +
'ffmpeg-n4.4-178-g4b583e5425-win64-gpl-shared-4.4'
MJPEGServer = '1.3.0'
}
$downloads = @{
nginx = 'https://nginx.org/download/nginx-1.21.3.zip' -f $versions.nginx
msedgedriver = 'https://msedgedriver.azureedge.net/{0}/edgedriver_win64.zip'
ffmpeg = ('https://github.com/BtbN/FFmpeg-Builds/releases/download/' +
'{0}.zip') -f $versions.ffmpeg
MJPEGServer = ('https://github.com/blueimp/mjpeg-server/releases/download/' +
'v{0}/mjpeg-server-windows-amd64.zip') -f $versions.MJPEGServer
}
$ffmpegCommand = ('ffmpeg' +
' -loglevel error' +
' -probesize 32' +
' -fpsprobesize 0' +
' -analyzeduration 0' +
' -fflags nobuffer' +
' -f gdigrab' +
' -r {0}' +
' -i desktop' +
' -f mpjpeg' +
' -q {1}' +
' -') -f $ffmpegOptions.fps,$ffmpegOptions.quality
# Relative install paths to add to the PATH environment variable:
$installPaths = @('bin', 'nginx', 'ffmpeg\bin')
# Runs the given command with Administrator privileges:
function Invoke-AdminCommand {
param([String]$command, [String]$params, [String]$reason)
Clear-Host
'Requesting Administrator privileges {0}...' -f $reason
Start-Process $command $params -Verb RunAs -Wait
}
# Runs the given Powershell command with Administrator privileges:
function Invoke-PowershellAdminCommand {
param([String]$command, [String]$reason)
$params = "-ExecutionPolicy ByPass -command $($command -replace '"','\"')"
Invoke-AdminCommand powershell $params $reason
}
# Overwrites the Windows hosts file with the file windows.hosts, if available:
function Update-HostsFile {
$newHostsFile = "$(Get-Location)\windows.hosts"
if (Test-Path $newHostsFile) {
$hostsFile = "$env:windir\System32\drivers\etc\hosts"
if ((Get-FileHash $hostsFile).hash -ne (Get-FileHash $newHostsFile).hash) {
$command = 'Copy-Item "{0}" "{1}"' -f $newHostsFile,$hostsFile
Invoke-PowershellAdminCommand $command 'to overwrite the hosts file'
}
}
}
# Downloads and extracts a ZIP file provided as URL:
function Invoke-ZipDownload {
param([String]$url)
$filename = [IO.Path]::GetRandomFileName() + '.zip'
Invoke-WebRequest $url -OutFile $filename
Expand-Archive $filename .
Remove-Item $filename
}
# Returns the Edge version number:
function Get-EdgeVersion {
$path = 'HKCU:\Software\Microsoft\Edge\BLBeacon'
Get-ItemProperty $path -ErrorAction SilentlyContinue |
ForEach-Object version
}
# Installs msedgedriver:
function Install-EdgeDriver {
$version = Get-EdgeVersion
if ($version) {
if (Test-Path bin\msedgedriver.exe) {
$driverVersion = ($(bin\msedgedriver.exe -v) -split ' ')[1]
if ($driverVersion -eq $version) {
return
}
Remove-Item bin\msedgedriver.exe
}
$url = $downloads.msedgedriver -f $version
New-Item bin -ItemType Directory -Force
Clear-Host
'Installing Microsoft Edge Driver ...'
Invoke-ZipDownload $url
Move-Item msedgedriver.exe bin
Remove-Item Driver_Notes -Recurse
}
}
# Installs and configures nginx:
function Install-NGINX {
if (!(Test-Path nginx)) {
Clear-Host
'Installing nginx ...'
Invoke-ZipDownload $downloads.nginx
Move-Item nginx-* nginx
}
if ((Test-Path nginx) -and (Test-Path nginx.conf)) {
# Bind nginx to all interfaces instead of only localhost:
(Get-Content nginx.conf) -replace '127.0.0.1:4444', '4444' |
Set-Content nginx.conf
Move-Item nginx.conf nginx\conf\nginx.conf -Force
}
}
# Installs ffmpeg:
function Install-FFmpeg {
if (!(Test-Path ffmpeg)) {
Clear-Host
'Installing ffmpeg ...'
Invoke-ZipDownload $downloads.ffmpeg
Move-Item ffmpeg-* ffmpeg
}
}
# Installs MJPEGServer:
function Install-MJPEGServer {
if (!(Test-Path bin\MJPEGServer.exe)) {
New-Item bin -ItemType Directory -Force
Clear-Host
'Installing MJPEGServer ...'
Invoke-ZipDownload $downloads.MJPEGServer
Move-Item MJPEGServer.exe bin
}
}
# Updates the PATH environment variable with the installed program paths:
function Update-Path {
$currentPath = $(Get-Location)
$originalEnvPath = $env:Path
$pathComponents = $originalEnvPath.TrimEnd(';') -split ';'
foreach ($path in $installPaths) {
if ($pathComponents -notcontains "$currentPath\$path") {
$pathComponents += "$currentPath\$path"
}
}
$env:Path = ($pathComponents -join ';') + ';'
if ($env:Path -ne $originalEnvPath) {
[Environment]::SetEnvironmentVariable(
'Path',
$env:Path,
[System.EnvironmentVariableTarget]::User
)
}
}
# Starts msedgedriver with nginx as reverse proxy as well as MJPEGServer:
function Start-Webdriver {
Clear-Host
'Starting servers ...'
Start-Process msedgedriver '--port=3333'
Start-Process nginx -WorkingDirectory nginx
Start-Process MJPEGServer $ffmpegCommand
}
Update-HostsFile
Install-EdgeDriver
Install-NGINX
Install-FFmpeg
Install-MJPEGServer
Update-Path
Start-Webdriver