gpt4 book ai didi

java - selenium webdriver 中的空指针异常 - TestNG - PageFactory

转载 作者:行者123 更新时间:2023-11-30 07:11:38 24 4
gpt4 key购买 nike

我正在尝试使用 selenium webdriver、testng 和页面工厂实现自动化。但我面临空指针异常。

public class BrowserOpenClose extends Common {
public WebDriver driver;

@BeforeTest
@Parameters("browser")
public void initBrowser(String browser) {
if (browser.equalsIgnoreCase("FF")) {
System.setProperty("webdriver.firefox.marionette", "./gecko/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.flipkart.com/");
} else if (browser.equalsIgnoreCase("CH")) {
System.setProperty("webdriver.chrome.driver", "./chrome/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.flipkart.com/");
}
}

@AfterTest
public void closeBrowser() {
waitUntil(10);
driver.quit();
}
}

public class Login extends BrowserOpenClose {

@FindBy(xpath = "//a[text()='Log In']")
private WebElement loginLink;

@FindBy(xpath = "//input[@class='_2zrpKA']")
private WebElement username;

@FindBy(xpath = "//input[@class='_2zrpKA _3v41xv']")
private WebElement password;

@FindBy(xpath = "//button[@class='_3zLR9i _1LctnI _36SmAs']")
private WebElement loginButton;

public Login(WebDriver driver) {
PageFactory.initElements(driver, this);
this.driver = driver;
}

public void openLoginDialog() {
loginLink.click();
}

public void enterUsername(String name) {
username.sendKeys(name);
}

public void enterPassword(String pass) {
password.sendKeys(pass);
}

public void clickLogin() {
loginButton.click();
}

}

public class SwitchTShirtPage extends BrowserOpenClose {
WebDriver driver;
Login login;

@BeforeMethod
public void initElements() {
login = new Login(driver);
}

@Test
public void switchToTShirt() {
login.openLoginDialog();
login.enterUsername("abc");
login.enterPassword("xyz");
login.clickLogin();
}

}

Error trace log: java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy9.click(Unknown Source) at com.login.test.Login.openLoginDialog(Login.java:50) at com.homepage.test.SwitchTShirtPage.switchToTShirt(SwitchTShirtPage.java:28) 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:100) at org.testng.internal.Invoker.invokeMethod(Invoker.java:646) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:746) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) at org.testng.SuiteRunner.run(SuiteRunner.java:268) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264) at org.testng.TestNG.runSuitesLocally(TestNG.java:1189) at org.testng.TestNG.runSuites(TestNG.java:1104) at org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)

最佳答案

来自 SwitchTShirtPage

WebDriver 驱动程序 从未初始化,并且始终为 null

您想要的是 BrowserOpenClose 中的声明,因此只需从 SwitchTShirtPage 中删除声明即可。

然后,要小心,因为 driver 是用 @BeforeTest 初始化的,它仅由测试(类组)运行一次。因此,只有 BrowserOpenClose 的第一个实例才会初始化 driver。考虑将 @BeforeTest 替换为 @BeforeClass 这应该可以解决问题。

关于java - selenium webdriver 中的空指针异常 - TestNG - PageFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39143731/

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