2014-10-20 295 views
3

我在移动网页上使用eclipse和maven进行移动自动化测试。从pom.xml获取变量java

我定义我的pom.xml

<properties> 
    <MY_VARIABLE>www.google.com/</MY_VARIABLE> 
</properties> 

以下,但是当我打电话这一点使用

String testurl1 = System.getProperty("MY_VARIABLE"); 

它似乎总是返回null。

我也试图定义变量

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.16</version> 
    <configuration> 
     <systemPropertyVariables> 
     <MY_VARIABLE>www.google.com</MY_VARIABLE> 
     </systemPropertyVariables> 
    </configuration> 
</plugin> 

以下方式,但仍然我得到了一个空值。

我可以使用一些帮助 谢谢。

+0

您应该使用属性文件为 – 2014-10-20 07:15:51

+0

我认为有混合的变量名称。您使用我们的属性MY_VARIABLE,但在确定的插件配置其MY_URL/MY_VARIABLE?您使用surefire插件的方式应该可行,您可能会使用错误的名称? – wemu 2014-10-20 07:18:32

+0

@wemu我没有在我的代码中使用MY_VARIABLE两次,无论是第一种方法还是第二种方法... – Ram 2014-10-20 07:27:09

回答

3

您的配置在eclipse中不起作用,因为surefire没有良好的m2e支持。 maven surefire插件创建了一个新进程,并提供了systemPropertyVariables。如果您从命令行运行测试,您的配置将会工作,例如

mvn surefire:test 

,使其在两个世界运行(命令行和eclipse)我做这种方式...

  1. 创建src/test/resources/maven.properties
  2. 编辑maven.properties文件,并把属性你需要它,例如

    project.build.directory=${project.build.directory} 
    MY_VARIABLE=${MY_VARIABLE} 
    
  3. 在您的测试启用资源筛选测试资源

    <build> 
        <testResources> 
         <testResource> 
          <directory>src/test/resources</directory> 
          <filtering>true</filtering> 
         </testResource> 
        </testResources> 
        ... 
    </build> 
    
  4. 负载的性能和访问它们

    Properties mavenProps = new Properties(); 
    InputStream in = TestClass.class.getResourceAsStream("/maven.properties"); 
    mavenProps.load(in); 
    
    String buildDir = mavenProps.getProperty("project.build.directory"); 
    String myVar = mavenProps.getProperty("MY_VARIABLE"); 
    
+0

所以我不得不添加此行在maven.properties文件 MY_VARIABLE = www.google.com 以及如何在mavenProps.load(in)后调用变量;命令... – Ram 2014-10-20 07:30:29

+0

我更新了我的答案,以便更清楚地了解您的用例。 – 2014-10-20 07:36:09

+0

感谢它只为你工作了一个问题,我可以通过$ {MY_VARIABLE}对我的pom.xml中的maven.properties使用MY_VARIABLE? – Ram 2014-10-20 07:37:34