更新时间:2023-10-10 15:35:22
我建议您看一下:
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中设置的属性/在命令行中覆盖. /p>
该文件如下所示:
binaryRootFolder=${standalone.binary.root.folder}
最简单的方法是克隆此答案开头所链接的项目,然后运行:
mvn verify -Pselenium-tests
这将向您显示一切正常,并为您提供良好的基础.
I am currently creating selenium automation using webdriver in Maven using Java. Now for initializing the browsers such as Chrome and IE I have to set the system property such as
System.setProperty("webdriver.chrome.driver", "F:\\somewhereintheworkingdir\\drivers\chromedriver.exe");
Now, my deliverable is in terms of JAR. I am using maven so this is currently under main>resources>drivers>chromedriver.exe
So after package it will be unders root>drivers>chromedriver.exe
So How to make the system property that it will run in both the cases?
I researched about some constants like java.class.path
or java.file.seperator
etc. but I am not sure how they will useful here in this case.
I hope someone can help me.
I would suggest having a look at this:
https://github.com/Ardesco/Selenium-Maven-Template
The relevant parts are the POM where it uses this plugin:
<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>
and in the base class where it pulls in the locations of the binaries:
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;
}
You also need to have a properties file in src/main/resources (has to be in main, not test) that maven can update at build time to pass in properties set in the POM/overridden on the command line.
That file would look like this:
binaryRootFolder=${standalone.binary.root.folder}
The easiest thing to do would be clone the project linked to at the start of this answer and just run:
mvn verify -Pselenium-tests
That will show you everything working and give you a good base to start from.