【发布时间】:2018-11-03 19:22:10
【问题描述】:
使用 Maven 和 TestNG 处理 java 项目。尝试将环境变量放入 Maven 配置文件并使用 mvn clean install -Pdevhost 调用它们
问题是浏览器无法获取站点网址。错误:
java.lang.NullPointerException: null value in entry: url=null
Pom 文件配置文件:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<file>${project.basedir}/suites/smoke.xml</file>
</suiteXmlFiles>
<systemPropertyVariables>
<environment.properties>/environment.properties</environment.properties>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<testResources>
<testResource>
<directory>src\test\resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<resources>
<resource>
<directory>src\test\resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<!-- Profiles configuration -->
<profiles>
<!--Environments "mvn test -P dev3"-->
<profile>
<id>devhost</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<site.url>https://siteurl.com</site.url>
</properties>
</profile>
</profiles>
具有属性的文件称为“environment.properties”文件,位于 src/test/resources 下:
site.url=${site.url}
驱动程序获取站点URL的Java类:
public void SetUp() {
driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver",
ConfigFileReader.getDriverPath());
driver.manage().window().maximize();
driver.get(System.getProperty("site.url"));
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
【问题讨论】:
-
是否还有其他配置文件处于活动状态?
activeByDefault并不意味着“始终处于活动状态”。这意味着“当且仅当没有其他配置文件处于活动状态时才处于活动状态”。虽然我希望它与命令行中指定的配置文件一样好。 -
没有,但 devhost 配置文件处于活动状态甚至存在。
-
如果你查看
target/classes/environment.properties,site.url是否有预期值?如果是,则 Maven 正在正确过滤。仅仅因为属性存在于类路径上的属性文件中并不意味着它最终会成为系统属性。