从Selenium Java中的Excel文件读取数据 [英] Read data from Excel file in Selenium Java

查看:156
本文介绍了从Selenium Java中的Excel文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从excel表中读取数据以自动执行我的测试(具有多个登录凭据)。我正在使用我在网上找到的实用工具。但它没有成功运行。

I am trying to read data from excel sheet to automate my testing(with a number of login credentials). I am using a utility that I found on web. But it is not running successfully.

这是实用程序

    package google;

    import java.io.File;
    import java.io.IOException;
    import java.util.Hashtable; 

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

    public class class2 {

    static Sheet wrksheet;
    static Workbook wrkbook =null;
    static Hashtable dict= new Hashtable();
     //Create a Constructor
    public class2(String ExcelSheetPath) throws BiffException, IOException
    {
    //Initialize
    wrkbook = Workbook.getWorkbook(new File(ExcelSheetPath));
    //For Demo purpose the excel sheet path is hardcoded, but not recommended :)
    wrksheet = wrkbook.getSheet("Sheet1");
    }

    //Returns the Number of Rows
    public static int RowCount()
    {
    return wrksheet.getRows();
   `enter code here` }

    //Returns the Cell value by taking row and Column values as argument
    public static String ReadCell(int column,int row)
    {
    return wrksheet.getCell(column,row).getContents();
    }

    //Create Column Dictionary to hold all the Column Names
    public static void ColumnDictionary()
    {`enter code here`
    //Iterate through all the columns in the Excel sheet and store the value       
    for(int col=0; col <= wrksheet.getColumns();col++)
    {
    dict.put(ReadCell(col,0), col);
    }
    }

    //Read Column Names
    public static int GetCell(String colName)
    {
    try {
    int value;
    value = ((Integer) dict.get(colName)).intValue();
    return value;
    } catch (NullPointerException e) {
    return (0);

    }
    }

    }

以下是调用此实用程序的类。

And following is the class that calls this utility.

package google;

import java.io.IOException;

import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import google.class2;

public class class3 {

//Global initialization of Variables
static class2 xlsUtil;
WebDriver driver = new InternetExplorerDriver();

//Constructor to initialze Excel for Data source
public class3() throws BiffException, IOException
{
//Let's assume we have only one Excel File which holds all Testcases. Demo !!!
 xlsUtil = new class2("C:/Users/admin/workspace/login.xls");
 //Load the Excel Sheet Col in to Dictionary for Further use in our Test cases.
 xlsUtil.ColumnDictionary();
 }

@BeforeTest
public void EnvironmentalSetup()
{
   System.setProperty("webdriver.chrome.driver",
 "C:/Users/admin/Downloads/chromedriver.exe");
  WebDriver driver = new ChromeDriver();
   driver.get("http://192.168.1.20/dental/userlogin");
}

@Test

public void GmailLoginPage() throws InterruptedException {

//Create a for loop.. for iterate through our Excel sheet for all the test cases.
for(int rowCnt = 1;rowCnt <= xlsUtil.RowCount();rowCnt++)
{

//Enter User Name by reading data from Excel
WebElement userName = driver.findElement(By.name("UserName"));
userName.clear();
userName.sendKeys(xlsUtil.ReadCell(xlsUtil.GetCell("EmailUserName"), rowCnt));

//Enter Password
WebElement password = driver.findElement(By.name("Password"));
password.clear();
password.sendKeys(xlsUtil.ReadCell(xlsUtil.GetCell("Emailpassword"), rowCnt));

//Click on the Sign In Button
// WebElement signin = driver.findElement(By.name("signIn"));
password.submit();

//Sleep for some time,so that we can see things in action @ Screen :)
Thread.sleep(2000);
}
}

}

但是我运行不了它说不能实例化google.class3
我没有得到这里的错误。
请帮助我成功运行此代码。

But when I run dis cass it says 'cant instantiate google.class3 I don't get the mistake here. Please help me run this code successfully.

推荐答案

FileInputStream file = newFileInputStream(newFile("C:/Users/admin/workspace/login.xls"));

    //Get the workbook instance for XLS file 
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    //Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);

    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();

    while(rowIterator.hasNext()) {

        Row row = rowIterator.next();

        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {        

            Cell cell = cellIterator.next();
            if(cell.getColumnIndex() == 0){
                     driver.findElement(By.name("UserName")).sendKeys(cell.getStringCellValue());
            }
            else
                driver.findElement(By.name("Password")).sendKeys(cell.getStringCellValue());
        }

这篇关于从Selenium Java中的Excel文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆