Ultra Lightweight Web Application UI Automation with PowerShell

The use of ASP.NET Web applications continues to grow, and testing Web applications is an increasingly common task for software developers, testers, and managers. Manual testing remains the keystone of application testing but automated testing has five key advantages, which I like to summarize by the acronym SAPES: compared to manual testing, automated tested has better speed, better accuracy, better precision, better efficiency, and better skill-building characteristics. In an Agile-type development environment, traditional automated testing is difficult because of the difficulty and time required t create the automation — in many cases the test automation is obsolete or irrelevant by the time it’s ready to be used. In many situations, the use of Windows PowerShell, the new Microsoft replacement for the cmd.exe shell and .BAT files, changes all that. You can quickly create what I’ve coined "Ultra Lightweight Software Test Automation" (ULSTA) using PowerShell. For example, the screenshot below (running on Windows Vista) was produced by the following code, which took only about 30 minutes to create.
# file: webAppUITestVista.ps1
write-host "`nBegin test automation using Windows PowerShell,"
write-host " Vista version`n"
write-host ‘Launching IE’
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
write-host ‘Navigating to StatCalc Web application’
write-host "`nGetting controls"
$doc = $ie.document
$tb1 = $doc.getElementByID("TextBox1")
$rb1 = $doc.getElementByID("RadioButton1")
$rb2 = $doc.getElementByID("RadioButton2")
$rb3 = $doc.getElementByID("RadioButton3")
$btn = $doc.getElementByID("Button1")
write-host "Setting input to 30 60"
$tb1.value = "30 60"
write-host "Selecting ‘Harmonic Mean’ option"
$rb3.checked = $true
write-host "`nClicking button"
write-host "`nChecking for 40.0000"
$tb2 = $doc.getElementByID("TextBox2")
$ans = $tb2.value
if ($ans -eq ‘40.0000’) {
  write-host "`nTest scenario: Pass" -foregroundcolor ‘green’
else {
  write-host "`nTest scenario: *FAIL*" -foregroundcolor ‘red’
write-host "`nEnd test automation`n"
# end script
If you haven’t experimented with PowerShell yet, I encourage you to check it out at http://www.microsoft.com/powershell
This entry was posted in Software Test Automation. Bookmark the permalink.