Sunday, May 30, 2010

Template for IE automation with PowerShell

<#
    .SYNOPSIS 
      Validates application login userid and password from a given excel sheet
    .EXAMPLE
     validateLogin users.xls
     This command picks userid and password from given excel sheets and validates from given URL
#>
#Set-PSDebug -trace 2 # Debug Information
#Set-PSDebug -step # Debug using stepping


if ($args) 
{
 throw "USAGE: validateLogin users.xls"
}

function openExcel($args)
{
 $xl = new-object -com Excel.Application
 foreach ( $wbk in $args )
 {
  foreach ($user in $users)
  {
   $xl.Workbooks.open($args) | out-null  #drop the object info output
   $xl.Cells.Item(1,1).Value()
   $xl.Cells.Item(3,3).Value()
   $xl.Cells.Item(6,8).Value()
   $xl.Workbooks.Close()
  }
 }
}


Function NavigateTo([string] $url, [int] $delayTime = 100)
{
  Write-Verbose "Navigating to $url"
  
  $global:ie.Navigate($url)
  
  WaitForPage $delayTime
}

Function WaitForPage([int] $delayTime = 100)
{
  $loaded = $false
  
  while ($loaded -eq $false) {
    [System.Threading.Thread]::Sleep($delayTime) 
    
    #If the browser is not busy, the page is loaded
    if (-not $global:ie.Busy)
    {
      $loaded = $true
    }
  }
  
  $global:doc = $global:ie.Document
}

Function SetElementValueByName($name, $value, [int] $position = 0) {
  if ($global:doc -eq $null) {
    Write-Error "Document is null"
    break
  }
  $elements = @($global:doc.getElementsByName($name))
  if ($elements.Count -ne 0) {
    $elements[$position].Value = $value
  }
  else {
    Write-Warning "Couldn't find any element with name ""$name"""
  }
}

Function ClickElementById($id)
{
  $element = $global:doc.getElementById($id)
  if ($element -ne $null) {
    $element.Click()
    WaitForPage
  }
  else {
    Write-Error "Couldn't find element with id ""$id"""
    break
  }
}

Function ClickElementByName($name, [int] $position = 0)
{
  if ($global:doc -eq $null) {
    Write-Error "Document is null"
    break
  }
  $elements = @($global:doc.getElementsByName($name))
  if ($elements.Count -ne 0) {
    $elements[$position].Click()
    WaitForPage
  }
  else {
    Write-Error "Couldn't find element with name ""$name"" at position ""$position"""
    break
  }
}

#Entry point
#$verbosePreference = "SilentlyContinue"
$verbosePreference = "Continue"

$global:ie = New-Object -com "InternetExplorer.Application"
$global:ie.Navigate("about:blank")
$global:ie.visible = $true
$global:ie.children



#NavigateTo "http://www.bing.com"
#SetElementValueByName "q" "powershell variable scope"
#ClickElementById "sb_form_go"
#Can also do: ClickElementByName "go"

#NavigateTo "http://mail.com"
Write-Host -ForegroundColor Green "Attempting to login to Web Application.";
#SetElementValueByName "login1" "xxx@yyy.com"
#SetElementValueByName "password" "password"
#SetElementValueByName "lr" "lang_en"
#ClickElementById "btnSignIn"

function validate
{
 begin{
  #All init for this function
 }

 process{
  #All process function
 }

 end{
  #All finilazers
 }
}

No comments: