0% found this document useful (0 votes)
892 views9 pages

Data Driven Test Using Selenium

TestNG is a testing framework that enables data-driven testing in Selenium. It allows test methods to be executed multiple times with different data sets via its DataProvider feature. A DataProvider returns data to a test method annotated with the @Test annotation. In the example, a DataProvider method fetches test data from an Excel sheet and returns it as an array to be used across multiple executions of the testEmployeeData method.

Uploaded by

Thiru Rao
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
892 views9 pages

Data Driven Test Using Selenium

TestNG is a testing framework that enables data-driven testing in Selenium. It allows test methods to be executed multiple times with different data sets via its DataProvider feature. A DataProvider returns data to a test method annotated with the @Test annotation. In the example, a DataProvider method fetches test data from an Excel sheet and returns it as an array to be used across multiple executions of the testEmployeeData method.

Uploaded by

Thiru Rao
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Data-Driven testing generally means executing a set of steps with multiple sets of data.

Selenium does not provide any out-of-the box solution for data driven testing but leaves it up to the user to implement this on his own. People who are familiar with QuickTest Professional or QTP would know Datatables (Global & Action) that allows the scripter to link a action or a function with multiple rows of data.

TestNG is a framework that makes data-driven testing possible in selenium. TestNG is a testing framework created in line with Junit but with added features that makes it suitable for use in regression test automation projects. DataProvider is one such feature in testng; it allows a test method to be executed with multiple sets of data.

A DataProvider is data feeder method defined in your class that supplies a test method with data. You may hook up any test method with a DataProvider and make the test method execute once for every row of your test data. @DataProvider(name = "DP1") public Object[][] createData() { Object[][] retObjArr={{"001","Jack","London"}, {"002","John","New York"}, {"003","Mary","Miami"}, {"004","George","california"}}; return(retObjArr); }

@Test (dataProvider = "DP1") public void testEmployeeData(String empid, String empName, String city){ selenium.type("id", empid); selenium.type("name", empName); selenium.click("submit_button"); selenium.waitForPageToLoad("30000"); assertTrue(selenium.isTextPresent(city)); }

In the example above createData() is the DataProvider function that is sending out test data in the form of array of array of objects (object[][]). The test method testEmployeeData() hooks onto the DataProvider by declaring that its data should be supplied by the DataProvider named "DP1". On execution testEmployeeData() will be executed 4 times, once with each row of object[][]. Note that the dimension of the array returned by the DataProvider must match the parameter type of the test method. This means if your test method takes 3 parameters then array returned by the DataProvider must have its second dimension equal to 3. Try to visualize the array of array as a table where each row constitutes one test case.

In the example above the test data is defined withing the DataProvider function. But ideally you want to keep your test data in spreadsheet like media and not in the test script. To achieve this instead of declaring the test data array within the DataProvider function we should call a function that fetches the data from an excel sheet and returns an array of array object to the DataProvider. I will share with you my way of storing data in XL sheets. I store test data in the form of tables within an excel sheet. There could be any number of tables within an excel sheet. Each table is marked by an startTag and an endTag. The startTag and endTag is the Name of the table. The startTag is placed one cell diagonally above the cell from where the first row of table data begins. And the endTag is placed one cell diagonally below the cell from where last row of table data ends. A table always begins with a column header row although this row is just for our understanding and is ignored in the computation. Below is the image of a sample test data excel sheet. Click on the image to see a bigger version of it. Also below the image you can see the google doc with the test data I will use in an example later in this post.

Below is the function that fetches the table from the excel sheet and returns it as an array of array of String type. It uses Java Excel API to fetch data from excel sheet. It takes 3 parameters 1. xlFilePath - the path of XL file/workbook containing the data, the path is relative to java project 2. sheetName- name of the xl sheet that contains the table 3. tableName- Name of the table that you wish to fetch The function returns a String type array of array.

public String[][] getTableArray(String xlFilePath, String sheetName, String tableName){ String[][] tabArray=null; try{ Workbook workbook = Workbook.getWorkbook(new File(xlFilePath)); Sheet sheet = workbook.getSheet(sheetName); int startRow,startCol, endRow, endCol,ci,cj; Cell tableStart=sheet.findCell(tableName); startRow=tableStart.getRow(); startCol=tableStart.getColumn(); Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000, false);

endRow=tableEnd.getRow(); endCol=tableEnd.getColumn(); System.out.println("startRow="+startRow+", endRow="+endRow+", " + "startCol="+startCol+", endCol="+endCol); tabArray=new String[endRow-startRow-1][endCol-startCol-1]; ci=0; for (int i=startRow+1;i<endRow;i++,ci++){ cj=0; for (int j=startCol+1;j<endCol;j++,cj++){ tabArray[ci][cj]=sheet.getCell(j,i).getContents(); } } } catch (Exception e) { System.out.println("error in getTableArray()"); } return(tabArray); }

Now we will put all of this together in an example. We will keep the test data in excel sheets and use the above function to fetch from it. Note: 1. testng jar should be in the class path. You may download the latest testng jar file from here. 2. testng eclipse plugin must be installed. Get it from the same location as the testng jar file mentioned above. 3. jxl jar should be in the class path Java Excel API. You can get this from here. Download the jexcelapi.zip, unzip to get the jxl.jar 4. Selenium-server and selenium-java-client-driver and junit jar (selenium classes need junit) files must in class path. 5. The file data1.xls is kept at test\\Resources\\Data\\data1.xls I have attached this file to the blog as a google doc, please copy paste its contents to a excel file. Rename the excel-sheet to DataPool and the save the excel file as data.xls. The path mentioned is relative to the project. The test method testDataProviderExample() below navigates to www.imdb.com and performs search for movie titles. On the movie details page it verifies the presence of director name, movie plot and the name of the lead actor. This routine is repeated for all the rows of the test-data table.

import com.thoughtworks.selenium.*; import org.junit.AfterClass; import org.openqa.selenium.server.SeleniumServer; import org.testng.annotations.*; import java.io.File; import jxl.*; public class DataProviderExample extends SeleneseTestCase{

@BeforeClass public void setUp() throws Exception { SeleniumServer seleniumserver=new SeleniumServer(); seleniumserver.boot(); seleniumserver.start(); setUp("http://www.imdb.com/", "*firefox"); selenium.open("/"); selenium.windowMaximize(); selenium.windowFocus(); } @DataProvider(name = "DP1") public Object[][] createData1() throws Exception{ Object[][] retObjArr=getTableArray("test\\Resources\\Data\\data1.xls", "DataPool", "imdbTestData1"); return(retObjArr); } @Test (dataProvider = "DP1") public void testDataProviderExample(String movieTitle, String directorName, String moviePlot, String actorName) throws Exception { //enter the movie title selenium.type("q", movieTitle); //they keep switching the go button to keep the bots away if (selenium.isElementPresent("nb15go_image")) selenium.click("nb15go_image"); else selenium.click("xpath=/descendant::button[@type='submit']"); selenium.waitForPageToLoad("30000"); //click on the movie title in the search result page selenium.click("xpath=/descendant::a[text()='"+movieTitle+"']"); selenium.waitForPageToLoad("30000"); //verify director name is present in the movie details page verifyTrue(selenium.isTextPresent(directorName)); //verify movie plot is present in the movie details page verifyTrue(selenium.isTextPresent(moviePlot)); //verify movie actor name is present in the movie details page verifyTrue(selenium.isTextPresent(actorName)); } @AfterClass public void tearDown(){ selenium.close(); selenium.stop(); } public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{ String[][] tabArray=null; Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));

Sheet sheet = workbook.getSheet(sheetName); int startRow,startCol, endRow, endCol,ci,cj; Cell tableStart=sheet.findCell(tableName); startRow=tableStart.getRow(); startCol=tableStart.getColumn(); Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000, false); endRow=tableEnd.getRow(); endCol=tableEnd.getColumn(); System.out.println("startRow="+startRow+", endRow="+endRow+", " + "startCol="+startCol+", endCol="+endCol); tabArray=new String[endRow-startRow-1][endCol-startCol-1]; ci=0; for (int i=startRow+1;i<endRow;i++,ci++){ cj=0; for (int j=startCol+1;j<endCol;j++,cj++){ tabArray[ci][cj]=sheet.getCell(j,i).getContents(); } } return(tabArray); } }//end of class

Just copy paste the above in a java file, reference the necessary jars in the build path, put the test-data excel file in the correct location and it should work just fine. To run right click the java file in eclipse and selectRun As-->TestNG Test. Watch video of how to create a data driven test script from scratch step by step

======================================================================

Using Selenium-IDE to Generate the Data for a DataDriven Test


By Mary Ann May-Pumphrey

In January, I made a post on the Sauce Labs blog about how to use Selenium-IDE to do data-driven testing. In that post, I raised the issue of what one might do if faced with building a test for a lengthy list of links. The data-driven test case would be quite similar to the one I included in my post, but producing the test data for the XML file would be a big pain. One solution is to use Selenium-IDE to generate the data. Below is an example that generates the XML needed for data-driven testing of the options in the Foothills College Quick Links drop-down. The original version of this testdata-generator program was written at my behest by two former students from my Selenium-IDE classLini Kuriyan & Archana Sajith. Back then, there was no datadriven.js extension for Selenium-IDE , so their version of the program generated an extension file with the test data stored in a JavaScript array. My update below generates the test data in XML format, ready for use with the datadriven.js extension. Heres how to see this program in action: 1. Install the File Logging and Flow Control extensions for Selenium-IDE from the Download page. 2. Enable File Logging. 3. Create an HTML test like this one within Selenium-IDE:

open

http://www.foothill.edu

store

//select[@name='topnav']

selectLocator

waitForElementPresent ${selectLocator}

storeSelectOptions

${selectLocator}

options

runScript

javascript{storedVars['options'].splice(0,2)}

storeEval

storedVars['options'].length

totalOptions

store

index

store

<testdata>

xmlCode

while

(${index}<${totalOptions})

storeEval

storedVars['options'][storedVars['index']]

option

selectAndWait

${selectLocator}

label=${option}

waitForPageToLoad

30000

storeTitle

pageTitle

storeEval

storedVars['xmlCode'] + <test optionLabel= xmlCode

storeEval

storedVars['xmlCode'] + storedVars['option']

xmlCode

storeEval

storedVars['xmlCode'] + title=

xmlCode

storeEval

storedVars['xmlCode'] + storedVars['pageTitle'] xmlCode

storeEval

storedVars['xmlCode'] + />

xmlCode

goBackAndWait

storeEval

${index}+1

index

endWhile

runScript

javascript{storedVars['xmlCode'] += </testdata>;}

echo

${xmlCode}

This script uses storeSelectOptions to store all the option labels from the Foothills Quick Links drop-down into a variable named options. The first two (irrelevant) options are then removed from options via the runScript call to JavaScripts splice method. totalOptions and index are the two variables used to control the while loop that makes up the bulk of the script. The loop begins by retrieving one of the stored options values, and then using it with selectAndWait to mimic a user selecting that drop-down option. Once the target page has loaded, storeTitle is used to store the target pages title in pageTitle. At this point in the code, five rather ugly calls to storeEval take care of actually creating a single line of test data in the eventual XML file, via concatenation onto xmlCode. 4. Execute the script within Selenium-IDE. 5. Use an editor on your logging file to remove everything prior to the opening <testdata> container. 6. Convert all occurrences of an ampersand into its character entity &amp; to conform to XML requirements. 7. View your former log file, now converted into an XML file, in the browser to ensure that no errors are present. (This step is only necessary if you havent used an XML editor.)

8. Create a data-driven test case similar to that I showed in my January post, to utilize the generated test data. When using a Selenium app like this one to generate test data, it is critical to remember to review the generated data, to make sure no bugs are revealed! For example, a line like this one in the generated data
<test optionLabel="Classified Senate" title="Campus Clubs"/>

clearly indicates a bug with the option for Classified Senate, and should be reported via the defect-tracking system. The XML file would also need to be modified to show the correct value for the title attribute in the line above. P. S. If you want to run my code above, copy/paste the contents of this .doc file into Selenium-IDEs Source tab, inside the tbody container.

============================

http://testerinyou.blogspot.com/2011/03/how-to-create-reports-using-junit.html

===================== Selenium documentation sites http://seleniumhq.org/docs/06_test_design_considerations.html

http://www.wakaleo.com/component/content/article/241 http://www.testingexcellence.com/how-to-start-selenium-server-with-java-code/

You might also like