Data-Driven Testing in iTest

Posted by Zhimin Zhan on November 03, 2009|Comments|Read full article

Data-Driven Testing means tests' input and output are driven from external sources, quite commonly in Excel or CSV files. For example, if you have a list of user logins with different roles, and login process is the same but with different assertions. In this case, you can extract the pure test data out in an Excel spreadsheet, use the same test script to load test data and execute one by one. Because RWebSpec/Watir tests are in fact Ruby scripts, it is quite easy to do so.

For impatient readers, you can see and run the sample by opening the demo project C:\Program Files\TestWise\samples\demo\demo.tpr in iTest, run test script file: database_spec.rb.

The Excel file: C:\Program Files\TestWise\samples\demo\testdata\users.xls contains 3 username-password combination.

DESCRIPTION LOGIN PASSWORD EXPECTED_TEXT
Valid Login agileway agileway Login successful!
User name not exists nonexists smartass Login is not valid
Password not match agileway badpass Password is not valid

Test Script

require 'rwebspec'
require 'spreadsheet'

spec "Use Excel for data driven web tests" do
  include RWebSpec::RSpecHelper

  before(:all) do
    open_browser("http://travel.agileway.net")

    # Load Excel file
    excel_file = File.join(File.dirname(__FILE__), "testdata", "users.xls")
    excel_book = Spreadsheet.open excel_file
    @excel_sheet1 = excel_book.worksheet "Users" # or use 0 for first sheet
  end

  before(:each) do
  end

  after(:all) do
    close_browser unless debugging?
  end

  scenario "Load user list from an Excel spreadsheet to test multiple user logins" do
    # Iterate each row in the spreadsheet, use data for test scripts
    @excel_sheet1.each_with_index do |row, idx|
      next if idx == 0 # ignore first row
      login, password, expected_text = row[1], row[2], row[3]
      goto_page("/")
      enter_text("userName", login)
      enter_text("password", password)
      click_button("Sign In")
      page_text.should include(expected_text)
      failsafe{ click_link("SIGN-OFF") } # if logged in OK, try log out
    end
  end

end


Run test scripts from command line

Posted by Zhimin Zhan on November 09, 2009|Comments|Read full article

As you may know, test scripts (RWebSpec or Watir) developed by iTest can be run from command line. This has many advantages, You can run tests on machines without iTest installed, integrate with a continuous build server, run specific (or group of) test case, or even generate customized test report.

In iTest v1.6.7, a default Rakefile (default build language in Ruby, a good rake tutorial here) is added when a project is created. To run all tests (*_spec.rb or *_test.rb) in current folder,

rake

For readers who are curious and want to customize the test execution, have a look source of Rakefile below, it is quite simple, isn’t it!

task :default => ["go"]

# List tests you want to exclude
def excluded_test_files
  ["ignore_spec.rb", "bad_test.rb"]
end

desc "run all tests in this folder"
Spec::Rake::SpecTask.new("go") do |t|
  test_files = Dir.glob("*_spec.rb") + Dir.glob("*_test.rb") - excluded_test_files
  t.spec_files = FileList[test_files]
  t.warning = false
end

Testing complex sites

Posted by Zhimin Zhan on November 19, 2009|Comments|Read full article

We are not living in a perfect world, some web sites are created without automated testing in mind. So we have some sites with complex javascripts, elements without IDs or duplicated IDs. .

Here is one, the test scripts generated from TestWise/Watir recorder won’t work.

  scenario "Site with complex JavaScripts: Recorded" do
    select_option("intFrom", "Brisbane")
    click_link_with_id("toSYD")
    select_option("intDepMonthYear", "Jan 2010")
    select_option("intDepDay", "Sun 03")
    select_option("intRetMonthYear", "Feb 2010")
    select_option("intRetDay", "Wed 03")
    select_option("intAdults", "2")
    click_button("Go")
  end

1. Line 3: click_link_with_id(“toSYD”) failed with error ‘Unable to locate element, using :id, “toSYD”’

Reason: a javascript window pops up as user enters text in destination text field, however that event is not fired when running the test scripts.

Solution: Click the image to bring up the javascript popup window manually.

   image(:id, “toBoxPlusSign”).click 

  1. Line 10: click_button(“Go”) had no effect

Reason: There are more than one button with caption ‘Go’ on the page





Solution: Click the specific button by using :index

    button(:text => "Go", :index => 2).click # click 2nd 'Go' button

Here is the complete test script:

  scenario "Site with complex JavaScripts:  Working version" do
    select_option("intFrom", "Brisbane")
    image(:id, "toBoxPlusSign").click # bring up the popup    
    click_link_with_id("toSYD")
    select_option("intDepMonthYear", "Jan 2010")
    select_option("intDepDay", "Sun 03")
    select_option("intRetMonthYear", "Feb 2010")
    select_option("intRetDay", "Wed 03")
    select_option("intAdults", "2")
    button(:text => "Go", :index => 2).click # click 2nd 'Go' button
  end

Verify database records in functional testing

Posted by Zhimin Zhan on November 30, 2009|Comments|Read full article

Function testing, in a certain way, is verifying the data (retrieved or stored at the database) through the user interface layer. Sometimes, it might be easier to verify database records directly, some SQL skills are required though. RWebSpec provides an easy way to do so, in a easy 3 steps.

  1. Connect to the database
  2. Choose the table
  3. Use easy syntax or SQL to do verification
 story "Check Mysql database" do
    connect_to_database mysql_db(:host => "localhost", :database => "lavabuild_local", :user => "root", :password => ""), true
    load_table("projects") # now can use Project.xxx for easy-to-user datbase query on this table

    Project.count.should == 1  # record count
    Project.first.name.should == "BuildMonitor"
    Project.last.name.should == "BuildMonitor"
    Project.exists?(:name => "BuildMonitor")
  end

Check out the verify_database_spec.rb in demo project.


Work with frames

Posted by Zhimin Zhan on November 10, 2009|Comments|Read full article

TestWise Recorder does not support operations in frames yet. The work around is simple: Ues Watir syntax for operations in frames. Let’s walk through with an example.

Here is a sample page with an iframe embedded, its html source fragment of frames as below:

<IFRAME frameborder='1' id="Frame1" src="/downloads/login_iframe.html"
 Style="HEIGHT: 100px; WIDTH: 320px; MARGIN=0" SCROLLING="no" >

1. Firstly, we use TestWise recorder to record operations on that page, including controls in the iFrame.

2. Paste into TestWise IDE to create a test case, and run it. We will have get an error on first operations in the frame.

3. Go back the recorder in Firefox, click ‘Watir’ tab, copy operations in the frame.

browser.text_field(:name, "username").set("tester")
browser.text_field(:name, "password").set("TestWise")
browser.button(:value,"Login").click

4. Replace the (iframe) test steps in previous test scripts,

enter_text("username", "tester")
enter_text("password", "TestWise")
click_button("Login")

Then replace browser. with frame(:id, “Frame1”), this tell browser the operations context. Run it, pass!

Complete test script:

 
require 'rwebspec'

spec "Page with iFrames" do
  include RWebSpec::RSpecHelper

  before(:all) do
    open_browser("http://TestWise.com/demo/page-with-iframes")
  end

  scenario "Test pages in an iFrame" do
    click_link("Link on page")
    frame(:id, "Frame1").text_field(:name, "username").set("tester")
    frame(:id, "Frame1").text_field(:name, "password").set("TestWise")
    frame(:id, "Frame1").button(:value,"Login").click
  end

end