出现 java.lang.NullPointerException [英] java.lang.NullPointerException is appearing
本文介绍了出现 java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是自动化领域的新手.这是我的简单 TestNG 登录代码,当我将代码作为 TestNG 运行时,它会出现 java.lang.NullPointerException 并通过双击它突出显示我导航到 URL 的位置.这是我的代码.
I am new in automation. Here is my simple TestNG login code , when I ran the code as TestNG it appears java.lang.NullPointerException and by double clicking it highlights the place where I navigate to the URL. here is my code.
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 实例声明为:
You have only declared the WebDriver instance as:
public WebDriver driver;
您也需要按如下方式对其进行初始化:
You need to initialize it too as follows:
driver = new FirefoxDriver();
与 FirefoxOptions()
的实例一起,您的有效代码块将是:
Along with the instance of FirefoxOptions()
your effective code block will be:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文