Internet explorer 让powershell进入ie类,并在ui测试期间拍摄ie窗口的屏幕截图

Internet explorer 让powershell进入ie类,并在ui测试期间拍摄ie窗口的屏幕截图,internet-explorer,powershell,coded-ui-tests,ui-testing,Internet Explorer,Powershell,Coded Ui Tests,Ui Testing,所以我正在用powershell运行一个ui测试 当我得到一个错误,我想采取的只是ie窗口的屏幕截图,这可以通过alt打印scrn %{prtsc} 但它只需要一个活动窗口的jpg 我试过这个 $h=(获取进程iexplore).MainWindowHandle SetForegroundWindow((获取进程-名称iexplore).MainWindowHandle) 睡眠-第2节 $h=(获取进程-id$pid).MainWindowHandle 另外,如果您能帮助您识别ie错误,也将不胜

所以我正在用powershell运行一个ui测试

当我得到一个错误,我想采取的只是ie窗口的屏幕截图,这可以通过alt打印scrn

%{prtsc}

但它只需要一个活动窗口的jpg

我试过这个
$h=(获取进程iexplore).MainWindowHandle SetForegroundWindow((获取进程-名称iexplore).MainWindowHandle) 睡眠-第2节 $h=(获取进程-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)
    }
}

你注意到了吗?要捕获整个屏幕,它没有%:

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

你注意到了吗?要捕获整个屏幕,它没有%:

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至少产生两个进程的事实。所以你需要抓住正确的窗口

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

现在,您可以从两种方式之一拍摄屏幕截图

  • 像JPBlanc向你展示的那样发送PrtSc

    Add-Type -Assembly System.Windows.Forms
    Start-Sleep -seconds 1
    
    ## Capture the current window 
    [System.Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
    

  • 设置活动窗口

    有几件事你需要改变。首先,您需要按以下方式设置活动窗口:

    获取正确的窗口

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

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

    现在,您可以从两种方式之一拍摄屏幕截图

  • 像JPBlanc向你展示的那样发送PrtSc

    Add-Type -Assembly System.Windows.Forms
    Start-Sleep -seconds 1
    
    ## Capture the current window 
    [System.Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
    

  • 我希望有一种方法可以将脚本设置为只捕获IE窗口。如果有办法使IE窗口成为“当前”窗口,我希望有办法将脚本设置为只捕获IE窗口。如果有一种方法可以使IE窗口成为“当前”窗口,那么它就可以工作了