我有一个使用POM模型的框架,我使用了Utils文件夹结构,该文件夹结构包含配置和打开浏览器的DriverFactory,Constant和ReadConfigFile java文件。 (以下代码)
在我这样做之前(介绍了Utils包),代码运行正常,在介绍了Utils包后,我得到了#34;无法加载browsernull"例外。
不确定导致问题的原因。我正在使用Mac机。
Constant.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();
}
}
}
答案 0 :(得分:0)
这里的问题是,在Read config file class中,Input Stream类型的输入实例变量被赋值为null。 应将其分配给输入流,如下所示。
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 {
input = 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");
}
}
答案 1 :(得分:0)
此错误消息......
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"
答案 2 :(得分:0)
你能分享你的properties / config.properties文件吗?
在你分享的信息中,我正在思考:
当你在DriverFactory中调用
时 ReadConfigFile file = new ReadConfigFile();
String browserName = file.getBrowser();
在getBrowser()中有:
public String getBrowser(){
if (prop.getProperty("browser") == null)
return " ";
return prop.getProperty("browser");
}
因此,如果prop.getProperty("browser")
为空,则您将在browserName中返回" "
。
在开关中:
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);
}
我没有看到任何检查案例browserName是" "
,或者在另一种情况下(我的意思是,没有&#39; ta 默认情况下)< / strong>即可。因此,如果browserName是&#34; &#34;或其他不同的&#34; firefox&#34;或&#34; chrome&#34;你直接进入finally语句(最后总是执行)。在这种情况下,由于未实例化驱动程序,因此在
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);