From iMacros
Jump to: navigation, search

The iMacros Scripting Interface can be used from any programming or script language. The following example shows you how to call this interface from the Windows PowerShell. The macro is the PowerShell version of the Combine-Macros.vbs VBS script.

 #region Using
 #region Helper methods
 function CreateObject($progID)
 	new-object -com $progID
 function MsgBox($string, $title = 'PowerShell Message')
   $eat = [windows.forms.messagebox]::Show($string, $title)
 function LogOnFailure($logFile, $macroName, $statusCode, $iim)
 	if($statusCode -lt 0) {
 		$logFile.WriteLine($macroName + ": Error-No: " + $statusCode + " => Description: " + $iim.iimGetLastError())
 #region Main code
 # This part is for the script specific error log (instead of popup messages).
 # The advantage of an error log instead of a popup is that the script 
 # continues running even after an error appears. An example of an error would
 # be a timeout due to a temporarily slow web site.  
 # Generate a logfile name based on the script name
 $logFileName = "./combine-macros-errorlog.txt" 
 $fileSystemObject = CreateObject("Scripting.fileSystemObject")
 $logFile = $fileSystemObject.CreateTextFile($logFileName, $True)
 $logFile.WriteLine("Error Log for COMBINE-MACROS.VBS demo script")
  $iim1 = CreateObject("imacros")
   $i = $iim1.iimInit()
 LogOnFailure $logFile "INIT"  $i  $iim1
 $i = $iim1.iimPlay("wsh-start")
 LogOnFailure $logFile "WSH-START" $i $iim1
 $i = $iim1.iimPlay("wsh-lunch")
 LogOnFailure $logFile "WSH-LUNCH" $i $iim1
 $i = $iim1.iimPlay("wsh-submit-button")
 LogOnFailure $logFile "WSH-SUBMIT" $i $iim1
 $i = $iim1.iimExit()
 LogOnFailure $logFile "EXIT" $i $iim1
 # This part is for the script specific error log (instead of popup messages).

This script runs all available demo macros for the specified browser, e.g. iMacros Browser, iMacros for Internet Explorer or iMacros for Firefox, and creates a test report as PowerShell grid table (exported also as a xml file).

There are two options to run macros in the following script: 1. You can ran all macros in a selected folder. 2. You can specify the macros names in the PowerShell script directly.

This script can also serve as template for your own web application regression test scripts.

    iMacros Demo Macros Self-Test Script
    by iOpus/Ipswitch Inc.
# Region Using
# endregion

# Region Main code
Write-Host "Start Self-Test.ps1" -ForegroundColor Yellow

$iim1 = new-object -com "imacros" 

# Specify macros folder
$iMacrosPath = "C:\Users\ipswitch\Documents\iMacros\Macros\Demo";

# Specify the browser, e.g. IM: " "
$Browser=" "
# Specify the macro(s) to be run or just run all macros in the selected folder
$macro = @()
$macro = Get-ChildItem $iMacrosPath -Filter *.iim -Name # runs all the macros in the folder
#$macro += "Filter.iim"
#$macro += "SavePdf.iim"
$countMacros = $macro.Length

# Start browser
$i = $iim1.iimOpen($Browser, "false")
Write-Host "Start browser, return message:" $i $iim1.iimGetErrorText() -ForegroundColor Green

# Execute macro and store the result
Write-Host "Execute test macros" -ForegroundColor Green    
$m = 0
foreach ($element in $macro)
    #Create new row in the table
    $ResultRow = New-Object PSObject
    Add-Member -InputObject $ResultRow -MemberType NoteProperty -Name "No" -Value ""
    Add-Member -InputObject $ResultRow -MemberType NoteProperty -Name Time -Value ""
    Add-Member -InputObject $ResultRow -MemberType NoteProperty -Name "Macro Name" -Value ""
    Add-Member -InputObject $ResultRow -MemberType NoteProperty -Name "Return Result" -Value ""
    Add-Member -InputObject $ResultRow -MemberType NoteProperty -Name "Return Message" -Value ""

    #Retrieve results
    #Macro No.
    $ResultRow."No" = $m + 1

    # Date
    $ResultRow.Time = Get-Date -Format HH:mm:ss
    $ResultRow."Macro Name" = $macro[$m]
    # Return Result
    $ResultRow."Return Result" = $iim1.iimPlay("$iMacrosPath\"+$macro[$m])

    # Return Message
    $ResultRow."Return Message" = $iim1.iimGetErrorText()

    #Save the row to the result table
    $ResultTable += $ResultRow  

#Create GridView of the results
$ResultTable | Export-Clixml -Path $PSScriptRoot\Self-Test-Results.xml
$Date = (Get-Date -Format f)
Import-Clixml -Path $PSScriptRoot\Self-Test-Results.xml | Out-GridView -Title "iMacros Demos Self Test Results ($Date)"
Write-Host "End Test" -ForegroundColor Yellow

$i = $iim1.iimClose()