Narayan Raman presented on Functional Testing of Web Applications using Sahi at the XP Goa Day in Goa University.
View more presentations from narayanraman.
As part of a presentation , we did a small demo on record and playback of a script using Sahi, then refactored the code to be maintainable.
The site under test is available here: /demo/training
The first cut from the recorder came out to be this:
_setValue(_textbox("user"), "test");
_setValue(_password("password"), "secret");
_click(_submit("Login"));
_setValue(_textbox("q"), "2");
_setValue(_textbox("q[1]"), "1");
_setValue(_textbox("q[2]"), "1");
_click(_button("Add"));
_assertExists(_textbox("total"));
_assert(_isVisible(_textbox("total")));
_assertEqual("1150", _textbox("total").value);
_click(_button("Logout"));
This was then refactored into 2 scripts, one containing functions and the other invoking it:
// goa3_included.sah
function login($username, $password){
_setValue(_textbox("user"), $username);
_setValue(_password("password"), $password);
_click(_submit("Login"));
}
function addBooks($numJava, $numRuby, $numPython){
_setValue(_textbox("q"), $numJava);
_setValue(_textbox("q[1]"), $numRuby);
_setValue(_textbox("q[2]"), $numPython);
_click(_button("Add"));
}
function verifyTotal($total){
_assertEqual($total, _textbox("total").value);
}
function logout(){
_click(_button("Logout"));}// goa3.sah
_include("goa3_included.sah");
login("test", "secret");
addBooks(2, 1, 1);
verifyTotal(1150);
logout();
The next step was to modifyfunction addBooks($numJava, $numRuby, $numPython){
_setValue(_textbox("q"), $numJava);
_setValue(_textbox("q[1]"), $numRuby);
_setValue(_textbox("q[2]"), $numPython);
_click(_button("Add"));
}such that identifiers "q", "q[1]" and "q[2]" become more meaningful and are independent of their order. Using the _near API, the function becomes:
function addBooks($numJava, $numRuby, $numPython){
_setValue(_textbox("q", _near(_cell("Core Java"))), $numJava);
_setValue(_textbox("q", _near(_cell("Ruby for Rails"))), $numRuby);
_setValue(_textbox("q", _near(_cell("Python Cookbook"))), $numPython);
_click(_button("Add"));
}We then data drive the whole test by wrapping the various steps into a single function "addAndVerify", build a 2 dimensional array of values and then invoke "addAndVerify" for each row of values using _dataDrive
// club the functionality into a single function
function addAndVerify($numJava, $numRuby, $numPython, $total){
login("test", "secret");
addBooks($numJava, $numRuby, $numPython);
verifyTotal($total);
logout();
}
// build a 2D array
var $data = [
[2, 1, 1, 1150],
[3, 2, 1, 1650],
[1, 1, 1, 850]
]
// automatically invoke addAndVerify for each row in $data.
_dataDrive(addAndVerify, $data);
We concluded the talk with an enthusiastic Q & A session.
Thank you Goa University for being a great host!