Simple Data Driven Framework script
Why use Data driven Framework?
We go for data driven framework, to test application behavior
for different sets of data. So in this, script remains the same only the data
on which the script will work changes as manual testing scenarios. One basic
example would be to test login functionality.
Typical test case for login functionality have the test
cases wherein only data changes are present and basic steps remain the same.
Example
basic login test cases:
- Verify that for valid credentials, user is granted access to system.
- Verify that for invalid credentials, user is denied access to system.
- Verify that after successful login user is presented with correct landing page.
Basic steps in all above test cases would as
given below:
- Navigate to Application Under Test URL
- Enter user name
- Enter password
- Click on Login/Submit button
- Verify result
When above scenario is to be automated then the best
solution is to implement it using Data Driven Framework.
Simple Data Driven Framework script:
Let’s see a simple data driven framework script to test
login functionality.
Basic steps:
- Create a excel sheet having data for each test case. It can be as given below, save it with name DataDriver1.xls
- Create a TestNG class in eclipse
-
While creating TestNG class, select these annotations - @BeforeMethod, @AfterMethod and @DataProvider
-
Code for the script would as given below – You can use Arrow keys from keyboard for horizontal/vertical scrolling of code below. Complete code is available at my GitHub Project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package TestCases; import org.testng.annotations.Test; import jxl.Sheet; import jxl.Workbook; import java.io.File; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.DataProvider; public class DataDrivenTest { WebDriver driver = null; @Test(dataProvider = "dp") public void f(String userName, String password) { driver.findElement(By.xpath(".//*[@name = 'userName']")).sendKeys(userName); driver.findElement(By.xpath(".//*[@name = 'password']")).sendKeys(password); driver.findElement(By.xpath(".//*[@name='login']")).click(); //Verify correct landing page is presented to user after successful login. Assert.assertEquals(driver.getCurrentUrl(), "http://newtours.demoaut.com/mercuryreservation.php"); } @BeforeMethod public void beforeMethod() { driver = new FirefoxDriver(); driver.get("http://newtours.demoaut.com/"); driver.manage().window().maximize(); } @AfterMethod public void afterMethod() { driver.quit(); } @DataProvider public Object[][] dp() { Object[][] o = null; try { Workbook wb = Workbook.getWorkbook(new File( "D:\\path\\to\\excel\\sheet\\DataDriver1.xls")); Sheet sh = wb.getSheet(0); // zero selects first worksheet from the excelsheet. o = new Object[sh.getRows()][sh.getColumns()]; // initialized the object with total no. of rows and columns present in the selected sheet (sheet zero). for (int i = 0; i < sh.getRows(); i++) { // loop for rows. for (int j = 0; j < sh.getColumns(); j++) { // loop for cols. o[i][j] = sh.getCell(j, i).getContents(); // getcell function first argument is columns and then rows. // In array we have first row and then column.that is why reverse of j and i is present. } } } catch (Exception e) { System.out.println(e.getMessage()); } return o; } }
Output:
Once you run the script, it open Firefox browser and navigate to Mercury Travels site and run the test cases as many times there is data present in the excel sheet.
Script Code Explanation:
Lets understand logic implemented in dp() function. It uses JXL API to read data from excel sheet.
First we create a Workbook instance and instantiate it by giving location of excel sheet - DataDriver1.xls.
Workbook wb = Workbook.getWorkbook(new File( "D:\\path\\to\\excel\\sheet\\DataDriver1.xls"));
Then we create object of Sheet class to access particular sheet within the DataDriver1.xls
Sheet sh = wb.getSheet(0);
The dp() function returns String array, so we instantiate the String array with total number of rows and columns that have data in the excel sheet.
o = new Object[sh.getRows()][sh.getColumns()];
Then two nested for loops are used to push excel sheet cell data into String array for all rows and columns present in the excel sheet.
Notes:
One short coming present the JXL API that it can only read/write data into excel sheet which have .xls extension. The .xlsx extension is not supported by JXL API yet.
Please let me know your feedback on this post and also let me know if you have any doubts/questions about the topic.
Enjoy and Happy Testing!!
Good post....thanks!
ReplyDeleteYour website is really interesting. I would love to see more updates.
ReplyDeleteSelenium Training in Chennai
Selenium Training
It is very good blog for Selenium testing tool. The blog will useful to improve my knowledge about testing. Thanks for sharing this valuable blog.
ReplyDeleteSelenium Training | Selenium Course in Chennai | Selenium Training Institute in Chennai
I had got a clear idea of what the blog ia all about. Kindly continue doing more.
ReplyDeleteSpoken English Classes in Perambur
Spoken English Classes in Anna Nagar West
Spoken English in Porur
Spoken English Classes in Chennai Poonamallee
Spoken English Class in Kodambakkam
Spoken English in Valasaravakkam
Spoken English Class in Triplicane
Spoken English in Adyar
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete