Skip to content

Instantly share code, notes, and snippets.

@c3s4r
Forked from koola/selenium_node_install.bat
Last active September 11, 2023 08:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save c3s4r/0df06d4854495b6b8966c6da5e3c7de2 to your computer and use it in GitHub Desktop.
Save c3s4r/0df06d4854495b6b8966c6da5e3c7de2 to your computer and use it in GitHub Desktop.
Install Selenium grid node as a windows service
Copy both files in a folder like c:\Selenium-install
Modify the selenium_node_install.bat accordingly
If you want to use MicrosoftEdge, before running the script you must install the webDriver: https://www.microsoft.com/en-us/download/details.aspx?id=48212
If you want to use IE11, you must follow this instructions, for it to work: http://www.michael-whelan.net/selenium-webdriver-and-ie11/
You could also modify the selenium_node_install.bat if needed
Once ready, run the selenium_node_install.bat file, run it as Administrator
A service will be created.
@echo off
powershell.exe -executionpolicy unrestricted -command "%~dp0\selenium_node_install.ps1 -nodes edge,ie -hub 192.168.1.1"
REM powershell.exe -executionpolicy unrestricted -command "%~dp0\selenium_node_install.ps1 -nodes ie,chrome -hub 192.168.1.1"
REM powershell.exe -executionpolicy unrestricted -command "%~dp0\selenium_node_install.ps1 -nodes ie,chrome,firefox -hub 192.168.1.1"
pause
Param(
[Parameter(Mandatory=$True)]
[string[]]$nodes,
[string]$hub
)
$nodes = $nodes | sort -unique
$serviceName = 'selenium-node'
$installPath = 'C:\Selenium'
$download = @{
"selenium-server-standalone.jar" = "http://goo.gl/IHP6Qw";
"${serviceName}.exe" = "http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/1.18/winsw-1.18-bin.exe"
}
$argument = New-Object System.Collections.ArrayList
$argument.Add("-jar ${installPath}\selenium-server-standalone.jar") > $null
$argument.Add("-role node") > $null
$argument.Add("-hub http://${hub}:4444/grid/register") > $null
foreach ($node in $nodes) {
switch ($node.ToUpper()) {
"EDGE"{
$argument.Add("-Dwebdriver.edge.driver=`"C:\Program Files (x86)\Microsoft Web Driver\MicrosoftWebDriver.exe`"") > $null
$argument.Add("-browser `"browserName=MicrosoftEdge,maxInstances=10,platform=WINDOWS`"") > $null
}
"IE" {
$ver = [int](Get-Item ('HKLM:\Software\Microsoft\Internet Explorer\Version Vector')).GetValue("IE")
$argument.Add("-Dwebdriver.ie.driver=`"${installPath}\IEDriverServer.exe`"") > $null
$argument.Add("-browser `"browserName=internet explorer,version=${ver},maxInstances=10,platform=WINDOWS`"") > $null
$download.Add("IEDriverServer.zip", "http://selenium-release.storage.googleapis.com/2.53/IEDriverServer_x64_2.53.1.zip")
}
"CHROME" {
$argument.Add("-Dwebdriver.chrome.driver=`"${installPath}\chrome-driver.exe`"") > $null
$argument.Add("-browser `"browserName=chrome,maxInstances=10,platform=WINDOWS`"") > $null
$download.Add("chrome-driver.zip", "http://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip")
}
"FIREFOX" {
$argument.Add("-browser `"browserName=firefox,maxInstances=10,platform=WINDOWS`"") > $null
}
default { exit 1 }
}
}
# Uninstall service if installed
$service = Get-Service "$serviceName" -ErrorAction SilentlyContinue
if ($service)
{
if ($service.status -ne "Stopped")
{
$service.stop()
$service.WaitForStatus('Stopped', (New-Timespan -seconds 10))
}
& "$installPath\${serviceName}.exe" uninstall
rm "$installPath" -recurse > $null
}
# Create install directory
mkdir "$installPath" > $null
mkdir "$installPath\log\" > $null
# Download files
$wc = New-Object System.Net.WebClient
foreach ($file in $($download.keys)) {
try {
Write-Host "Downloading $file"
$wc.DownloadFile($($download[$file]), "$installPath\$file")
}
catch [Net.WebException],[System.IO.IOException] {
Write-Host "Failed to download file, exiting..." -ErrorAction stop
}
if ($file.EndsWith('zip')) {
$sh = New-Object -com shell.application
$zip = $sh.namespace("$installPath\$file")
$zip.items() | foreach {
($sh.namespace("$installPath")).Copyhere($_, 0x14)
$fname = $_.name
$file = $file -replace 'zip','exe'
mv "$installPath\$fname" "$installPath\$file" -force
}
}
}
# Service config file
$xml = @"
<service>
<id>selenium</id>
<name>$serviceName</name>
<description>Selenium Node Server</description>
<executable>java</executable>
<arguments>$($argument -join " ")</arguments>
<onfailure action="restart" />
<interactive />
<logmode>rotate</logmode>
<logpath>$installPath\log\</logpath>
</service>
"@
$xml | Out-File -force "${installPath}\${serviceName}.xml"
# Install service
& "$installPath\${serviceName}.exe" install
# Add firewall inbound rule
& netsh advfirewall firewall add rule name="$serviceName" dir=in action=allow protocol=TCP localport=5555 profile=any >null
# Start the service
$service = Get-Service "$serviceName" -ErrorAction SilentlyContinue
if ($service.status -eq "Stopped") {
$service.start()
try {
$service.WaitForStatus('Running', (New-Timespan -seconds 10))
} catch {
Write-Host "Failed to start the service, exiting..." -ErrorAction stop
} finally {
"Service started."
"Available at: http://${hub}:4444/grid/console"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment