【问题标题】:getting powershell to class ie and during a ui test take a screenshot of the ie window让 powershell 进入 ie 类,并在 ui 测试期间截取 ie 窗口的屏幕截图
【发布时间】:2011-07-15 14:24:00
【问题描述】:

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

当我遇到错误时,我想只截取 ie 窗口的屏幕截图,这可以通过 alt print scrn 完成

%{prtsc}

但它只需要活动窗口的 jpg。

我试过这个
$h = (Get-Process iexplore).MainWindowHandle SetForegroundWindow((Get-Process -name iexplore).MainWindowHandle) 睡眠-秒 2 $h = (Get-Process -id $pid).Ma​​inWindowHandle

另外,任何有关识别错误的方法的帮助都将非常感谢。

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

【问题讨论】:

    标签: internet-explorer powershell coded-ui-tests ui-testing


    【解决方案1】:

    设置活动窗口

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

    How to set foreground Window from Powershell event subscriber action

    获取正确的窗口

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

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

    截图

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

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

      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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我希望有一种方法可以将脚本设置为仅捕获 IE 窗口。如果有办法让 IE 窗口成为可以工作的“当前”窗口
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 2018-11-13
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多