使用Maven和TestNG的java项目。尝试将环境变量放入maven配置文件,并使用mvn clean install -Pdevhost
调用它们问题在于浏览器无法获得siteurl。错误:
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>
文件名为&#34; environment.properties&#34;文件并位于src / test / resources:
下site.url=${site.url}
驱动程序获取siteurl的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);
答案 0 :(得分:0)
将属性加载器添加到java类中。
PropertyLoader config = new PropertyLoader();
baseUrl = config.getProperty("site.url");
driver.get(baseUrl);