java 将 PhantomJS 二进制文件添加到 Maven 项目的更好方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32678881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Better way to add PhantomJS binary to a maven project?
提问by Anudeep Samaiya
I tried using the phantomjs-maven-plugin to install phantomjs binary. I wanted to run my tests on a Tomcat7 server that is why I need to configure binary automatically.
我尝试使用 phantomjs-maven-plugin 来安装 phantomjs 二进制文件。我想在 Tomcat7 服务器上运行我的测试,这就是我需要自动配置二进制文件的原因。
Here is my pom.xml
这是我的 pom.xml
<properties>
<ghostdriver.version>1.2.0</ghostdriver.version>
<phantomjs.version>1.9.7</phantomjs.version>
<phantomjs-maven-plugin.version>0.7</phantomjs-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>com.github.detro</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>${ghostdriver.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.21</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.21</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.21</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>${phantomjs-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.9.7</version>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<phantomjs.binary>${phantomjs.binary}</phantomjs.binary>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
And then here is how I am initializing the webdriver ....just look the constructor and skip to the main() function in bottom
然后这里是我如何初始化 webdriver ....只看构造函数并跳到底部的 main() 函数
public class FindTrains {
private WebDriver driver;
//private WebDriverWait wait;
JavascriptExecutor js;
String baseURL = "http://www.indianrail.gov.in/inet_Srcdest.html";
public FindTrains(){
driver = new PhantomJSDriver();
//((HtmlUnitDriver)driver).setJavascriptEnabled(true);
//wait = new WebDriverWait(driver, 2);
js = (JavascriptExecutor) driver;
}
public void getTrains(String src, String dest){
driver.get(baseURL);
WebElement elemSrc = driver.findElement(By.xpath(xpathSrc));
setAttributeValue(elemSrc, src.toUpperCase());
WebElement elemDest = driver.findElement(By.xpath(xpathDest));
setAttributeValue(elemDest, dest.toUpperCase());
WebElement elemGetDetails = driver.findElement(By.xpath("//*[@id='formId']/table/tbody/tr/td/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table/tbody/tr[16]/td[2]/input[1]"));
elemGetDetails.click();
System.out.println(driver.getCurrentUrl()+ " "+ driver.getTitle());
}
public void setAttributeValue(WebElement elem, String value){
String scriptSetAttrValue = "arguments[0].setAttribute(arguments[1],arguments[2]);";
js.executeScript(scriptSetAttrValue, elem, "value", value);
}
public static void main(String [] args){
System.out.println(System.getProperty("phantomjs.binary"));
new FindTrains().getTrains("nad", "ndls");
}
}
So the problem is that the I am unable to verify that my whether the binary has been installed or not ....and even if it did, then why does main() prints null for system.property("phantomjs.binary")
所以问题是我无法验证我的二进制文件是否已安装......即使它安装了,那为什么 main() prints null for system.property("phantomjs.binary")
I provided my complete pom.xml and java code... please help me see what I am doing wrong
我提供了完整的 pom.xml 和 java 代码...请帮我看看我做错了什么
Edit:
编辑:
In the main() function I invoke FindTrains
by creating an object of FindTrains
and calling getTrains()
on that object. but since the driver
is not configured because of missing binary ....the first line of main()
prints null
.
在 main() 函数中,我FindTrains
通过创建一个对象FindTrains
并调用该对象来调用getTrains()
。但是由于driver
缺少二进制文件而未配置....main()
打印的第一行null
。
回答by Boni García
You can use WebDriverManager. Simply add the following dependency:
您可以使用WebDriverManager。只需添加以下依赖项:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.0.0</version>
</dependency>
And then, in your code call to:
然后,在您的代码中调用:
WebDriverManager.phantomjs().setup();
WebDriverManager downloads the latest version of the required PhantomJS binary to be used with Selenium WebDriver.
WebDriverManager 下载与 Selenium WebDriver 一起使用的所需 PhantomJS 二进制文件的最新版本。
回答by Kyle
The reason that the system property is not set is because you are using the maven-surefire-plugin
to set it. That is the plugin that runs all your JUnit tests during the maven test phase. So any JUnit tests that surefire executes will have the system property available. However, it sounds like you are running a main()
method of a class and not a JUnit at all so of course the system property isn't there.
未设置系统属性的原因是因为您正在使用maven-surefire-plugin
来设置它。这是在 maven 测试阶段运行所有 JUnit 测试的插件。因此,surefire 执行的任何 JUnit 测试都将具有可用的系统属性。但是,听起来您正在运行main()
类的方法而不是 JUnit,所以当然系统属性不存在。
I'm really unclear about what you are actually doing/expecting. Is this test ran as part of your maven build process? That is what the phantomjs-maven-plugin is built for, it's not built for embedding phantomjs into a Java application.
我真的不清楚你实际上在做什么/期待什么。此测试是否作为 Maven 构建过程的一部分运行?这就是 phantomjs-maven-plugin 的目的,它不是为将 phantomjs 嵌入 Java 应用程序而构建的。