4

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?

wrieedx
  • 254
  • 1
  • 3
  • 14
  • 1
    You could add `--start-maximized` as an argument to your driver to get a defined state on startup and after that track the state by yourself. – stackprotector Sep 13 '21 at 08:35
  • @stackprotector Nice idea, but I'm interested in finding out if the window is minimized regardless of any previous state, i.e. whether maximized or not. – wrieedx Sep 14 '21 at 06:11
  • 1
    maybe this helps? http://vcloud-lab.com/entries/powershell/powershell-find-application-window-state-minimized-or-maximized – Guenther Schmitz Sep 15 '21 at 04:48
  • @GuentherSchmitz Very interesting, but one issue is that there may be multiple Edge windows open at any given time. I am only interested in whether the Edge window instantiated by the selenium webdriver is minimized or not (not any Edge windows instantiated separately by the user). – wrieedx Sep 15 '21 at 07:06

3 Answers3

1

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

K. B.
  • 3,342
  • 3
  • 19
  • 32
1

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')
Guenther Schmitz
  • 1,955
  • 1
  • 9
  • 23
-1

The best way is to force start the selenium edgedriver.

By that it will force minimize.

Yun
  • 3,056
  • 6
  • 9
  • 28