0

我尝试在我的 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);
}

有人可以帮我解决这里的问题吗?

4

1 回答 1

1

您的代码违反了一些 Spring 准则,例如用于访问属性的静态字段,或P_1_LoginPage在容器外使用 new 运算符创建。但是 NullPointer 的主要问题是,Spring 上下文尚未初始化@BeforeClass(方法也应该是静态的,否则会导致异常)。替换@BeforeClass@Before

于 2013-01-09T07:44:17.317 回答