Reference URL:
For testng Documentation please click on the link Testng Documentation
For Order of Testng Annotations please click on the link http://total-qa.com/testng-introduction/order-of-testng-annotations/
@DataProvider
Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
Data Driven Framework
This Framework is one of the Automation Framework useful to execute the test cases using multiple sets of data.
Reference URL for Automation Frameworks: Automation Frameworks
Testng Feature DataProvider – Data Driven Framework
Steps to achieve :
a. Store the input data in an xls file.
b. Read the data from the xls using an third party jar jxl2.6.jar
- jxl download link ->http://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl/2.6.12
- jxl stands for -> java excel library
c. Add the jar to the build path.
- Workbook contains 3 sheets. They are referred by index / sheet name.
- Sheet contains rows and columns.
- We call the data is stored in the excel as cell
- Store the data into the 2-D array.
d. Store the data in @Dataprovider
e. Use the @Dataprovider in the @Test methods.
JExcel API ->http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/
Testng Feature DataProvider – Data Driven Framework
Utility Class for Storing Re-Usable Methods
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 |
package org.retailmenot.lib; import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class Utility { public static String[][] readXlsFile(String fileName) throws BiffException, IOException { //Creating a new file File srcFile = new File(fileName); System.out.println(srcFile.getAbsolutePath()); //Read the workbook in src file Workbook wb = Workbook.getWorkbook(srcFile); Sheet sheet = wb.getSheet(0); int rows = sheet.getRows(); int cols = sheet.getColumns(); //Array String data[][] = new String[rows][cols]; for(int i =0 ;i<rows;i++) { for(int j=0;j<cols;j++) { Cell cell = sheet.getCell(j,i); data[i][j] = cell.getContents(); } } return data; } } |
Testng Class for Implementing TestCases
Page Object Reference: //https://github.com/SeleniumHQ/selenium/wiki/PageObjects
Example for Testng Feature DataProvider – Data Driven Framework
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package org.retailmenot.tests; import java.io.IOException; import java.util.concurrent.TimeUnit; import jxl.read.biff.BiffException; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.retailmenot.lib.Utility; import org.retailmenot.pages.DealsPage; import org.retailmenot.pages.HomePage; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class RetailMeNotTests { WebDriver driver; String url; Logger logger; @Parameters({"url","browserType"}) @BeforeClass public void invokeBrowser(String url,String browserType) { logger = Logger.getLogger("RetailMeNotTests.class"); PropertyConfigurator.configure("Log4j.properties"); if(browserType.equals("FF")) { driver = new FirefoxDriver(); logger.info("Created ff driver instance"); } else if(browserType.equals("IE")) { System.setProperty("webdriver.ie.driver", "IEDriverServer.exe"); driver = new InternetExplorerDriver(); } else { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); } this.url = url; driver.get(url); logger.info("Opening the url " + url); driver.manage().window().maximize(); logger.info("Maximizing the window"); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); logger.info("ImplicitTimeOut"); } @DataProvider(name="DP") public String[][] feedDP() throws BiffException, IOException { String data[][] = Utility.readXlsFile("InputData.xls"); return data; } //@Parameters({"sectionName","expected"}) @Test(dataProvider="DP") public void verifySectionItemsCount(String sectionName,String expected) { driver.get(url); //Creating an object of HomePage HomePage hPage = new HomePage(driver); DealsPage dealsPage = hPage.browseProductDeals(); dealsPage.jumptoSection(sectionName); int actual = dealsPage.getSectionDealsCount(sectionName); int expectedValue = Integer.parseInt(expected); Assert.assertEquals(actual,expectedValue); } } |
Testng XML for Invoking the Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" verbose="5"> <test name="Test+IE"> <parameter name="url" value="https://www.retailmenot.com/" /> <parameter name="expected" value="60" /> <parameter name="sectionName" value="Electronics" /> <parameter name="browserType" value="CH" /> <classes> <class name="org.retailmenot.tests.RetailMeNotTests" /> </classes> </test> <!-- Test --> </suite> <!-- Suite --> |
InputData.xls
1 2 3 4 5 6 7 8 |
Electronics 60 Accessories 15 Travelk 10 Home 12 |
Page Object Class-DealsPage.java
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 |
package org.retailmenot.pages; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.UnhandledAlertException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class DealsPage { WebDriver driver; public DealsPage(WebDriver driver) { this.driver = driver; } public void jumptoSection(String sectionName) { //'"+sectionName+"' driver.findElement(By.xpath("(//a[contains(text(),'"+sectionName+"')])[2]")).click(); } public int getSectionDealsCount(String sectionName) { List<WebElement> elements=null; try{ elements = driver.findElements(By.xpath(".//*[@id='"+sectionName+"']/div/div")); } catch(UnhandledAlertException e) { if(elements==null) { elements = driver.findElements(By.xpath(".//*[@id='"+sectionName+"']/div/div")); } } return elements.size(); } } |
Page Object Class-DealsPage.java
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 |
package org.retailmenot.pages; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class HomePage { Logger logger; WebDriver driver; public HomePage(WebDriver driver) { this.driver = driver; logger = Logger.getLogger("HomePage.class"); PropertyConfigurator.configure("Log4j.properties"); } public DealsPage browseProductDeals() { driver.findElement(By.linkText("Coupons")).click(); logger.info("click on coupouns"); driver.findElement(By.linkText("Product Deals")).click(); logger.info("click on product deals link"); //navigate to new page dealsPage.. return new DealsPage(driver); } } //Whenever a method performs a functionality and navigate to new page.. |