从excel读取值时获取空指针异常

时间:2017-02-04 15:49:53

标签: selenium selenium-webdriver nullpointerexception

正确阅读Excel数据:

  

错误:信息@ gmail.com,1234444       INFO1 @ gmail.com,jjkkll ;; JH       [Utils]试图创建C:\ Users \ priya \ workspace \ seleniumtopics \ test-output \ Default   suite \ Default test.xml       [Utils]目录C:\ Users \ priya \ workspace \ seleniumtopics \ test-output \ Default suite   存在:真实       FAILED:inputdata(" info@gmail.com" ;," 1234444")       显示java.lang.NullPointerException       包pckg2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataInputfromExcel {
    public WebDriver driver;
    public WebDriverWait wait;
    @BeforeClass
    public void setup(){
        System.setProperty("webdriver.firefox.marionette","D:\\desktop\\Selenium\\geckodriver-v0.9.0-arm7hf\\geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://mail.google.com");
         wait = new WebDriverWait(driver,20);
        //wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#account-chooser-add-account"))));
        //driver.findElement(By.cssSelector("#account-chooser-add-account")).click();
        }
    @Test(dataProvider="login")
    public void inputdata(String username,String password){
        System.out.println(username +","+password);
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Email"))));
        driver.findElement(By.cssSelector("#Email")).sendKeys(username);
        driver.findElement(By.cssSelector("#next")).click();
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Passwd"))));
        driver.findElement(By.cssSelector("#Passwd")).sendKeys(password);
        driver.findElement(By.cssSelector("#next")).click();
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#errormsg_0_Passwd"))));
        String errortext=driver.findElement(By.cssSelector("#errormsg_0_Passwd")).getText();
        Assert.assertEquals(errortext,"Wrong password. Try again.");
    }

    @DataProvider(name="login")
    public Object[][] logindata() throws IOException{
        Object[][] arraydata=getexceldata("C:/Users/priya/Desktop/automationTopics.xlsx","Sheet3");
        return arraydata;

    }
    private String[][] getexceldata(String excelpath, String sheetname) throws IOException {
        String[][] exceldata=null;
        try {
            FileInputStream fs = new FileInputStream(excelpath);
            XSSFWorkbook wb = new XSSFWorkbook(fs);
            XSSFSheet sheet= wb.getSheet(sheetname);
            int rowcount=sheet.getLastRowNum()-sheet.getFirstRowNum();
            int colcount=sheet.getRow(0).getLastCellNum();
            System.out.println(rowcount +","+colcount);
            exceldata = new String[rowcount+1][colcount];
            for(int i=0;i<rowcount+1;i++){
                 Row row = sheet.getRow(i);
                for(int j=0;j<row.getLastCellNum();j++){
                    //exceldata[i][j]=sheet.getRow(i).getCell(j).getRichStringCellValue().getString();
                    DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
                     Cell cell = row.getCell(j);
                     exceldata[i][j] = formatter.formatCellValue(cell);
                }
            }


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


        return exceldata;
    }

}

1 个答案:

答案 0 :(得分:0)

您已在代码中声明了两次驱动程序,并且在调用 inputData 方法时,驱动程序对象未初始化。如果您仍然遇到同样的问题,请尝试关注并告知我们:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataInputfromExcel {
    public WebDriver driver;
    public WebDriverWait wait;
    @BeforeClass
    public void setup(){
        System.setProperty("webdriver.firefox.marionette","D:\\desktop\\Selenium\\geckodriver-v0.9.0-arm7hf\\geckodriver");
        driver = new FirefoxDriver();
        driver.get("https://mail.google.com");
         wait = new WebDriverWait(driver,20);
        //wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#account-chooser-add-account"))));
        //driver.findElement(By.cssSelector("#account-chooser-add-account")).click();
        }
    @Test(dataProvider="login")
    public void inputdata(String username,String password){
        System.out.println(username +","+password);
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Email"))));
        driver.findElement(By.cssSelector("#Email")).sendKeys(username);
        driver.findElement(By.cssSelector("#next")).click();
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Passwd"))));
        driver.findElement(By.cssSelector("#Passwd")).sendKeys(password);
        driver.findElement(By.cssSelector("#next")).click();
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#errormsg_0_Passwd"))));
        String errortext=driver.findElement(By.cssSelector("#errormsg_0_Passwd")).getText();
        Assert.assertEquals(errortext,"Wrong password. Try again.");
    }

    @DataProvider(name="login")
    public Object[][] logindata() throws IOException{
        Object[][] arraydata=getexceldata("C:/Users/priya/Desktop/automationTopics.xlsx","Sheet3");
        return arraydata;

    }
    private String[][] getexceldata(String excelpath, String sheetname) throws IOException {
        String[][] exceldata=null;
        try {
            FileInputStream fs = new FileInputStream(excelpath);
            XSSFWorkbook wb = new XSSFWorkbook(fs);
            XSSFSheet sheet= wb.getSheet(sheetname);
            int rowcount=sheet.getLastRowNum()-sheet.getFirstRowNum();
            int colcount=sheet.getRow(0).getLastCellNum();
            System.out.println(rowcount +","+colcount);
            exceldata = new String[rowcount+1][colcount];
            for(int i=0;i<rowcount+1;i++){
                 Row row = sheet.getRow(i);
                for(int j=0;j<row.getLastCellNum();j++){
                    //exceldata[i][j]=sheet.getRow(i).getCell(j).getRichStringCellValue().getString();
                    DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
                     Cell cell = row.getCell(j);
                     exceldata[i][j] = formatter.formatCellValue(cell);
                }
            }


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


        return exceldata;
    }

}

在上面的代码中,我已经从 setup 方法中删除了驱动程序的重新声明,因为它已经被声明为类变量。此外,在现有代码中,设置方法的驱动程序变量仅在其方法中具有可见性,即设置。如果您有任何疑问,请与我们联系。