我正在用Java运行一个硒框架,该框架使用DriverFactor类,Constant类,ReadConfigFile类和config.properities文件存储相关数据,我可以通过DriverFactory进行调用。
但是,当我运行功能部件文件时,我几乎会立即指出“无法加载浏览器:空”错误。
我认为这是由于配置文件无法正确读取而发生的,因此返回“ null”然后失败。我似乎无法解决此问题。
我添加了e.printstacktrace();试图更好地了解问题的根源,但我还没有暗示要解决。
DriverFactory.java
package utils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class DriverFactory {
public static WebDriver driver;
public WebDriver getDriver() {
try {
// Read Config
ReadConfigFile file = new ReadConfigFile();
String browser = file.getBrowser();
switch (browser) {
/*
* switch statement lets you select the browser you want
*/
case "firefox":
// code
if (null == driver) {
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver();
}
break;
case "chrome":
// code
if (null == driver) {
System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
break;
}
} catch (Exception e) {
System.out.println("Unable to load browser: " + e.getMessage());
e.printStackTrace();
}// finally {
//driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
//}
return driver;
}
}
恒定。 Java
package utils;
public class Constant {
/*
* Config Property File
*/
public final static String CONFIG_PROPERTIES_DIRECTORY = "config.properties";
public final static String CHROME_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\java\\resources\\chromedriver.exe";
public final static String GECKO_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\java\\resources\\geckondriver.exe";
}
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");
}
}
config.properities
browser=chrome
控制台/ StackTrace打印输出
Feature: Log into account
Existing user should be able to log into account using correct credentials
Scenario Outline: Login to account with credentials # C:/Users/Tom/Desktop/CucumberFramework/CucumberFramework/src/test/java/features/Login.feature:4
Given user navigates to "<url>"
When user clicks on the login portal button
And User enters username "<username>"
And User enters password "<password>"
And User clicks on the login button
Then user should be presented with validation "<message>"
Examples:
Unable to load browser: null
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at utils.ReadConfigFile.<init>(ReadConfigFile.java:15)
at utils.DriverFactory.getDriver(DriverFactory.java:16)
at stepDefinitions.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)
Unable to load browser: null
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at utils.ReadConfigFile.<init>(ReadConfigFile.java:15)
at utils.DriverFactory.getDriver(DriverFactory.java:16)
at stepDefinitions.GenericWebSteps.user_navigates_to(GenericWebSteps.java:21)
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.JavaStepDefinition.execute(JavaStepDefinition.java:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:300)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
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)
Scenario Outline: Login to account with credentials # C:/Users/Tom/Desktop/CucumberFramework/CucumberFramework/src/test/java/features/Login.feature:14
Given user navigates to "http://www.webdriveruniversity.com" # GenericWebSteps.user_navigates_to(String)
java.lang.NullPointerException
at stepDefinitions.GenericWebSteps.user_navigates_to(GenericWebSteps.java:21)
at ✽.Given user navigates to "http://www.webdriveruniversity.com"(C:/Users/Tom/Desktop/CucumberFramework/CucumberFramework/src/test/java/features/Login.feature:5)
When user clicks on the login portal button # GenericWebSteps.user_clicks_on_the_login_portal_button()
And User enters username "tomdale" # GenericWebSteps.user_enters_a_username(String)
And User enters password "testuser1" # GenericWebSteps.user_enters_a_password(String)
And User clicks on the login button # GenericWebSteps.user_clicks_on_the_login_button()
Then user should be presented with validation "validation failed" # GenericWebSteps.user_should_be_presented_with_validation(String)
Failed scenarios:
C:/Users/Tom/Desktop/CucumberFramework/CucumberFramework/src/test/java/features/Login.feature:14 # Scenario Outline: Login to account with credentials
1 Scenarios (1 failed)
6 Steps (1 failed, 5 skipped)
0m0.076s
java.lang.NullPointerException
at stepDefinitions.GenericWebSteps.user_navigates_to(GenericWebSteps.java:21)
at ✽.Given user navigates to "http://www.webdriveruniversity.com"(C:/Users/Tom/Desktop/CucumberFramework/CucumberFramework/src/test/java/features/Login.feature:5)
我希望加载chrome驱动程序实例或gecko驱动程序实例,并执行测试。
实际结果,我收到“无法加载浏览器:null”错误
答案 0 :(得分:0)
在这些行中
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);
您已将input
初始化为空,然后在prop.load(input)
中加载了一个input
,它仍然为空。这就是为什么有NPE的原因。
答案 1 :(得分:0)
对于遇到类似问题的任何人,我都设法将其解决。
首先,在ReadConfigFile.java中的try语句中,您需要将ReadConfig的初始化设置为输入。如下所示。
try {
input = ReadConfigFile.class.getClassLoader().getResourceAsStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
prop = new Properties();
prop.load(input);
} catch (IOException e) {
e.printStackTrace();
}
此外,在Constant.java类中,将CONFIG_PROPERTIES_DIRECTORY设置为存储config.properties文件的文件路径,如下所示:
package utils;
public class Constant {
/*
* Config Property File
*/
public final static String CONFIG_PROPERTIES_DIRECTORY = "properties\\config.properties";
public final static String CHROME_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\java\\resources\\chromedriver.exe";
public final static String GECKO_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\java\\resources\\geckondriver.exe";
}
如果您随后在所选功能文件上运行配置,则该文件应创建所选Web驱动程序的实例,并且测试应正常运行。