让 powershell 进入 ie 类并在 ui 测试期间截取 ie 窗口的屏幕截图
所以我正在使用 power shell 运行 ui 测试。
当我收到错误时,我只想截取 ie 窗口的屏幕截图,这可以使用 alt print scrn
%{prtsc}
来完成,但它只截取活动窗口的 jpg 图像。
我尝试过这个
$h = (Get-Process iexplore).MainWindowHandle SetForegroundWindow((获取进程-名称 iexplore).MainWindowHandle) 睡眠 - 秒 2 $h = (Get-Process -id $pid).MainWindowHandle
另外,任何有关识别 ie 错误的方法的帮助将非常感谢。
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)
}
}
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).MainWindowHandle
Also 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)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置活动窗口
您需要做一些不同的事情。首先,您需要这样设置活动窗口:
如何从 Powershell 事件订阅者操作设置前台窗口
获取正确的窗口
接下来,您需要处理 IE 生成至少两个进程的事实。所以你需要抓住正确的窗口。
截取屏幕截图
现在您可以通过两种方式之一截取屏幕截图。
像 JPBlanc 向您展示的那样发送 PrtSc。
来自 PoschCode 的截屏脚本
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.
Taking the screenshot
Now you can take the screenshot one of two ways.
Send PrtSc like JPBlanc showed you.
Take-Screenshot script from PoschCode
您是否注意捕获没有 % 的整个屏幕:
Do you take care about that to capture the entire screen it's without % :