读取Excel数据并在selenium中使用它

时间:2017-05-29 10:18:37

标签: selenium-webdriver

此脚本用于从excel读取数据并在selenium脚本中使用它。这使用Apache POI读取数据,将其存储在变量中并使用它。

3 个答案:

答案 0 :(得分:1)

/*
 * Download Apache POI from https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.16-20170419.zip
 * 
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class FormFill {
    public static void main(String[] args) throws Exception {



        try {
            FileInputStream fileInputStream = new FileInputStream("C:\\data.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet worksheet = workbook.getSheet("sheet1");
            HSSFRow row1 = worksheet.getRow(0);
            HSSFCell cellA1 = row1.getCell((short) 0);
            String a1Val = cellA1.getStringCellValue();
            HSSFCell cellB1 = row1.getCell((short) 1);
            String b1Val = cellB1.getStringCellValue();

            System.out.println("A1: " + a1Val);
            System.out.println("B1: " + b1Val);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        String url = "http://thedemosite.co.uk/addauser.php";
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get(url);
        //Thread.sleep(30000);
        driver.findElement(By.name("username")).sendKeys(a1Val);
        driver.findElement(By.name("password")).sendKeys(b1Val);


    }

}

答案 1 :(得分:1)

您可以尝试以下代码。我把facebook作为示例应用程序。

 package testPackage;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.concurrent.TimeUnit;

 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.chrome.ChromeDriver;

 import org.testng.annotations.*;
 import org.testng.annotations.DataProvider;

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

 public class ExcelReadingwithDP {

 WebDriver driver;
 @BeforeTest
 public void OpenApp()
 {
    System.setProperty("webdriver.chrome.driver",  "E:/Selenium/Webdriver/Softwares/chromedriver.exe");
    driver = new ChromeDriver();
    driver.navigate().to("http://facebook.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }


 @Test(dataProvider="empLogin")
 public void login(String username, String password)
 {
     WebElement login1 = driver.findElement(By.id("email"));
     login1.clear();
    login1.sendKeys(username);
    WebElement passwd=driver.findElement(By.id("pass"));
    passwd.clear();
    passwd.sendKeys(password);
    driver.findElement(By.xpath("//*[@id='u_0_q']")).click();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    WebElement back = driver.findElement(By.xpath("//*[@id='blueBarDOMInspector']/div/div[1]/div/div/h1/a/i"));
    back.click();
 }


 @DataProvider(name="empLogin")
  public Object[][] logindata()
 {
     Object[][] arrayobject = getexceldata("E://Deepak/IntranetLogin.xls","Sheet1");
     return arrayobject;
 }

public String[][] getexceldata(String filename, String sheetname)
{

    String[][] arrayexceldata = null;
    try
    {
    FileInputStream fis = new FileInputStream(filename);
    Workbook wb = Workbook.getWorkbook(fis);
    Sheet sh = wb.getSheet(sheetname);
    int row = sh.getRows();
    int col = sh.getColumns();
    arrayexceldata = new String[row-1][col];
    for (int i=1;i< row;i++)
    {
        for(int j=0;j<col;j++)
        {
            arrayexceldata[i-1][j]=sh.getCell(j,i).getContents();
        }
    }

    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        e.printStackTrace();
    } catch (BiffException e) {
        e.printStackTrace();
    }
    return arrayexceldata;

}

}

答案 2 :(得分:-1)

我觉得csv比Excel好,因为Excel有更多时间来读取数据,但csv很快。

private static final String CSV_SEPARATOR =&#34;,(?=(?:[^ \&#34;] \&#34; [^ \&#34;] \& #34) [^ \&#34;] $)&#34 ;;

public List<String[]> parseFile(String fileName) {
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        br.readLine(); // skip header;
        String line = br.readLine();
        List<String[]> lines = new ArrayList();
        while (line != null) {
            //System.out.println(line.toString());
            lines.add(line.split(CSV_SEPARATOR));
            line = br.readLine();
        }
        return lines;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}