getting powershell to class ie and during a ui test take a screenshot of the ie window
So i'm running a ui test with power shell.
When i get an error i want to take a screen shot of just the ie window this can be done with alt print scrn
%{prtsc}
but it only takes a jpg of the active window.
I tryied this
$h = (Get-Process iexplore).MainWindowHandle SetForegroundWindow((Get-Process -name iexplore).MainWindowHandle) sleep -sec 2 $h = (Get-Process -id $pid).MainWindowHandleAlso any help with a way to identify ie error would be great thanks.
function screenshot
{
param(
[Switch]$OfWindow
)
begin {
Add-Type -AssemblyName System.Drawing
$jpegCodec = [Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() |
Where-Object { $_.FormatDescription -eq "JPEG" }
}
process {
Start-Sleep -Milliseconds 250
if ($OfWindow) {
[Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
} else {
[Windows.Forms.Sendkeys]::SendWait("{PrtSc}")
}
Start-Sleep -Milliseconds 250
$bitmap = [Windows.Forms.Clipboard]::GetImage()
$ep = New-Object Drawing.Imaging.EncoderParameters
$ep.Param[0] = New-Object Drawing.Imaging.EncoderParameter ([System.Drawing.Imaging.Encoder]::Quality, [long]100)
$screenCapturePathBase = "$pwd\ScreenCapture"
$c = 0
while (Test-Path "${screenCapturePathBase}${c}.jpg") {
$c++}
$bitmap.Save("${screenCapturePathBase}${c}.jpg", $jpegCodec, $ep)开发者_开发问答
}
}
Setting the active window
There are a couple things you need to do differently. First, you need to set the active window this way:
How to set foreground Window from Powershell event subscriber action
Getting the right window
Next, you need to deal with the fact that IE spawns at least two processes. So you need to grab the right window.
$h = Get-Process | Where-Object {$_.MainWindowTitle -like "My website*"} | Select-Object -ExpandProperty MainWindowHandle
Taking the screenshot
Now you can take the screenshot one of two ways.
Send PrtSc like JPBlanc showed you.
Add-Type -Assembly System.Windows.Forms Start-Sleep -seconds 1 ## Capture the current window [System.Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
Take-Screenshot script from PoschCode
Do you take care about that to capture the entire screen it's without % :
Add-Type -Assembly System.Windows.Forms
Start-Sleep -seconds 1
## Capture the entire screen
[System.Windows.Forms.Sendkeys]::SendWait("{PrtSc}")
## Capture the current window
[System.Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
精彩评论