Anu*_*iya 2 java maven phantomjs selenium-webdriver phantomjs-maven-plugin
我尝试使用phantomjs-maven-plugin来安装phantomjs二进制文件.我想在Tomcat7服务器上运行我的测试,这就是我需要自动配置二进制文件的原因.
这是我的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>
Run Code Online (Sandbox Code Playgroud)
接下来是我如何初始化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");
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是我无法验证我的二进制文件是否已安装......即使它已经安装,那么为什么呢? main() prints null for system.property("phantomjs.binary")
我提供了完整的pom.xml和java代码......请帮我看看我做错了什么
编辑:
在main()函数中,我FindTrains通过创建对象FindTrains并调用该对象来调用getTrains()它.但由于driver没有配置因为缺少二进制....第一行main()打印null.
您可以使用WebDriverManager.只需添加以下依赖项:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.7.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后,在您的代码调用中:
WebDriverManager.phantomjs().setup();
Run Code Online (Sandbox Code Playgroud)
WebDriverManager下载与Selenium WebDriver一起使用的所需PhantomJS二进制文件的最新版本.
| 归档时间: |
|
| 查看次数: |
11965 次 |
| 最近记录: |