Prerequisites
In order to create and run the tests in there are a few things we need to download and install:
- Download Selenium RC
- Download and install Java which is required for running Selenium
- In order to run Selenium tests we need to run Selenium Server which is shipped with Selenium RC.
Since it's a Java program we run it by typing (replacing # with the current server version) java -jar selenium-server-#.jar in a console window.
For more information go to Documentation page
Working with elements
Getting text of an element
Set Element = Driver.findElementBy(Driver.name,"q")
Msgbox Element.getText
Clicking an element (link, checkbox, etc.)
Set Element = Driver.findElementBy(Driver.name,"q")
Element.click
Typing into text field
Set Element = Driver.findElementBy(Driver.name,"q")
Element.sendKeys "VBScript"
Get attribute of an element
Set Element = Driver.findElementBy(Driver.name,"q")
Msgbox Element.getAttributeName
Executing JavaScript
Synchronous script execution
Set Driver = New WebDriver
Driver.connect "127.0.0.1","4444","firefoxproxy", ""
Driver.navigateTo "http://www.google.com"
Driver.executeScript("alert('test')","")
Window handling
Getting current window handle
Set Driver = New WebDriver
Driver.connect "127.0.0.1","4444","firefoxproxy", ""
Driver.navigateTo "http://www.google.com"
Msgbox Driver.getWindowHandle
Google Search Test
Sub Include(ByVal strFile)
Set objFs = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
strFile = WshShell.ExpandEnvironmentStrings(strFile)
file = objFs.GetAbsolutePathName(strFile)
Set objFile = objFs.OpenTextFile(strFile)
strCode = objFile.ReadAll
objFile.Close
ExecuteGlobal(strCode)
End Sub
Include "WebDriver.vbs"
Set Driver = New WebDriver
Driver.connect "127.0.0.1","4444","firefox", ""
Driver.navigateTo "http://www.google.com"
MsgBox "Retrieve the URL of the current page: " & Driver.getCurrentUrl()
Driver.executeScript("alert('test')","")
Set Element = Driver.findElementBy(Driver.name,"q")
Element.sendKeys "VBScript"
Element.submit
MsgBox "Element's tag name: " & Element.getName()
MsgBox "Element attribute name: " & Element.getAttributeName
MsgBox "Element is enabled?: " & Element.isEnabled
MsgBox "Element is displayed?: " & Element.isDisplayed
MsgBox "Element CSS Color property: " & Element.getCssProperty("color")
Help me Buy and Sell a House
Demo page by bassistance.de
ScriptUnit integration example:
Sub Include(ByVal strFile)
Set objFs = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
strFile = WshShell.ExpandEnvironmentStrings(strFile)
file = objFs.GetAbsolutePathName(strFile)
Set objFile = objFs.OpenTextFile(File)
strCode = objFile.ReadAll
objFile.Close
ExecuteGlobal(strCode)
End Sub
Include "WebDriver.vbs"
Dim Driver
Dim sSUT : sSUT = "http://htejera.users.sourceforge.net/vbswebdriver/demo/"
Dim sBrowser : sBrowser = "internet explorer"
sub setup
Set Driver = New WebDriver
Driver.connect "127.0.0.1","4444",sBrowser, ""
end sub
sub Teardown()
Set Driver = Nothing
end sub
Sub Test_completeForm1()
Assert.IsSomething Driver, "object Driver was not created"
Driver.NavigateTo sSUT
'Are you currently working with a real estate agent?
Dim radio : Set radio = Driver.findElementBy(Driver.xpath,"//*[@id='sf1']/div/fieldset/input[2]")
Assert.IsSomething radio, "object Element by xpath was not created"
radio.click
Set radio = Nothing
'When would you like to move?
Dim combo1 : Set combo1 = Driver.findElementBy(Driver.xpath,"//*[@id='recordPurchaseTimeFrameID']/option[2]")
Assert.IsSomething combo1, "object Element by xpath was not created"
combo1.click
Set combo1 = Nothing
'Purchase price range
Dim combo2 : Set combo2 = Driver.findElementBy(Driver.xpath,"//*[@id='recordPurchasePriceRangeID']/option[6]")
Assert.IsSomething combo2, "object Element by xpath was not created"
combo2.click
Set combo2 = Nothing
'State
Dim combo3 : Set combo3 = Driver.findElementBy(Driver.xpath,"//*[@id='recordPurchaseState']/option[6]")
Assert.IsSomething combo3, "object Element by xpath was not created"
combo3.click
Set combo3 = Nothing
'Desired property type
Dim city : Set city = Driver.findElementBy(Driver.xpath,"//*[@id='recordCityName']")
Assert.IsSomething city, "object Element by xpath was not created"
city.sendKeys "Montevideo"
Set city = Nothing
'Desired property type
Dim combo4 : Set combo4 = Driver.findElementBy(Driver.xpath,"//*[@id='recordPurchasePropertyTypeID']/option[3]")
Assert.IsSomething combo4, "object Element by xpath was not created"
combo4.click
Set combo4 = Nothing
'Next
Dim button : Set button = Driver.findElementBy(Driver.name,"formNext1")
Assert.IsSomething button, "object Element by name was not created"
button.click
Set button = Nothing
'Verification
Dim input : Set input = Driver.findElementBy(Driver.name,"recordPropertyAddress1")
Assert.IsSomething input , "object Element by name was not created"
Set input = Nothing
End Sub
Run test
ScriptUnit.exe VBSWebDriver test.vbs /log results.xml.