gpt4 book ai didi

出现 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 09:21:34 28 4
gpt4 key购买 nike

我是自动化领域的新手。这是我的简单 TestNG 登录代码,当我以 TestNG 身份运行该代码时,它会出现 java.lang.NullPointerException,双击它会突出显示我导航到 URL 的位置。这是我的代码。

enter code here
package Day5pkg;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Day5 {
public WebDriver driver;
@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.firefox.driver","C:\\Users\\Nabila\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");

FirefoxOptions options = new FirefoxOptions();
driver.navigate().to("http://www.demo.guru99.com/V4/");
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");

}
@Test (priority = 0)
public void login(){
driver = new FirefoxDriver();

driver.findElement(By.name("uid")).sendKeys("mngr231");
driver.findElement(By.name("password")).click();
driver.findElement(By.name("password")).sendKeys("ehyjusu");
driver.findElement(By.name("btnLogin")).click();
System.out.println("Manger Id : mngr230");
}

最佳答案

您仅将 WebDriver 实例声明为:

public WebDriver driver;

您还需要按如下方式初始化它:

driver = new FirefoxDriver();

FirefoxOptions() 实例一起,您的有效代码块将是:

System.setProperty("webdriver.firefox.driver","C:\\Users\\Nabila\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new FirefoxDriver(options);
driver.navigate().to("http://www.demo.guru99.com/V4/");
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);

关于出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58661054/

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