我尝试在我的 maven-java 项目中使用属性文件进行测试自动化。
这是 context.xml 文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/context ">
<util:properties id="properties" location="classpath:test-context.properties"/>
<context:property-placeholder properties-ref="properties" ignore-unresolvable="false"/>
<bean id="settings" class="util.TestSettings">
<property name="properties" ref="properties"/>
</bean>
这些是我的java类。
import java.util.Properties;
public class TestSettings {
private static Properties properties;
public static String getProperty(String key) {
return properties.getProperty(key);
}
public void setProperties(Properties properties) {
TestSettings.properties = properties;
}
}
@ContextConfiguration(locations = "classpath:test-context.xml")
public class P_1_LoginPage extends AbstractTestNGSpringContextTests {
private P_1_LoginPage p1LoginPage;
private WebDriver driver;
public P_1_LoginPage(WebDriver driver) {
this.driver = driver;
driver.get(TestSettings.getProperty("base.url"));
}
@BeforeClass(alwaysRun = true)
public void BeforeTest() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
p1LoginPage = new P_1_LoginPage(driver);
}
public void assertThere() {
//assert here
}
更新
这是我的 context.property 文件。
base.url=http://uname:pword@test.mysite.com.au/sdfdf
email.Queue.url=http://uname:pword@test.mysite.com.au/admin/admin/show_msgs
当我尝试运行测试用例时,它会在这里给出一个空指针异常。
public static String getProperty(String key) {
return properties.getProperty(key);
}
有人可以帮我解决这里的问题吗?