问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/13348201/making-system-setproperty-platform-independent-in-a-maven-project-for-selenium-t