问题描述
我尝试使用phantomjs-maven-plugin来安装phantomjs二进制文件。我想在Tomcat7服务器上运行我的测试,这就是我需要自动配置二进制文件的原因。
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.
这是我的pom.xml
Here is my 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-jackson</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>
接下来是我如何初始化webdriver ....看看构造函数并跳转到底部的main()函数
And then here is how I am initializing the webdriver ....just look the constructor and skip to the main() function in bottom
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");
}
}
所以问题在于我我无法验证我的二进制文件是否已经安装....即使它确实如此,那么为什么 main()为system.property(phantomjs.binary)打印null? / code>
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")
我提供了完整的pom.xml和java代码......请帮我看看我是什么
做错了
I provided my complete pom.xml and java code... please help me see what I amdoing wrong
编辑:
在main()函数中,我调用 FindTrains
通过在该对象上创建 FindTrains
的对象并调用 getTrains()
。但由于缺少二进制文件而未配置驱动程序
.... main()
的第一行打印 null
。
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
.
推荐答案
您可以使用。只需添加以下依赖项:
You can use WebDriverManager. Simply add the following dependency:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.2.0</version>
</dependency>
然后,在你的代码电话中:
And then, in your code call to:
WebDriverManager.phantomjs().setup();
WebDriverManager下载与Selenium WebDriver一起使用的最新版本的PhantomJS二进制文件。
WebDriverManager downloads the latest version of the required PhantomJS binary to be used with Selenium WebDriver.
这篇关于将PhantomJS二进制文件添加到maven项目的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!