英文:
NullPointerException at "new RemoteWebDriver(srvc.getUrl(), options)"
问题
我有以下三个类:
当我从“Tests.java”运行测试作为TestNG时,我会得到java.lang.NullPointerException异常:
注意:
-
它成功从.properties文件中获取浏览器名称“chrome”。
-
如果我丢弃“Tests.java”类,并将@Test方法移动到“LaunchBrowserTemp.java”类中,并将其余的相应标签(即BeforeClass、BeforeMethod、AfterClass、AfterMethod)添加到“LaunchBrowserTemp.java”中的相应方法中,它就可以正常运行。只有当我将testng注解保留在“Tests.java”类中时,它才会失败。
src\main\java\utilities\LaunchBrowserTemp.java
public class LaunchBrowserTemp {
private static ChromeDriverService srvc;
private static WebDriver driver;
private static String browser = getProperties("selenium.browser");
private static final String baseBrowserPath = "src\\main\\resources\\drivers\\";
private static ChromeOptions options;
public static void initBrowser() throws Exception {
switch (browser.toLowerCase()){
case "chrome":
options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-extensions");
options.setAcceptInsecureCerts(true);
srvc = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(baseBrowserPath + "chromedriver.exe"))
.usingAnyFreePort()
.build();
srvc.start();
break;
default:
throw new Exception("Browser Not handled in code!");
}
}
public static void stopDriverService() {
srvc.stop();
}
public void startDriver() {
driver = new RemoteWebDriver(srvc.getUrl(), options);
}
public void endDriver() {
driver.quit();
}
public static WebDriver driver() {
return driver;
}
}
src\main\java\utilities\LoadProperties.java
public class LoadProperties {
public static String getProperties(String propName) {
Properties prop = new Properties();
try {
FileInputStream fis = new FileInputStream("src/main/resources/config.properties");
prop.load(fis);
} catch (Exception e){}
return prop.getProperty(propName);
}
}
src\test\java\Tests.java
public class Tests {
LaunchBrowserTemp obj = new LaunchBrowserTemp();
@BeforeClass
public void init() throws Exception{
LaunchBrowserTemp.initBrowser();
}
@BeforeTest
public void start(){
obj.startDriver();
}
@Test
public void openUrl(){
driver().get("https://www.google.com");
}
@AfterTest
public void teardown(){
obj.endDriver();
}
@AfterClass
public void terminate() {
LaunchBrowserTemp.stopDriverService();
}
}
英文:
I have Three Classes as below:
And When I run the test as TestNG from 'Tests.java' I get java.lang.NullPointerException exception:
NOTE:
-It picks the browser name 'chrome' successfully from the .properties file.
-If I discard the 'Tests.java' class and move the @Test method to 'LaunchBrowserTemp.java' class and add remaining corresponding tags (i.e. BeforeClass, BeforeMethod, AfterClass, AfterMethod) to corresponding methods in 'LaunchBrowserTemp.java' it works fine. It fails only when I keep the testng annotations to the 'Tests.java' class.
src\main\java\utilities\LaunchBrowserTemp.java
public class LaunchBrowserTemp {
private static ChromeDriverService srvc;
private static WebDriver driver;
private static String browser = getProperties("selenium.browser");
private static final String baseBrowserPath = "src\\main\\resources\\drivers\\";
private static ChromeOptions options;
public static void initBrowser() throws Exception {
switch (browser.toLowerCase()){
case "chrome":
options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-extensions");
options.setAcceptInsecureCerts(true);
srvc = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(baseBrowserPath + "chromedriver.exe"))
.usingAnyFreePort()
.build();
srvc.start();
break;
default:
throw new Exception("Browser Not handled in code!");
}
}
public static void stopDriverService() {
srvc.stop();
}
public void startDriver() {
driver = new RemoteWebDriver(srvc.getUrl(), options);
}
public void endDriver() {
driver.quit();
}
public static WebDriver driver() {
return driver;
}
}
src\main\java\utilities\LoadProperties.java
public class LoadProperties {
public static String getProperties(String propName) {
Properties prop = new Properties();
try {
FileInputStream fis = new FileInputStream("src/main/resources/config.properties");
prop.load(fis);
} catch (Exception e){}
return prop.getProperty(propName);
}
}
src\test\java\Tests.java
public class Tests {
LaunchBrowserTemp obj = new LaunchBrowserTemp();
@BeforeClass
public void init() throws Exception{
LaunchBrowserTemp.initBrowser();
}
@BeforeTest
public void start(){
obj.startDriver();
}
@Test
public void openUrl(){
driver().get("https://www.google.com");
}
@AfterTest
public void teardown(){
obj.endDriver();
}
@AfterClass
public void terminate() {
LaunchBrowserTemp.stopDriverService();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论