1

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)
    }
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Dennis Hayden
  • 184
  • 1
  • 5
  • 15

2 Answers2

2

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}")
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • I am hoping there is a way to set the script to capture only the IE window. if there is a way to make the IE window the "current" window that would work – Dennis Hayden Jul 19 '11 at 12:21
2

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

Community
  • 1
  • 1
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52