Skip to content

Features/v4 optmize driver get stop #179 #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Examples/Combined.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ Describe "'Headless' mode browser test" {
$Processes = Get-CimInstance -Class Win32_Process
$DriverProcess = $Processes | Where { $_.Name -like '*driver.exe' -and $_.ParentProcessId -eq $Pid }
$BrowserProcess = $Processes | Where-Object { $_.ParentProcessId -eq $DriverProcess.ProcessId -and $_.Name -ne 'conhost.exe' }
(Get-process -Id $BrowserProcess.ProcessId).MainWindowHandle | Should -be 0
if ($BrowserProcess -ne $null) {
(Get-process -Id $BrowserProcess.ProcessId).MainWindowHandle | Should -be 0
}
}
else {
$DriverProcess = Get-Process *driver | Where-Object { $_.Parent.id -eq $pid }
Expand Down
2 changes: 1 addition & 1 deletion Examples/_TestDependencies.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Function Get-DefaultParams() {
if ($null -eq $Global:DefaultBrowser) { $Global:DefaultBrowser = 'Chrome' }
if ($null -eq $Global:DefaultBrowser) { $Global:DefaultBrowser = 'Firefox' }
if ($null -eq $env:SITE_URL) { $env:SITE_URL = 'http://tailspin-spacegame-web.azurewebsites.net' }
if ($null -eq $Global:HeadlessOnly) { $Global:HeadlessOnly = $False }
}
Expand Down
7 changes: 6 additions & 1 deletion Help/Get-SeDriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ Get the list of all active drivers.

## SYNTAX

### Current (Default)
### All (Default)
```
Get-SeDriver [<CommonParameters>]
```

### Current
```
Get-SeDriver [-Current] [<CommonParameters>]
```
Expand Down
18 changes: 18 additions & 0 deletions Internal/Get-DriverProcessId.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function Get-DriverProcessId {
[CmdletBinding()]
param (
$ServiceProcessId
)

$IsWindowsPowershell = $PSVersionTable.PSVersion.Major -lt 6

if ($IsWindowsPowershell) {
$Processes = Get-CimInstance -Class Win32_Process -Filter "ParentProcessId=$ServiceProcessId"
$BrowserProcess = $Processes.Where( { $_.Name -ne 'conhost.exe' }, 'first').ProcessId
}
else {
$BrowserProcess = (Get-Process).Where( { { $_.Parent.id -eq $ServiceProcessId -and $_.Name -ne 'conhost' } }, 'first').Id
}
return $BrowserProcess

}
2 changes: 1 addition & 1 deletion Internal/Start-SeChromeDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Start-SeChromeDriver {

$Driver = [OpenQA.Selenium.Chrome.ChromeDriver]::new($service, $Options)
if (-not $Driver) { Write-Warning "Web driver was not created"; return }

Add-Member -InputObject $Driver -MemberType NoteProperty -Name 'SeServiceProcessId' -Value $Service.ProcessID
#region post creation options
$Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromMilliseconds($ImplicitWait * 1000)

Expand Down
1 change: 1 addition & 0 deletions Internal/Start-SeEdgeDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function Start-SeEdgeDriver {
Write-Warning "Web driver was not created"; return
}
else {
Add-Member -InputObject $Driver -MemberType NoteProperty -Name 'SeServiceProcessId' -Value $Service.ProcessID
$driverversion = $Driver.Capabilities.ToDictionary().msedge.msedgedriverVersion -replace '^([\d.]+).*$', '$1'
if (-not $driverversion) { $driverversion = $driver.Capabilities.ToDictionary().chrome.chromedriverVersion -replace '^([\d.]+).*$', '$1' }
Write-Verbose "Web Driver version $driverversion"
Expand Down
2 changes: 1 addition & 1 deletion Internal/Start-SeFirefoxDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Start-SeFirefoxDriver {

$Driver = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($service, $Firefox_Options)
if (-not $Driver) { Write-Warning "Web driver was not created"; return }

Add-Member -InputObject $Driver -MemberType NoteProperty -Name 'SeServiceProcessId' -Value $Service.ProcessID
#region post creation options
$Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromMilliseconds($ImplicitWait * 1000)

Expand Down
2 changes: 1 addition & 1 deletion Internal/Start-SeInternetExplorerDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function Start-SeInternetExplorerDriver {

$Driver = [OpenQA.Selenium.IE.InternetExplorerDriver]::new($service, $InternetExplorer_Options)
if (-not $Driver) { Write-Warning "Web driver was not created"; return }

Add-Member -InputObject $Driver -MemberType NoteProperty -Name 'SeServiceProcessId' -Value $Service.ProcessID
if ($PSBoundParameters.ContainsKey('LogLevel')) {
Write-Warning "LogLevel parameter is not implemented for $($Options.SeParams.Browser)"
}
Expand Down
2 changes: 1 addition & 1 deletion Internal/Start-SeMSEdgeDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Start-SeMSEdgeDriver {
throw $_ ; return
}
if (-not $Driver) { Write-Warning "Web driver was not created"; return }

Add-Member -InputObject $Driver -MemberType NoteProperty -Name 'SeServiceProcessId' -Value $Service.ProcessID
#region post creation options
if ($PSBoundParameters.ContainsKey('Size')) { $Driver.Manage().Window.Size = $Size }
if ($PSBoundParameters.ContainsKey('Position')) { $Driver.Manage().Window.Position = $Position }
Expand Down
4 changes: 3 additions & 1 deletion Output/Selenium/Examples/Combined.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ Describe "'Headless' mode browser test" {
$Processes = Get-CimInstance -Class Win32_Process
$DriverProcess = $Processes | Where { $_.Name -like '*driver.exe' -and $_.ParentProcessId -eq $Pid }
$BrowserProcess = $Processes | Where-Object { $_.ParentProcessId -eq $DriverProcess.ProcessId -and $_.Name -ne 'conhost.exe' }
(Get-process -Id $BrowserProcess.ProcessId).MainWindowHandle | Should -be 0
if ($BrowserProcess -ne $null) {
(Get-process -Id $BrowserProcess.ProcessId).MainWindowHandle | Should -be 0
}
}
else {
$DriverProcess = Get-Process *driver | Where-Object { $_.Parent.id -eq $pid }
Expand Down
2 changes: 1 addition & 1 deletion Output/Selenium/Examples/_TestDependencies.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Function Get-DefaultParams() {
if ($null -eq $Global:DefaultBrowser) { $Global:DefaultBrowser = 'Chrome' }
if ($null -eq $Global:DefaultBrowser) { $Global:DefaultBrowser = 'Firefox' }
if ($null -eq $env:SITE_URL) { $env:SITE_URL = 'http://tailspin-spacegame-web.azurewebsites.net' }
if ($null -eq $Global:HeadlessOnly) { $Global:HeadlessOnly = $False }
}
Expand Down
Binary file modified Output/Selenium/Selenium.psm1
Binary file not shown.
51 changes: 34 additions & 17 deletions Public/Get-SeDriver.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

function Get-SeDriver {
[cmdletbinding(DefaultParameterSetName = 'Current')]
[cmdletbinding(DefaultParameterSetName = 'All')]
param(
[parameter(ParameterSetName = 'Current', Mandatory = $false)]
[Switch]$Current,
Expand All @@ -9,25 +10,41 @@ function Get-SeDriver {
[ArgumentCompleter( { [Enum]::GetNames([SeBrowsers]) })]
[ValidateScript( { $_ -in [Enum]::GetNames([SeBrowsers]) })]
$Browser



)


if ($Script:SeDrivers.count -eq 0) { return $null }
if ($Current) { return $Script:SeDriversCurrent }


if ($PSBoundParameters.ContainsKey('Browser')) {
return $Script:SeDrivers.Where( { $_.SeBrowser -like "$Browser*" } )
}

if ($PSBoundParameters.ContainsKey('Name')) {
return $Script:SeDrivers.Where( { $_.SeFriendlyName -eq $Name }, 'first' )
$Output = $null
switch ($PSCmdlet.ParameterSetName) {
'All' { $Output = $Script:SeDrivers; break }
'Current' { $Output = $Script:SeDriversCurrent; break }
'ByName' { $Output = $Script:SeDrivers.Where( { $_.SeFriendlyName -eq $Name }, 'first' ); break }
'ByBrowser' { $Output = $Script:SeDrivers.Where( { $_.SeBrowser -like "$Browser*" }); break }
}

return $Script:SeDrivers
if ($null -eq $Output) { return }

$DriversToClose = [System.Collections.Generic.List[PSObject]]::new()
Foreach ($drv in $Output) {
$Processes = (Get-Process -Id $Drv.SeProcessId, $Drv.SeServiceProcessId -ErrorAction SilentlyContinue )
if ($Processes.count -eq 2) { Continue }

}
if ($Processes.count -eq 0) {
Write-Warning -Message "The driver $($Drv.SeFriendlyName) $($Drv.SeBrowser) processes are not running anymore and have been removed automatically from the list of available drivers."
}
else {
$ProcessType = if ($Processes.id -eq $Drv.SeServiceProcessId) { "driver service" } else { "browser" }
Write-Warning -Message "The driver $($Drv.SeFriendlyName) $($Drv.SeBrowser) $ProcessType is not running anymore and will be removed from the active driver list."
}
$DriversToClose.Add($Drv)
}

if ($DriversToClose.Count -gt 0) {
foreach ($drv in $DriversToClose) {
$Output = $Output.Where( { $_.SeServiceProcessId -ne $drv.SeServiceProcessId })
Stop-SeDriver -Driver $drv
}
}
return $Output
}
11 changes: 6 additions & 5 deletions Public/SeShouldHave.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,32 @@ function SeShouldHave {
$foundElements = [System.Collections.Generic.List[PSObject]]::new()
}
process {
$Driver = (Get-SeDriver -Current)
#If we have been asked to check URL or title get them from the driver. Otherwise call Get-SEElement.
if ($URL) {
do {
$Success = applyTest -testitems $Script:SeDriversCurrent.Url -operator $Operator -value $Value
$Success = applyTest -testitems $Driver.Url -operator $Operator -value $Value
Start-Sleep -Milliseconds 500
}
until ($Success -or [datetime]::now -gt $endTime)
if (-not $Success) {
throw (expandErr "PageURL was $($Script:SeDriversCurrent.Url). The comparison '-$operator $value' failed.")
throw (expandErr "PageURL was $($Driver.Url). The comparison '-$operator $value' failed.")
}
}
elseif ($Title) {
do {
$Success = applyTest -testitems $Script:SeDriversCurrent.Title -operator $Operator -value $Value
$Success = applyTest -testitems $Driver.Title -operator $Operator -value $Value
Start-Sleep -Milliseconds 500
}
until ($Success -or [datetime]::now -gt $endTime)
if (-not $Success) {
throw (expandErr "Page title was $($Script:SeDriversCurrent.Title). The comparison '-$operator $value' failed.")
throw (expandErr "Page title was $($Driver.Title). The comparison '-$operator $value' failed.")
}
}
elseif ($Alert -or $NoAlert) {
do {
try {
$a = $Script:SeDriversCurrent.SwitchTo().alert()
$a = $Driver.SwitchTo().alert()
$Success = $true
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion Public/Start-SeDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function Start-SeDriver {
Add-Member @mp -Name 'SeBrowser' -Value "$SelectedBrowser$($Headless)"
Add-Member @mp -Name 'SeFriendlyName' -Value "$FriendlyName"
Add-Member @mp -Name 'SeSelectedElements' -Value $null

Add-Member -InputObject $Driver -MemberType NoteProperty -Name 'SeProcessId' -Value (Get-DriverProcessId -ServiceProcessId $Driver.SeServiceProcessId)

$Script:SeDrivers.Add($Driver)
Return Switch-SeDriver -Name $FriendlyName
Expand Down
38 changes: 17 additions & 21 deletions Public/Stop-SeDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,35 @@ function Stop-SeDriver {
)
Begin {
$ElementsToRemove = [System.Collections.Generic.List[PSObject]]::new()
$TextInfo = (Get-Culture).TextInfo
}
Process {
if (! $PSBoundParameters.ContainsKey('Driver')) {
$Driver = $script:SeDriversCurrent
$Driver = Get-SeDriver -Current
}


if ($null -ne $Driver) {
$BrowserName = $TextInfo.ToTitleCase($Driver.Capabilities.browsername)
$Processes = (Get-Process -Id $Driver.SeProcessId, $Driver.SeServiceProcessId -ErrorAction SilentlyContinue )

if ($null -eq $Driver.SessionId) {
Write-Warning "$BrowserName Driver already closed"
if ($ElementsToRemove.contains($Driver)) { $ElementsToRemove.Add($Driver) }

switch ($Processes.Count) {
2 {
Write-Verbose -Message "Closing $BrowserName $($Driver.SeFriendlyName )..."
$Driver.Close()
$Driver.Dispose()
break
}
1 { Stop-Process -Id $Processes.Id -ErrorAction SilentlyContinue }
}
else {
Write-Verbose -Message "Closing $BrowserName $($Driver.SeFriendlyName )..."

$Driver.Close()
$Driver.Dispose()
$ElementsToRemove.Add($Driver)
}




$ElementsToRemove.Add($Driver)
}

}
End {
$ElementsToRemove | ForEach-Object { [void]($script:SeDrivers.Remove($_)) }
$script:SeDriversCurrent = $null
if ($script:SeDriversCurrent -notin $script:SeDrivers) {
$script:SeDriversCurrent = $null
}

}


Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy