Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Test Framework Example with MiniTest

Justin Ko edited this page Jun 22, 2013 · 1 revision

This is a simples usage of Watir and Minitest. You do not have to install anything, MiniTest is included in Ruby.

require 'minitest/autorun'
require 'watir'
class GoogleHomePage <MiniTest::Unit::TestCase
  def test_there_should_be_text_About_Google
    browser = Watir::Browser.new
    browser.goto("http://www.google.com")
    assert(browser.text.include?("About Google"))
  end
end

If you run this code, you should see something like this.

Run options: --seed 6282

# Running tests:

.

Finished tests in 29.064000s, 0.0344 tests/s, 0.0344 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

Using MiniTest Assertions

Using xUnit assertions can be a more powerful way of validating test results.

Ruby has an xUnit framework called Minitest that we can use with Watir. To use MiniTest in your test scripts, enter the following in your test script:

require 'minitest/autorun'

Create a Test Instance

We create a new class for our test suite which is inherited from the MiniTest::Unit::TestCase class.

class TC_myTest < MiniTest::Unit::TestCase
  # fill in Test Case methods here
end

Create a Test Case Method

Within our test case class, we need to declare test cases as methods like this:

def test_myTestCase
  # fill in method body with Watir code and assertion here
end

Test case method names must be prefixed with the word test.

When we run the test script from the command line, MiniTest 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 in a random order.

The test case class with test cases defined will look like this:

class TC_myTest < MiniTest::Unit::TestCase

  def test_ myTestCase
    # Watir code and assertion here
  end

  def test_anotherTestCase
    # Watir code and assertion here
  end

  def test_aTestCase
    # Watir code and assertion here
  end

end

Use Assertions

Watir can support assertions by wrapping Watir methods within an assert:

assert(browser.div.text.includes?("Reached test verification point."))

Watir can test for many different states of objects. We can use assertions to check if 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 MiniTest::Unit class, and use one browser instance, use either a class variable @@browser, or a global variable $browser. This allows the test script to use the same browser instance for all the test cases.

Note: use global variables with caution.

Setup and Teardown

The method names setup and teardown are reserved for MiniTest::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.

def setup
  # fill in code that will run before every test case here
end

def teardown
  # fill in code that will run after every test case here
end

Why are my test cases running in the wrong order?

Subclassing MiniTest::Unit::TestCase will make your tests run in random order. Therefore in the case of this code:

class SampleTest < MiniTest::Unit::TestCase

  def test_login
    # login test code, etc
  end

  def test_account
    # account test code, etc
  end

end

In one test run, test_login might right before test_account. Other times, test_account might run before test_login.

You can force the tests to run in a specific order by overriding the test order to be alphabetically and naming the tests alphabetically. Note that it is usually not recommended to have inter-dependent tests.

class SampleTest < MiniTest::Unit::TestCase

  def self.test_order
    :alpha
  end	

  def test_001_login
    # login test code, etc
  end

  def test_002_account
    # account test code, etc
  end

end