gpt4 book ai didi

java - 通过 TestNG 运行 Selenium 时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 21:16:43 26 4
gpt4 key购买 nike

我有 Test 基类,它调用 LandingPage 类中的方法,该类具有被测页面的定位器。当我执行这个项目时,我收到空指针异常。我确定这与 testNG 注释有关,但我无法找出原因。

package com.xyz.tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.xyz.pageObjects.LandingPage;
import com.xyz.pageObjects.LoginPage;
import com.xyz.utils.Utils;

import engine.Engine;

public class LandingPageTest {

public WebDriver driver;
//Engine engine = new Engine(driver);
LandingPage landingPage;
LoginPage loginPage;

@BeforeTest
public void setUp(){

driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.go.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}

@Test
public void clickOnSignInLink() {

landingPage.SignIn().click();

}

下面是我的 LandingPage 类

 package com.xyz.pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class LandingPage {

private WebDriver driver;

public LandingPage(WebDriver driver) {
this.driver = driver;
}

//By signIn= By.xpath(".//*[@id='pageContainerInner']/div[2]/div[1]/div/div[2]/div[1]/a");

By signInBtn = By.linkText("Sign In or Create Account");
//By signInBtn2 = By.cssSelector("css=a.signIn");

By closeCrisisMessage = By.xpath(".//*[@id='closeCrisisMessageBtn']");
public WebElement SignIn(){
return driver.findElement(signInBtn);
}
}

错误信息如下

 C:\Users\ad\AppData\Local\Temp\testng-eclipse--957796922\testng-customsuite.xml

FAILED: clickOnSignInLink
java.lang.NullPointerException
at com.disney.tests.LandingPageTest.clickOnSignInLink(LandingPageTest.java:36)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
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)

如有任何建议,我们将不胜感激。

最佳答案

我不确定在哪一行抛出 NullPointerException,但我希望这会失败,因为运行 @Test 方法时您的landingPage 尚未初始化:

landingPage = new LandingPage(driver);

关于java - 通过 TestNG 运行 Selenium 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39895382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com