完成所有指令后跳过Selenium webdriver测试,并给出java.lang.NullPointerException的错误,我该如何解决?

Skipped Selenium webdriver test after completing all the instructions, and give error of java.lang.NullPointerException, How can i solve it?

package defaultTest;

import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFRow;
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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;

public class NewTest{
    static WebDriver driver;
    XSSFWorkbook wb;
    XSSFSheet sheet1;
    int numrow;

    @BeforeClass
    public void setUp() {
        driver = new FirefoxDriver();
        //gmail URL
        driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/");
    }

    @DataProvider(name = "testdata")
    public Object[][] dp() throws Exception {
        Object[][] tabArray = null;
        try {
            FileInputStream fis = new FileInputStream(
                "C:\Users\gaurav\workspace\Test\src\Resources\LoginDataExcelFile.xlsx");
            wb = new XSSFWorkbook(fis);
            sheet1 = wb.getSheet("TestSheet");

            for (int count = 0; count <= sheet1.getLastRowNum(); count++) {
                XSSFRow row = sheet1.getRow(count);
                //System.out.println("ID: "+row.getCell(0).toString()+"\t password : "+row.getCell(1).toString());
                testLogin(row.getCell(0).toString(), row.getCell(1).toString());
            }
            fis.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return tabArray;
    }

    @Test(dataProvider = "testdata")
    public void testLogin(String UserName, String Password)throws Exception {

        driver.findElement(By.id("Email")).clear();
        Thread.sleep(2000);
        driver.findElement(By.id("Email")).sendKeys(UserName);
        Thread.sleep(2000);
        driver.findElement(By.id("Passwd")).clear();
        Thread.sleep(2000);
        driver.findElement(By.id("Passwd")).sendKeys(Password);
        Thread.sleep(2000);
        driver.findElement(By.name("signIn")).click();
        Thread.sleep(20000);

        WebElement name = driver
    .findElement(By
            .xpath(".//*[@id=':hw']/div/div"));
        try {
            Assert.assertEquals("COMPOSE", name.getText());//Used some assertion
            System.out.println(name.getText());
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    @AfterMethod
    public void afterMethod() {
        driver.close();
        driver.quit();
    }
}

这是一个测试应用程序。在运行测试之前,确保excel文件只有loginID和password,没有任何header.
问题是,我在 excel 文件中只有一条记录,我的上述脚本成功运行一次,但是当 for 循环再次运行时,它在 excel 中找不到任何记录,我的测试被跳过,因此另一个测试失败了。

> [TestNG] Running:  
> C:\Users\gaurav\AppData\Local\Temp\testng-eclipse-1831957551\testng-customsuite.xml
> 
> COMPOSE
> 
> SKIPPED: testLogin java.lang.NullPointerException     at
> org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:149)
>   at
> org.testng.internal.Parameters.handleParameters(Parameters.java:429)
>   at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
>   at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
>   at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
>   at
> org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
>   at
> org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
>   at org.testng.TestRunner.privateRun(TestRunner.java:767)    at
> org.testng.TestRunner.run(TestRunner.java:617)    at
> org.testng.SuiteRunner.runTest(SuiteRunner.java:334)  at
> org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)  at
> org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)   at
> org.testng.SuiteRunner.run(SuiteRunner.java:240)  at
> org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)  at
> org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)   at
> org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)     at
> org.testng.TestNG.runSuitesLocally(TestNG.java:1149)  at
> org.testng.TestNG.run(TestNG.java:1057)   at
> org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)     at
> org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)  at
> org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
> 
> 
> ===============================================
>     Default test
>     Tests run: 1, Failures: 0, Skips: 1
> ===============================================

将@AfterMethod 更改为@AterClass 或将@BeforeClass 更改为@BeforeMethod

附带说明一下,我会更改 --> 静态 WebDriver 驱动程序;私有 WebDriver 驱动程序;

您的数据提供者 returns 空值。您需要编写代码以通过 tabArray 传递值。

您需要在读取 excel 后将数据放入您的 tabArray 中。您无需在数据提供者方法中调用 testLogin 方法,这会破坏使用 DataProvider 的全部目的。

阅读一些关于数据提供者的内容@ http://testng.org/doc/documentation-main.html#parameters-dataproviders

在您的 for 循环中,将对 testLogin 方法的调用替换为将数据放入您的 tabArray。 例如。

tabArray[count] =  new Object[]{row.getCell(0).toString(), row.getCell(1).toString()};