java - 无法在 selenium 中打开浏览器,抛出“null”消息
问题描述
我有一个使用 POM 模型的框架,并且我使用了一个 Utils 文件夹结构,其中包含配置和打开浏览器的 DriverFactory、Constant 和 ReadConfigFile java 文件。(下面的代码)
现在在我这样做之前(引入 Utils 包)代码运行良好,在引入 Utils 包之后,我得到“无法加载 browsernull”异常。
不确定是什么导致了问题。我正在使用 Mac 机器。
常量.java文件:
package Utils;
public class Constant {
public final static String CONFIG_PROPERTIES_DIRECTORY = "properties/config.properties";
public final static String GECKO_DRIVER_DIRECTORY = System.getProperty("user.dir")+"CucumberFramework/src/test/java/Resources/geckodriver";
public final static String CHROME_DRIVER_DIRECTORY = System.getProperty("user.dir")+"CucumberFramework/src/test/java/Resources/chromedriver";
}
DriverFactory.java文件:
package Utils;
//import com.sun.java.util.jar.pack.Instruction;
import com.sun.tools.javac.code.Attribute;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import pageObjects.AddIssuePage;
import pageObjects.Login_Page;
import java.util.concurrent.TimeUnit;
public class DriverFactory {
public static WebDriver driver;
public static Login_Page login_page;
public static AddIssuePage addIssuePage;
public WebDriver getDriver() {
try
{
ReadConfigFile file = new ReadConfigFile();
String browserName = file.getBrowser();
switch (browserName) {
case "firefox":
if (null == driver) {
System.setProperty("webdriver.geckodriver", Constant.GECKO_DRIVER_DIRECTORY);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver();
}
break;
case "chrome":
if (null == driver) {
System.setProperty("webdriver.chromedriver", Constant.CHROME_DRIVER_DIRECTORY);
driver = new ChromeDriver();
}
break;
}
} catch (Exception e)
{
System.out.println("Unable to load browser" + e.getMessage());
} finally
{
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
login_page = PageFactory.initElements(driver, Login_Page.class);
}
return driver;
}
}
ReadConfigFile.java文件:
package Utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadConfigFile {
protected InputStream input = null;
protected Properties prop = null;
public ReadConfigFile () {
try {
ReadConfigFile.class.getClassLoader ().getResourceAsStream (Constant.CONFIG_PROPERTIES_DIRECTORY);
prop = new Properties();
prop.load(input);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getBrowser(){
if (prop.getProperty("browser") == null)
return " ";
return prop.getProperty("browser");
}
}
堆栈跟踪:
java.lang.NullPointerException
at Utils.DriverFactory.getDriver(DriverFactory.java:63)
at Steps.MasterHooks.setup(MasterHooks.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at cucumber.runtime.Utils$1.call(Utils.java:40)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:34)
at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
at cucumber.runtime.model.CucumberScenarioOutline.run(CucumberScenarioOutline.java:46)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.runtime.Runtime.run(Runtime.java:122)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
java.lang.NullPointerException
at Utils.DriverFactory.getDriver(DriverFactory.java:63)
at Steps.MasterHooks.setup(MasterHooks.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at cucumber.runtime.Utils$1.call(Utils.java:40)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:34)
at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
at cucumber.runtime.model.CucumberScenarioOutline.run(CucumberScenarioOutline.java:46)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.runtime.Runtime.run(Runtime.java:122)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
Process finished with exit code 1
MasterHooks
package Steps;
import Utils.DriverFactory;
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class MasterHooks extends DriverFactory {
@Before
public void setup() {
driver = getDriver();
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
解决方案
此错误消息...
Unable to load browsernull
...暗示该函数getBrowser()
正在返回null。
我会建议更改:
在ReadConfigFile.java 中:
改变:
ReadConfigFile.class.getClassLoader ().getResourceAsStream (Constant.CONFIG_PROPERTIES_DIRECTORY);
至:
File src = new File(Constant.CONFIG_PROPERTIES_DIRECTORY); FileInputStream fis = new FileInputStream(src); Properties prop = new Properties(); prop.load(fis);
改变:
public String getBrowser(){ if (prop.getProperty("browser") == null) return " "; return prop.getProperty("browser"); }
至:
public String getBrowser(){ return prop.getProperty("browser"); }
在Constant.java中
改变:
public final static String CONFIG_PROPERTIES_DIRECTORY = "properties/config.properties";
至:
public final static String CONFIG_PROPERTIES_DIRECTORY = "./src/main/java/properties/config.properties";
在DriverFactory.java
改变:
"webdriver.geckodriver"
至:
"webdriver.gecko.driver"
改变:
"webdriver.chromedriver"
至:
"webdriver.chrome.driver"
推荐阅读
- http - Telnet 端口 80 可以工作,但 http 浏览不能... 两个不同的网络?这可能是什么?
- postgresql - Postgresql - 除以两个整数后,我得到一个零
- vb.net - 在 VB.net 中,使用 OLEDB 在一次执行中,如何更新在“GROUP BY ... HAVING COUNT(*) ...”查询中也引用的列值?
- speech-to-text - Google Speech To Text API:实时获取中间结果的单词置信度
- reactjs - ReactJS 分页帮助按钮无法正常工作
- string - 具有大写和相应小写字符的最小窗口(子字符串)
- rust - 在这种情况下,to_owned() 和 clone() 有什么区别?
- laravel - Laravel如何使用回调函数将失败的作业发送回队列?
- python - Django 对象不保存
- javascript - 使用 Vue 插件的 Vite MPA 是如何工作的?