An Example of Automated Test Design

Integrating Data-driven Testing in Sahi with N-way/All Combinations (Decision Table) generated by TestersDesk.com

Introduction: Below is a small sample page where the discount on a commodity is being calculated based on various factors.

Test Requirement: Based on the individual test input selection made for each factor/parameter in the form provided, the calculated discount has to be validated for accuracy (Note: below the form, there is a chart that shows how the input data shall influence the outcome).

Sahi
Better test design. Faster test data.
Enter the type of guitar
Enter no of units
Where should the items be shipped?
Payment method

Discount Available: %


Total discount is a simple summation of individual discounts:

Guitar Type Basic Acoustic 2%
Basic Acoustic with Kit 3%
World-class Acoustic with Kit 4%
Quantity 1-20 guitars 5%
More than 20 guitars 10%
Shipping Domestic 5%
Overseas 3%
Payment mode Credit Card 0%
Cash on receipt 4%


Here is how Sahi can be used to test it:

Create a single test case by choosing various options for the guitar.
For example, this could be the sample script which was recorded.
_setSelected(_select("typeofguitar"), "Basic Acoustic with Kit - Each Unit Rs. 4500");
_setValue(_textbox("qty"), "12");
_click(_radio("shipto[1]"));
_click(_radio("pay[1]"));
_click(_button("Click for discount >"));
_assertContainsText("15", _span("discount"));
We modify the script and replace the identification of various elements with their ids, for better readability. (This will be the default recording from the next version of Sahi)
_setSelected(_select("typeofguitar"), "BasicWithKit");
_setValue(_textbox("qty"), "12");
_click(_radio("Overseas"));
_click(_radio("CreditCard"));
_click(_button("Click for discount >"));
_assertContainsText("15", _span("discount"));
Create a function out of this and invoke it.
function checkDiscount($guitarType, $quantity, $destination, $paymentMode, $expectedDiscount){
	_setSelected(_select("typeofguitar"), $guitarType);
	_setValue(_textbox("qty"), $quantity);
	_click(_radio($destination));
	_click(_radio($paymentMode));
	_click(_button("Click for discount >"));
	_assertContainsText($expectedDiscount, _span("discount"));
}
checkDiscount("BasicWithKit", "12", "Overseas", "CreditCard", "15");
Now we need to create the different cases and the desired output for this. There is a simple way to create this data. We use TestersDesk.com (a free online toolkit for the testing community that provides test design and test data tools) and provide the below input model to the N-way TestCase Generator (all-combinations).
TypeOfGuitar:Basic,BasicWithKit,Advanced
Qty:12,24
Location:Domestic,Overseas
PaymentMode:CashOnReceipt,CreditCard
Note that the format is ParameterName:Value1,Value2,Value3 etc in each line. The generated file may usually open in MS Excel but try and open it in a normal text editor and copy what is needed. This will produce the below output, to which you can add one more column called expected result and enter the needed values.
Basic,12,Domestic,CashOnReceipt
Basic,12,Domestic,CreditCard
Basic,12,Overseas,CashOnReceipt
Basic,12,Overseas,CreditCard
Basic,24,Domestic,CashOnReceipt
Basic,24,Domestic,CreditCard
Basic,24,Overseas,CashOnReceipt
Basic,24,Overseas,CreditCard
BasicWithKit,12,Domestic,CashOnReceipt
BasicWithKit,12,Domestic,CreditCard
BasicWithKit,12,Overseas,CashOnReceipt
BasicWithKit,12,Overseas,CreditCard
BasicWithKit,24,Domestic,CashOnReceipt
BasicWithKit,24,Domestic,CreditCard
BasicWithKit,24,Overseas,CashOnReceipt
BasicWithKit,24,Overseas,CreditCard
Advanced,12,Domestic,CashOnReceipt
Advanced,12,Domestic,CreditCard
Advanced,12,Overseas,CashOnReceipt
Advanced,12,Overseas,CreditCard
Advanced,24,Domestic,CashOnReceipt
Advanced,24,Domestic,CreditCard
Advanced,24,Overseas,CashOnReceipt
Advanced,24,Overseas,CreditCard
Now, adding one more column with the expected result will have the 24 tests ready. So we compute the values based on the rules above and come up with this
Basic,12,Domestic,CashOnReceipt,16
Basic,12,Domestic,CreditCard,12
Basic,12,Overseas,CashOnReceipt,14
Basic,12,Overseas,CreditCard,10
Basic,24,Domestic,CashOnReceipt,21
Basic,24,Domestic,CreditCard,17
Basic,24,Overseas,CashOnReceipt,19
Basic,24,Overseas,CreditCard,15
BasicWithKit,12,Domestic,CashOnReceipt,17
BasicWithKit,12,Domestic,CreditCard,13
BasicWithKit,12,Overseas,CashOnReceipt,15
BasicWithKit,12,Overseas,CreditCard,11
BasicWithKit,24,Domestic,CashOnReceipt,22
BasicWithKit,24,Domestic,CreditCard,18
BasicWithKit,24,Overseas,CashOnReceipt,20
BasicWithKit,24,Overseas,CreditCard,16
Advanced,12,Domestic,CashOnReceipt,18
Advanced,12,Domestic,CreditCard,14
Advanced,12,Overseas,CashOnReceipt,16
Advanced,12,Overseas,CreditCard,12
Advanced,24,Domestic,CashOnReceipt,23
Advanced,24,Domestic,CreditCard,19
Advanced,24,Overseas,CashOnReceipt,21
Advanced,24,Overseas,CreditCard,17
Once the test case data has been created, we store it in a csv file, say, values.csv

We now read the csv file using _readCSVFile and iterate over the data using a for loop.
var $data = _readCSVFile("../scripts/td/values.csv"); // the file is kept in the scripts/td folder
for (var $i=0; $i<$data.length; $i++){
	var $row = $data[$i];
	checkDiscount($row[0], $row[1], $row[2], $row[3], $row[4]);
}
The full script now becomes:
function checkDiscount($guitarType, $quantity, $destination, $paymentMode, $expectedDiscount){
	_setSelected(_select("typeofguitar"), $guitarType);
	_setValue(_textbox("qty"), $quantity);
	_click(_radio($destination));
	_click(_radio($paymentMode));
	_click(_button("Click for discount >"));
	_assertContainsText($expectedDiscount, _span("discount"));
}

var $data = _readCSVFile("../scripts/td/values.csv");
for (var $i=0; $i<$data.length; $i++){
	var $row = $data[$i];
	checkDiscount($row[0], $row[1], $row[2], $row[3], $row[4]);
}

There is a small problem in this script. If there is a problem in the execution of any particular row of data, the script will fail and not proceed to the next row.
We fix this by using a try catch block and come up with this final script.
function checkDiscount($guitarType, $quantity, $destination, $paymentMode, $expectedDiscount){
    try{
        _setSelected(_select("typeofguitar"), $guitarType);
        _setValue(_textbox("qty"), $quantity);
        _click(_radio($destination));
        _click(_radio($paymentMode));
        _click(_button("Click for discount >"));
        _assertContainsText($expectedDiscount, _span("discount"));
    }catch(e){
        // The try catch block helps in continuing with the next step of data inspite of failures.
        // _logException will log the exception so that the logs will indicate the failing line.
        _logException(e);
    }
}

var $data = _readCSVFile("../scripts/td/values.csv");
for (var $i=0; $i<$data.length; $i++){
	var $row = $data[$i];
	checkDiscount($row[0], $row[1], $row[2], $row[3], $row[4]);
}


Conclusion:
We saw how to write a data driven test using a csv file in Sahi.
We also explored how using online tools like TestersDesk.com can make life easier for a tester.
Note: In cases where there are many values for each test parameter, and the number of combinations explode into a huge number, we will use a different tool provided by TestersDesk.com, namely Pairwise TestCase Generator. We will explore it in a different article.
You can contact Ashwin Palaparthi at ap@testersdesk.com for any queries you may have about the online toolkit they provide to the testing community.


Thanks to Ashwin for his valuable inputs, and for creating such a useful online tool.