Reputation: 252
One would think that this should be an easy thing to do, but I cannot find a way to detect whether the Chromium Edge Selenium webdriver Window is minimized or not in Powershell.
Specifically, the size and position of a Window seems to be the same regardless of whether or not it is in a maximized or minimized state. For example, take the following example (starting from a normal non-minimized window state):
> $driver.manage().Window.size
IsEmpty Width Height
------- ----- ------
False 1050 708
> $driver.manage().Window.position
IsEmpty X Y
------- - -
False 13 18
> $driver.manage().Window.minimize()
> $driver.manage().Window.size
IsEmpty Width Height
------- ----- ------
False 1050 708
> $driver.manage().Window.position
IsEmpty X Y
------- - -
False 13 18
As you can see, the Window size and position remain the same even though the Window has been minimized.
I can't find a isMinimized()
method or something similar anywhere either.
The Chromium Edge webdriver version is 93.0.961.38.
Any ideas?
Upvotes: 4
Views: 1045
Reputation: 121
The best way is to force start the selenium edgedriver.
By that it will force minimize.
Upvotes: -2
Reputation: 3690
You can try to execute JavaScript using the Selenium driver to get the size of the viewport (no powershell but in our scenarios work).
const width = window.innerWidth || document.documentElement.clientWidth ||
document.body.clientWidth;
const height = window.innerHeight|| document.documentElement.clientHeight||
document.body.clientHeight;
Reference:
Get the size of the screen, current web page and browser window
With powershell access WindowVisualState of the process:
Add-Type -AssemblyName UIAutomationClient
$prList = Get-Process -Name "notepad"
$prList | % {
$ae = [System.Windows.Automation.AutomationElement]::FromHandle($_.MainWindowHandle)
$wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
echo "Window visual state: $($wp.Current.WindowVisualState)"
}
Result:
Window visual state: Minimized
Reference:
Get window state of another process
Upvotes: 1
Reputation: 1999
my solution is to force the start of a selenium edgedriver instance maximized. therefore the process list before and after the edgedriver was instantiated are compared and filtered for the 'msedge' process which then first is minimized and afterwards maximized.
$workingPath = 'C:\selenium'
if (($env:Path -split ';') -notcontains $workingPath) {
$env:Path += ";$workingPath"
}
[System.Reflection.Assembly]::LoadFrom("$($workingPath)\WebDriver.dll")
# get all listening processes before new edgedriver is started
$processesBefore = $(netstat -aon | findstr LISTENING)
# launch edgedriver
$EdgeDriver = New-Object OpenQA.Selenium.Edge.EdgeDriver
# get all listening processes after edgedriver started
$processesAfter = $(netstat -aon | findstr LISTENING)
# get the differencing processes (find the new processes)
$processesDiff = Compare-Object -ReferenceObject $processesBefore -DifferenceObject $processesAfter
# get the process ids of new processes
$processIdArray = foreach($process in $processesDiff) {
$process.InputObject.Split(' ')[-1]
}
# get the msedge process
$p = Get-Process -id $processIdArray | where name -eq msedge
# source of howto maximize a window: https://community.spiceworks.com/topic/664020-maximize-an-open-window-with-powershell-win7
Add-Type '[DllImport("user32.dll")] public static extern bool ShowWindowAsync (IntPtr hwnd, int nCmdShow);' -Name Win32ShowWindowAsync -Namespace Win32Functions
# first minimize the window so it can be maximized
$null = [Win32Functions.Win32ShowWindowAsync]::ShowWindowAsync($p.MainWindowHandle, 6)
# lastly maximize the window
$null = [Win32Functions.Win32ShowWindowAsync]::ShowWindowAsync($p.MainWindowHandle, 3)
# navigate to web page and do stuff
$EdgeDriver.Navigate().GoToUrl('https://example.org')
Upvotes: 1