An Example of Automated Test DesignIntegrating Data-driven Testing in Sahi with N-way/All Combinations (Decision Table) generated by TestersDesk.comIntroduction: 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). |
| 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% |
_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.
TypeOfGuitar:Basic,BasicWithKit,Advanced Qty:12,24 Location:Domestic,Overseas PaymentMode:CashOnReceipt,CreditCardNote 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,CreditCardNow, 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
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]);
}
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]);
}