让 powershell 进入 ie 类并在 ui 测试期间截取 ie 窗口的屏幕截图

发布于 2024-11-24 07:28:53 字数 1408 浏览 1 评论 0原文

所以我正在使用 power shell 运行 ui 测试。

当我收到错误时,我只想截取 ie 窗口的屏幕截图,这可以使用 alt print scrn

%{prtsc}

来完成,但它只截取活动窗口的 jpg 图像。

我尝试过这个
$h = (Get-Process iexplore).MainWindowHandle SetForegroundWindow((获取进程-名称 iexplore).MainWindowHandle) 睡眠 - 秒 2 $h = (Get-Process -id $pid).Ma​​inWindowHandle

另外,任何有关识别 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-12-01 07:28:53

设置活动窗口

您需要做一些不同的事情。首先,您需要这样设置活动窗口:

如何从 Powershell 事件订阅者操作​​设置前台窗口

获取正确的窗口

接下来,您需要处理 IE 生成至少两个进程的事实。所以你需要抓住正确的窗口。

$h = Get-Process | Where-Object {$_.MainWindowTitle -like "My website*"} | Select-Object -ExpandProperty MainWindowHandle

截取屏幕截图

现在您可以通过两种方式之一截取屏幕截图。

  1. 像 JPBlanc 向您展示的那样发送 PrtSc。

    Add-Type -Assembly System.Windows.Forms
    开始-睡眠-秒1
    
    ## 捕获当前窗口 
    [System.Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
    
  2. 来自 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.

$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.

  1. 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}")
    
  2. Take-Screenshot script from PoschCode

没有伤那来痛 2024-12-01 07:28:53

您是否注意捕获没有 % 的整个屏幕:

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}")

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}")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文