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.
# Connect to the database
# Choose the table
# 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.
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
# 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
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:
1. Firstly, we use TestWise recorder to record operations on that page, including controls in the iFrame.
!/images/manual/iframe_recorder_TestWise.png!
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.
!/images/manual/iframe_test_failed.png!
3. Go back the recorder in Firefox, click 'Watir' tab, copy operations in the frame.
Then replace *browser.* with *frame(:id, "Frame1")*, this tell browser the operations context. Run it, pass!
!/images/manual/iframe_test_pass.png!
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
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":http://railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial) is added when a project is created. To run all tests (_*_spec.rb_ or _*_test.rb_) in current folder,
rake
!/images/manual/run_rakefile.png!
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
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