简体   繁体   English

完成所有指令后跳过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();
    }
}

It's a test application. 这是一个测试应用程序。 Before running the test, make sure that the excel file have a loginID and password only without any header. 在运行测试之前,请确保excel文件只有登录ID和密码而没有任何标题。
Issue is, I have just one record in excel file, my above script runs once successfully, but when the for loop runs again, it finds no record in excel, and my test gets skipped, and hence the other test get failed. 问题是,我在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
> ===============================================

Change @AfterMethod to @AterClass or change @BeforeClass to @BeforeMethod 将@AfterMethod更改为@AterClass或将@BeforeClass更改为@BeforeMethod

And on a side note, I would change --> static WebDriver driver; 另外,我会改变 - >静态WebDriver驱动程序; to private WebDriver driver; 私人WebDriver驱动程序;

You data provider returns null value. 您的数据提供程序返回null值。 You need to write code to pass values through tabArray. 您需要编写代码以通过tabArray传递值。

You need to put data in your tabArray after reading the excel. 您需要在阅读excel后将数据放入tabArray中。 There is no need for you to call the testLogin method in the dataprovider method, which would defeat the entire purpose of using the DataProvider. 您无需在dataprovider方法中调用testLogin方法,这会破坏使用DataProvider的整个目的。

Read a bit about dataprovider @ http://testng.org/doc/documentation-main.html#parameters-dataproviders 阅读一些关于dataprovider @ http://testng.org/doc/documentation-main.html#parameters-dataproviders

In your for loop, replace the call to your testLogin method with putting data in your tabArray. 在for循环中,将数据放入tabArray中,替换对testLogin方法的调用。 eg. 例如。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM