Friday, April 9, 2010

Watir:Validating Test Results: How to validate Test results in Watir?

Validating Test Results

How do we tell whether the test passed or failed? It is good practice to use a verification point in your test case.
To create a valid test case, we need to use some sort of validation. It isn't enough to have a sequence of events without validation in a test script. Watir can check the state of objects in the Internet Explorer DOM, and can check to see if a page contains the objects that are expected for the test to pass.

Object Exists

A simple verification point is to use the Watir method contains_text.
In our test, imagine we have gone through a sequence of steps to reach the desired point. This is a web page that contains the following text:
Reached test verification point.
Our test will pass if this is what is displayed on the page at our verification point.
Watir code to check that the text exists on the web page is contains_text method:
ie.contains_text("Reached test verification point.")
We will need to add some scripting code to validate that this occurred. Scripting code to let us know whether the test passed or not:
if ie.contains_text("Reached test verification point.")
  puts: "Test passed. Page contains the text: Reached test verification point."
  puts: "Test failed! Page didn't contain text: Reached test verification point."
This is what the scripting code above does:
  • checks if there is text Reached test verification point.
  • if true, print our passing message to the screen
  • if false, print our failure message to the screen
  • end our if block

No comments:

Post a Comment