Created
February 3, 2018 08:16
-
-
Save reubenmiller/fddda3293ed7ea9c8ac5d344908cb921 to your computer and use it in GitHub Desktop.
Create Selenium Server Service in Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Install-SeleniumService { | |
[cmdletBinding()] | |
Param( | |
[string] $Name = "SeleniumServer", | |
[string] $InstallPath = "C:\selenium" | |
) | |
$JavaPath = Get-Command "java.exe" | Select-Object -Last 1 -ExpandProperty Definition | |
if (!(Test-Path $InstallPath)) { | |
$null = mkdir $InstallPath | |
} | |
Get-ChildItem -Path ".\" -Filter "selenium-standalone-*.jar" -Recurse | Copy-Item -Destination "$InstallPath\" | |
Get-ChildItem -Path ".\" -Filter "nssm.exe" -Recurse | Copy-Item -Destination "$InstallPath\" | |
$NssmExe = Get-ChildItem -Path $InstallPath -Filter "nssm.exe" -Recurse | Get-Item | Select-Object -First 1 -ExpandProperty FullName | |
$JarFullPath = Get-ChildItem -Path $InstallPath -Filter "selenium-standalone-*.jar" -Recurse | Select-Object -First 1 -ExpandProperty FullName | |
if (!$JarFullPath) { | |
Write-Error "Could not find selenium-standalone jar file" | |
return | |
} | |
$Args = @( | |
"-Dwebdriver.gecko.driver=""$InstallPath/0.16.0-x64-geckodriver""", | |
"-Dwebdriver.chrome.driver=""$InstallPath/2.29-x64-chromedriver""", | |
"-Dwebdriver.ie.driver=""$InstallPath/3.4.0-x64-IEDriverServer.exe""", | |
"-jar ""$JarFullPath""" | |
) | |
$ArgString = $Args -join " " | |
$ArgString | |
if (Get-Service -Name $Name -ErrorAction SilentlyContinue) { | |
Write-Warning "Service already exists" | |
} else { | |
Write-Verbose "Installing Service (using nssm)" | |
& $NssmExe install $Name "$JavaPath" "-jar $JarFullPath" | |
& $NssmExe set $Name Description "Selenium Server used in dolerjs end to end tests" | |
& $NssmExe set $Name DisplayName "Selenium Server (dolerjs)" | |
& $NssmExe set $Name AppDirectory (Split-Path $JarFullPath -Parent) | |
& $NssmExe set $Name Start SERVICE_AUTO_START | |
& $NssmExe set $Name Type SERVICE_INTERACTIVE_PROCESS | |
# New-Service -Name $Name -BinaryPathName "$JavaPath -jar `"$JarFullPath`"" -DisplayName "Selenium Server (dolerjs)" -StartupType Manual -Description "Selenium Server used in dolerjs end to end tests" | |
} | |
} | |
Function Start-SeleniumService { | |
[cmdletBinding()] | |
Param( | |
[string] $Name = "SeleniumServer" | |
) | |
Start-Service -Name $Name | |
} | |
Function Stop-SeleniumService { | |
[cmdletBinding()] | |
Param( | |
[string] $Name = "SeleniumServer" | |
) | |
Stop-Service -Name $Name | |
} | |
Function Remove-SeleniumService { | |
[cmdletBinding()] | |
Param( | |
[string] $Name = "SeleniumServer" | |
) | |
if (Get-Service -Name $Name -ErrorAction SilentlyContinue) { | |
Stop-SeleniumService -Name $Name | |
Write-Verbose "Removing service [$Name]" | |
$NssmExe = Get-ChildItem -Path ".\" -Filter "nssm.exe" -Recurse | Get-Item | Select-Object -First 1 -ExpandProperty FullName | |
& $NssmExe remove $Name confirm | |
# (Get-WmiObject win32_service -Filter "name='$Name'").delete() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment