Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium webdriver 读取Excel数据并在selenium中使用_Selenium Webdriver - Fatal编程技术网

Selenium webdriver 读取Excel数据并在selenium中使用

Selenium webdriver 读取Excel数据并在selenium中使用,selenium-webdriver,Selenium Webdriver,此脚本用于从excel读取数据并在selenium脚本中使用。这使用ApachePOI读取数据,将其存储在变量中并使用它 我觉得csv比Excel好,因为Excel有更多的时间读取数据,但csv速度很快 /* * Download Apache POI from https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.16-20170419.zip * */ import java.io.FileInputStre

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

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

/*
 * 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);


    }

}
私有静态最终字符串CSV_SEPARATOR=“,(?=(?:[^\“]\”[^\“]\”[^\“]\”[^\“]$)”

公共列表解析文件(字符串文件名){
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(文件名));
br.readLine();//跳过标题;
String line=br.readLine();
列表行=新的ArrayList();
while(行!=null){
//System.out.println(line.toString());
行。添加(行。拆分(CSV_分隔符));
line=br.readLine();
}
回流线;
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}

您可以尝试以下代码。我已将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;

}
包testPackage;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.testng.annotations.*;
导入org.testng.annotations.DataProvider;
进口jxl.Sheet;
导入jxl.工作簿;
导入jxl.read.biff.BiffException;
公共课以DP成绩优异{
网络驱动程序;
@试验前
public void OpenApp()
{
System.setProperty(“webdriver.chrome.driver”,“E:/Selenium/webdriver/Softwares/chromedriver.exe”);
驱动程序=新的ChromeDriver();
驱动程序。导航()。到(“http://facebook.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
@测试(dataProvider=“empLogin”)
公共无效登录(字符串用户名、字符串密码)
{
WebElement login1=driver.findElement(By.id(“email”);
login1.clear();
login1.sendKeys(用户名);
WebElement passwd=driver.findElement(By.id(“pass”);
passwd.clear();
passwd.sendKeys(密码);
findElement(By.xpath(“/*[@id='u_0_q']))。单击();
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();
}
@数据提供者(name=“empLogin”)
公共对象[][]登录数据()
{
对象[][]arrayobject=getexceldata(“E://Deepak/IntranetLogin.xls”,“Sheet1”);
返回arrayobject;
}
公共字符串[][]getexceldata(字符串文件名、字符串名称)
{
字符串[][]arrayexceldata=null;
尝试
{
FileInputStream fis=新的FileInputStream(文件名);
工作簿wb=工作簿.get工作簿(fis);
Sheet sh=wb.getSheet(sheetname);
int row=sh.getRows();
int col=sh.getColumns();
arrayexceldata=新字符串[row-1][col];
对于(int i=1;i对于(int j=0;问题)?@ MasStutaChanaAgAjaAn你能考虑用你的工作、研究、相关HTML DOM和错误堆栈跟踪来更新问题区域吗?
 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;

}