【问题标题】:i am getting a java.lang.NullPointerException while executing the testng script我在执行 testng 脚本时收到 java.lang.NullPointerException
【发布时间】:2022-01-31 06:16:19
【问题描述】:

在运行代码时,我在 chrome 中得到了这个 java.lang.NullPointerException,我已经将主登录类扩展到另一个,并显示了这个异常。我已经将同一个主类扩展到另一个差异类并且它成功运行但只有这个类显示错误。

这里是代码

//登录页面 公共类 admin_login {

//public static void main(String args[]) throws InterruptedException{
WebDriver driver;
@Test
public void login() throws InterruptedException{    
    System.setProperty("webdriver.chrome.driver","E:\\Automation\\jarfiles2\\chromedriver_win32_new\\chromedriver.exe");
    driver = new ChromeDriver();
    
    driver.get("http://localhost/Login");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    //driver.close();

    driver.findElement(By.id("UserName")).sendKeys("admin");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    driver.findElement(By.id("Password")).sendKeys("adminadmin");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    driver.findElement(By.cssSelector("input.btn")).click();
}

}

//用户类

公共类用户扩展 admin_login{

@Test
public void user() throws InterruptedException{
 //master dropdown  
//driver.findElement(By.xpath("//*[@id='topNavbarCollapse']/ul[1]/li[1]/a")).click();
    try{
        driver.findElement(By.xpath("//*[@id='topNavbarCollapse']/ul[1]/li[1]/a")).click();
        
    }
    catch(java.lang.NullPointerException n){
        //System.out.println("exception handaled");
        System.out.println(n.getMessage());
    }
    finally{
        driver.findElement(By.xpath("//*[@id='topNavbarCollapse']/ul[1]/li[1]/a")).click();
    }

我已经尝试正常获取,然后通过 try catch 也尝试过,但它仍然显示相同的异常并且测试失败

堆栈跟踪

java.lang.NullPointerException
    at Finlogin.User.user(User.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

【问题讨论】:

  • 你需要显示异常的堆栈跟踪。
  • @tgdavies 更新
  • 看起来driver 为空。

标签: java selenium exception nullpointerexception testng


【解决方案1】:
  • 1 java.lang.NullPointerException 出现是因为驱动程序为空。

    您已决定在login() test-method 中初始化驱动程序。所以user() 只有在login() 方法之前已经执行过(并且驱动程序已经初始化)时才会起作用。

  • 2 当您运行从另一个 Test 类扩展的某个 Test 类时,将为这两个类调用测试。但顺序可能不同。 至少当类名从A 和 类名从Z 开始。所以这可能是它适用的原因 其他一些类(用另一个名字)。

解决方案

我建议:

@Before配置方法中初始化常用测试资源。

例如

@BeforeClass
public void initDriver() {
    System.setProperty("webdriver.chrome.driver", "E:\\Automation\\jarfiles2\\chromedriver_win32_new\\chromedriver.exe");
    driver = new ChromeDriver();
}

@Test
public void login() throws InterruptedException{    
    driver.get("http://localhost/Login");
...

@AfterClass
public void closeDriver() {
    driver.quit();
}

因此,您将避免测试方法执行顺序依赖性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 2017-01-05
    • 1970-01-01
    相关资源
    最近更新 更多