gpt4 book ai didi

java - 在用于 Selenium 测试的 Maven 项目中使 System.setProperty 平台独立

转载 作者:行者123 更新时间:2023-12-04 05:24:34 25 4
gpt4 key购买 nike

我目前正在使用 Java 在 Maven 中使用 webdriver 创建 selenium 自动化。现在为了初始化 Chrome 和 IE 等浏览器,我必须设置系统属性,例如

System.setProperty("webdriver.chrome.driver", "F:\\somewhereintheworkingdir\\drivers\chromedriver.exe");

现在,我的交付物是 JAR 格式的。我正在使用 maven,所以它目前位于 main>resources>drivers>chromedriver.exe

所以打包后会在 root>drivers>chromedriver.exe 下

那么如何使系统属性在这两种情况下都能运行呢?

我研究了一些常量,例如 java.class.pathjava.file.seperator 等,但我不确定它们在这种情况下会有什么用处。

我希望有人能帮助我。

最佳答案

我建议看看这个:

https://github.com/Ardesco/Selenium-Maven-Template

相关部分是使用此插件的 POM:

<properties>
<standalone.binary.root.folder>${project.basedir}/selenium_standalone_binaries</standalone.binary.root.folder>
</properties>

<profiles>
<profile>
<id>selenium-tests</id>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.selenium</groupId>
<artifactId>driver-binary-downloader-maven-plugin</artifactId>
<version>0.9.2</version>
<configuration>
<rootStandaloneServerDirectory>${standalone.binary.root.folder}</rootStandaloneServerDirectory>
<downloadedZipFileDirectory>${project.basedir}/selenium_standalone_zips</downloadedZipFileDirectory>
<customRepositoryMap>${project.basedir}/RepositoryMap.xml</customRepositoryMap>
</configuration>
<executions>
<execution>
<goals>
<goal>selenium</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

并在提取二进制文件位置的基类中:

private static ResourceBundle _prop = ResourceBundle.getBundle("dev");
//Load standalone executable if required
switch (browserType) {
case CHROME:
if (System.getProperties().getProperty("os.arch").toLowerCase().equals("x86_64") || System.getProperties().getProperty("os.arch").toLowerCase().equals("amd64")) {
if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/windows/googlechrome/64bit/26/chromedriver.exe");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/osx/googlechrome/64bit/26/chromedriver");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("linux")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/linux/googlechrome/64bit/26/chromedriver");
}
} else {
if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/windows/googlechrome/32bit/26/chromedriver.exe");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/osx/googlechrome/32bit/26/chromedriver");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("linux")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/linux/googlechrome/32bit/26/chromedriver");
}
}
break;
case IE:
if (System.getProperties().getProperty("os.arch").toLowerCase().equals("x86_64") || System.getProperties().getProperty("os.arch").toLowerCase().equals("amd64")) {
System.setProperty("webdriver.ie.driver", _prop.getString("binaryRootFolder") + "/windows/internetexplorer/64bit/2.29.0/IEDriverServer.exe");
} else {
System.setProperty("webdriver.ie.driver", _prop.getString("binaryRootFolder") + "/windows/internetexplorer/32bit/2.29.0/IEDriverServer.exe");
}
break;
}

您还需要在 src/main/resources 中有一个属性文件(必须在 main 中,而不是测试中),maven 可以在构建时更新该文件,以传递在命令行上的 POM/overridden 中设置的属性。

该文件看起来像这样:

binaryRootFolder=${standalone.binary.root.folder}

最简单的做法是克隆本答案开头链接的项目,然后运行:

mvn verify -Pselenium-tests

这将向您展示一切正常,并为您奠定良好的基础。

关于java - 在用于 Selenium 测试的 Maven 项目中使 System.setProperty 平台独立,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13348201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com