英文:
java.lang.NullPointerException for TestNG classes
问题
大家好,我在运行以下的 TestNG 测试时遇到了错误。
public class BaseTest {
WebDriver driver;
@BeforeClass
public void LaunchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(5000);
driver.get("https://stackoverflow.com/questions/26032149/selenium-testng-java-lang-nullpointerexception");
System.out.println("1");
driver.manage().window().maximize();
Thread.sleep(10000);
}
}
public class Homepage extends BaseTest
{
WebDriver driver;
@Test
public void Discover() {
driver.findElement(By.xpath("/html/body/header/div/ol[1]/li/a")).click();
}
}
错误信息:
1
FAILED: Discover
java.lang.NullPointerException
at pages.Homepage.Discover(Homepage.java:16)
===============================================
默认测试
已运行测试:1,失败:1,跳过:0
===============================================
英文:
Hello guys I am getting error while running below TestNG test.
public class BaseTest {
WebDriver driver;
@BeforeClass
public void LaunchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(5000);
driver.get("https://stackoverflow.com/questions/26032149/selenium-testng-java-lang-nullpointerexception");
System.out.println("1");
driver.manage().window().maximize();
Thread.sleep(10000);
}
}
public class Homepage extends BaseTest
{
WebDriver driver;
@Test
public void Discover() {
driver.findElement(By.xpath("/html/body/header/div/ol[1]/li/a")).click();
}
}
Error:
1
FAILED: Discover
java.lang.NullPointerException
at pages.Homepage.Discover(Homepage.java:16)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
答案1
得分: 1
空指针异常是由于您初始化驱动程序实例的方式与后来不同导致的。请将驱动程序实例创建并作为全局变量使用。
static WebDriver driver;
或者,
单例模式是解决测试自动化中空指针异常的方案。
创建一个类,并使用方法,您可以在任何地方使用驱动程序实例,而不会出现空指针等问题。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
public class TestApp {
private WebDriver driver;
private static TestApp myObj;
utils.PropertyFileReader property = new PropertyFileReader();
public static TestApp getInstance() {
if (myObj == null) {
myObj = new TestApp();
return myObj;
} else {
return myObj;
}
}
public WebDriver getDriver() {
return driver;
}
private void setDriver(WebDriver driver) {
this.driver = driver;
}
public static void setMyObj(TestApp myObj) {
TestApp.myObj = myObj;
}
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", getChromeDriverFilePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void navigateToURL() {
String url = property.getProperty("config", "url");
driver.get(url);
}
public void closeBrowser() {
driver.quit();
}
public WebElement waitForElement(By locator, int timeout) {
WebElement element = new WebDriverWait(TestApp.getInstance().getDriver(), timeout).until
(ExpectedConditions.presenceOfElementLocated(locator));
return element;
}
private String getChromeDriverFilePath() {
URL res = getClass().getClassLoader().getResource("chromedriver.exe");
File file = null;
try {
file = Paths.get(res.toURI()).toFile();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
}
如果要调用驱动程序实例,您可以使用以下行:
TestApp.getInstance().getDriver();
英文:
Null point exception occurring because of driver instance you are initiating begin differently than later.
Please create and use driver instance as a global variable
static WebDriver driver;
-------------------------------------------------------------------------------------------
OR
Singleton is solution for null point exception in your test automation
Create class and by using method you can use driver instance any where without any issue like null point
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
public class TestApp {
private WebDriver driver;
private static TestApp myObj;
// public static WebDriver driver;
utils.PropertyFileReader property = new PropertyFileReader();
public static TestApp getInstance() {
if (myObj == null) {
myObj = new TestApp();
return myObj;
} else {
return myObj;
}
}
//get the selenium driver
public WebDriver getDriver()
{
return driver;
}
//when selenium opens the browsers it will automatically set the web driver
private void setDriver(WebDriver driver) {
this.driver = driver;
}
public static void setMyObj(TestApp myObj) {
TestApp.myObj = myObj;
}
public void openBrowser() {
//String chromeDriverPath = property.getProperty("config", "chrome.driver.path");
//String chromeDriverPath = property.getProperty("config", getChromeDriverFilePath());
System.setProperty("webdriver.chrome.driver", getChromeDriverFilePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void navigateToURL() {
String url =property.getProperty("config","url");
;
driver.get(url);
}
public void closeBrowser()
{
driver.quit();
}
public WebElement waitForElement(By locator, int timeout)
{
WebElement element = new WebDriverWait(TestApp.getInstance().getDriver(), timeout).until
(ExpectedConditions.presenceOfElementLocated(locator));
return element;
}
private String getChromeDriverFilePath()
{
// checking resources file for chrome driver in side main resources
URL res = getClass().getClassLoader().getResource("chromedriver.exe");
File file = null;
try {
file = Paths.get(res.toURI()).toFile();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
}
//If you want call driver instance you can use following line
**TestApp.getInstance().getDriver();**
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论