Data Driven testing using excel sheet for the content on different web pages using JAVA in Selenium WebDriver

4.7k Views Asked by At

I have a requirement to test the large list of webpages for specific website and have to verify i) if the content on all provided webpages is present or not? ii) the content is neither duplicated as well on that particular page.

I need to automate this using Selenium WebDriver (Java). I want that I just provide all the pages URL into an excel sheet (.csv file) and just run the test through it and get back the results for my requirements.

Please help me in this.

Thanks in advance..

1

There are 1 best solutions below

0
On

Maybe you can try with jxl libraries.

With Maven, that is the pom dependency:

<dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>

In case of read username, password, ... you can use a bean like this:

import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 * 
 * This is a bean to easily get users data from an excel file.
 * 
 * @author Hector Flores - [email protected]
 * 
 */
public class ExcelUserBean {

private String filepath = null;
private Sheet sheet = null;

/**
 * Read an excel file with users
 * 
 * @param filepath
 * @throws IOException
 */
public ExcelUserBean(String filepath) throws IOException {
    this.filepath = filepath;
    // TestData td = new TestData("SignUpUsers.xls");
    getDatafromXL(this.filepath);
}

/**
 * Get ID counter from excel file
 * 
 * @param row
 *            (0 for column title, >0 for values)
 * @return
 */
public String getId(int row) {
    return sheet.getCell(0, row).getContents();
}

/**
 * Get email form excel file
 * 
 * @param row
 *            (0 for column title, >0 for values)
 * @return
 */
public String getEmail(int row) {
    return sheet.getCell(1, row).getContents();
}

/**
 * Get name form excel file
 * 
 * @param row
 *            (0 for column title, >0 for values)
 * @return
 */
public String getName(int row) {
    return sheet.getCell(2, row).getContents();
}

private void getDatafromXL(String filepath) throws IOException {
    File inputWorkbook = new File(filepath);
    Workbook w;
    try {
        w = Workbook.getWorkbook(inputWorkbook);
        // Get the first sheet
        sheet = w.getSheet(0);

    } catch (BiffException e) {
        e.printStackTrace();
    }
    }// End parseXLTestCase

}

Now in your test cases you can fill your bean

    ExcelUserBean userBean = new ExcelUserBean(FILEPATH);
    //ROWS
    int begin = PropertiesConfig.getUserExcelRowFrom(); <-- from
    int end = PropertiesConfig.getUserExcelRowTo();   <-- to

    for (int excelRow = begin; excelRow <= end; excelRow++) {

       driver.findElement(By.id("signfirstname")).sendKeys(userBean.getName(excelRow));
       driver.findElement(By.id("lastname")).sendKeys(userBean.getSurname(excelRow));
    }

Hope it helps!