Windows Powershelltm is a new Microsoft command shell and scripting language that can be used as a platform for a variety of lightweight test automation. In this month's Test run column, I'll show you how to use Windows PowerShell to create a quick and easy UI test automation for all types of WEB applications by automating the Internet explorer®. This column is mainly for beginners, but experienced engineers can also find some interesting information here.
Before you begin, make sure that the site you are testing is added to the Trusted Sites list in Internet Explorer-otherwise the script may not work. Start an automated demo by issuing the following Windows PowerShell command:
PS c:\> $ie = new-object-com "Internetexplorer.application"
It creates an instance of the typical InternetExplorer COM automation object in the SHDocVw.dll library. The New-object keyword is a Windows PowerShell cmdlet (pronounced Command-let). There are about 130 cmdlet, which form the core of Windows PowerShell functionality. You can get a list of cmdlet by running Get-command and always get Help on the cmdlet by running the Get-help command. I provided New-object with a-com switch (actually a –comobject shortcut). It specifies that a typical COM object is instantiated using the object's ProgID instead of instantiating the managed object.
Stores the generated objects in a $ie variable (all Windows PowerShell variables are preceded by a $ character that makes them easier to differentiate from other token types). Next, use the Navigate method to load the virtual Minicalc WEB application to be tested into the browser Automation object:
PS c:\> $ie. Navigate ("http://localhost/MiniCalc/Default.aspx")
PS c:\> $ie. Visible = $true
One of the powerful features of Windows PowerShell is the ability to help you find objects. When you use the old scripting technology, if you do not know that the InternetExplorer object has a Navigate method, you must use some type of external reference to find the information. However, if you are using Windows PowerShell, there are several quick ways to view the available methods and properties for an object. For example, type "$ie." You can then press the TAB key to complete the function using the TAB key. Each key is pressed, and an available property or method is displayed. You can also use the Get-member cmdlet to get a list of all available properties and methods and their signatures:
PS c:\> $ie | Get-member | More
Another Windows PowerShell discovery feature is command completion. For example, if you type $ie. VI and then press the Tab key, Windows PowerShell will type the ie.visible statement for me. These discovery features in Windows PowerShell can save a lot of time.
Next, get a reference to all the input controls on the application that you are testing:
PS c:\> $doc = $ie. Document
PS c:\> $tb 1 = $doc. getElementById ("TextBox1")
PS c:\> $tb 2 = $doc. getelement Byid ("TextBox2")
ps c:\> $add = $doc. getElementById ("RadioButton1")
ps c:\> $btn = $doc. getElementById (" Button1 ")