This is the simplest usage of Watir and Test::Unit that I could think of. You do not have to install anything, Test::Unit is included in Ruby.
If you run this code, you should see something like this.
Using xUnit assertions can be a more powerful way of validating test results.
Ruby has an xUnit framework called Test::Unit that we can use with Watir. Look at the Watir Examples and unit tests for executable examples. To use Test::Unit in your test scripts, enter the following in your test script:
We create a new class for our test suite which is inherited from the Test::Unit TestCase class.
Within our test case class, we need to declare test cases as methods like this:
Test case method names must be prefixed with the word test.
When we run the test script from the command line, Test::Unit uses reflection to go through our test class and execute all the test cases declared in it. The runner by default executes the test cases alphabetically, so if you need to chain test cases, prefix letters from the alphabet or numbers after the test prefix to force them to run in order. ex. test_a_mytest.
Note: If you use numbers in your method names, note that 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 will be executed in this order: 1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9. Instead, use this format: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12. ex. test_01_mytest, test_02_mytest, test_03_mytest will run in the order expected.
The test case class with test cases defined will look like this:
Watir can support assertions by wrapping Watir methods within an assert:
Watir can test for many different states of objects. We can use assertions to check of objects exists, are enabled or disabled, or any other state that the DOM tells the web browser about an object.
If you would like to chain test cases in a single Test::Unit class, and use one Internet Explorer instance, use either a class variable @@ie, or a global variable $ie. This allows the test script to use the same Internet Explorer instance for all the test cases.
Note: use global variables with caution.
The method names setup and teardown are reserved for Test::Unit. If you would like to use setup and teardown functionality, simply use those as method names for the actions you want executed before and after executing each test case.
Subclassing Test::Unit::TestCase will make your tests run in alphabetical order. Therefore in the case of this code:
The second test, test_account, will run before test_login.
To run the tests in the order they are defined in the suite, subclass Watir::TestCase instead.
For more information see Test-Unit Patch page.