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.

Installation

  • Download the latest build from: Sourceforge
  • Extract it to a directory of your choice

Compatibility

VBS WebDriver has been reported to run on the following operating systems / architectures. If you have VBS WebDriver running on something not listed here, please tell us!

Operating systems
Windows XP - 32 bits
Windows Vista - 32 bits
Windows Seven - 32 bits

Run the demo tests

VBScript WebDriver contains some web tests ready to run that you can use as "templates". You can run them just by moving to the project directory and calling:


runTest.bat

Go to Examples page for more examples.

List of implemented methods.

List of JsonWireProtocol implemented methods

Method Summary
acceptAlert Accepts the currently displayed alert dialog.
closeWindow Close the current window.
dismissAlert Dismisses the currently displayed alert dialog.
executeScript Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
findElementBy Search for an element on the page, starting from the document root.
getAlertText Gets the text of the currently displayed JavaScript alert(), confirm(), or prompt() dialog.
getCurrentUrl Retrieve the URL of the current page.
getScreenshot Take a screenshot of the current page.
getTitle Get the current page title.
getWindowHandle Retrieve the current window handle.
navigateTo Navigate to a new URL
refresh Refresh the current page.
clear Clear a TEXTAREA or text INPUT element's value.
click Click any mouse button (at the coordinates set by the last moveto command).
getAttributeName Get the value of an element's attribute.
getCssProperty Query the value of an element's computed CSS property.
getName Query for an element's tag name.
getText Returns the visible text for the element.
isDisplayed Determine if an element is currently displayed.
isEnabled Determine if an element is currently enabled.
sendKeys Send a sequence of key strokes to an element.
submit Submit a FORM element.

Include

VBScript 5.6 does not provide a statement to include files. But the ExecuteGlobal statement can be used to include program code from a file. The function below shows how to do it.


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"

Integrating with unit testing tools

VBS WebDriver is not a general purpose testing framework (unit or otherwise) is only a simple driver for WebDriver (Selenium 2), but VBScript WebDriver can work and be able to integrate with unit testing tools like ScriptUnit, ASPUnit or VBslib


ScriptUnit integration

VBS Webdriver integrates nicely with ScriptUnit. This is a tester in the same vein as JUnit and NUnit, but for the ActiveX scripting languages. It is similar to COMUnit, but without requiring the tests to be embedded in the testing application. ScriptUnit loads tests dynamically, so you can focus on the tests rather than the framework.


Write test

Write your VBS WebDriver tests in functions named TestXxxxx. You do not need to write a main-line that calls the functions - ScriptUnit will do that for you, and will let you call individual functions if you want.

Test functions must not return values, and take no parameters. The name must start with the word "Test".

If you define function called "Setup", it will be called before every test function in the file.

If you define a function called "TearDown", it will be called after every test function in the file. It will be called even if the test function fails.

If an unexpected error occurs during Setup or Teardown, then the test as a whole will be marked as failed.

If you do not write any test functions in your script, then the mainline will be run, and if no unexpected errors occur, then the script will be marked as a successful test.

For 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	


Assert commands

The scripting language is primed with an Assert object that supports the following methods for checking the validity of your object. If a check fails, then the message is logged and the script is stopped and marked with an error (red ball).

When there are multiple variations grouped together (e.g. Equal/Equals/IsEqual) then they are synonyms with no difference except for syntax. Use the one which reads best for you.


Command Summary
Assert.IsTrue( expr, [msg] ) expr is a boolean expression that should evaluate to TRUE.
msg optional message to note in the log if expr is false.
Example:


set db = CreateObject("Database")
Assert.IsTrue db.Login("foo","bar"), "Login failed"

Assert.IsFalse(expr,[msg]) expr is a boolean expression that should evaluate to FALSE.
msg optional message to note in the log if expr is true.
Assert.Equal(a,b,[msg])
Assert.Equals(a,b,[msg])
Assert.IsEqual(a,b,[msg])
Example


 v = LCase("Foo")
 Assert.IsEqual( "foo", v )

Assert.NotEqual(a,b,[msg]) Assert.NotEquals(a,b,[msg]) Assert.IsNotEqual(a,b,[msg]) Example


 v = UCase("Foo")
 Assert.IsNotEqual( "foo", v, "upper and lowercase are the same" )

Assert.IsNothing(obj,[msg])
Assert.IsNull(obj,[msg])
obj is an object expression that should be null/nothing.
msg is an optional message for the log if the obj is not nothing.
Example


set v = CreateObject("scripting.dictionary")
set v = nothing
Assert.IsNothing( v, "variable was not cleared" )

Assert.IsNotNull(obj,[msg])
Assert.IsSomething(obj,[msg])
obj is an object expression that should not be null/nothing.
msg is an optional message for the log if the obj is null.
Example


set v = CreateObject("scripting.dictionary")
Assert.IsSomething( v, "object was not created" )
set v = nothing
Assert.IsNothing( v, "variable was not cleared" )

Assert.ExpectError [errormsg] errormsg is an optional partial error message you expect to be raised. This means that if the next error that occurs contains the errormsg in its description, then the error will be ignored.
Example


Assert.ExpectError "division by 0"
x = 1 / 0
Assert.Trace "should not reach this point - should have got error by now"

Assert.Failure([msg])
Assert.Error([msg])
Reports an error to the framework. Useful for "Not-implemented" tests and "Should not get here" type tests. Note that this type of error can never be expected. i.e. ExpectError won't catch the error signaled by this.


dim x
x = 1 / 0
Assert.Error "should not reach this point - should have got error by now"


Run test

Open ScriptUnit.exe, drag files into the ScriptUnit window, or add whole directories of test files in one go and just click the RUN button to reload the file and re-run the tests. Tests are easy to run and re-run just click the button to load and run the current test again.



Also Test runs can be automated using the command-line. Results can be stored in an NUnit-compatible XML file. This means that tests can be easily integrated into an automated build system.

For example, to run all the tests in the Tests folder, plus the C:\test.vbs file, and log the results to results.xml can be done like this:


ScriptUnit.exe /Q Tests c:\test.vbs /log results.xml


Script Unit Command Line

Method Summary
/? /h Help.
/q Quiet - do not show the GUI - runs the tests once automatically.
/regserver Register the COM components inside ScriptUnit.exe.
/unregserver Remove the COM components from the registry.
/log filename Save a copy of the test results to an XML file. The XML format corresponds to NUnit.
filepath Load the named script file.
dirpath Load all the script files in the directory.