将 PhantomJS 二进制文件添加到 Maven 项目的更好方法?

Better way to add PhantomJS binary to a maven project?

我尝试使用 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>

下面是我初始化 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");

    }
} 

所以问题是我无法验证我的二进制文件是否已安装....即使安装了,那为什么 main() prints null for system.property("phantomjs.binary")

我提供了完整的 pom.xml 和 java 代码...请帮我看看我是什么 做错了

编辑:

在 main() 函数中,我通过创建 FindTrains 的对象并对该对象调用 getTrains() 来调用 FindTrains。但是由于 driver 由于缺少二进制文件而未配置.... main() 的第一行打印 null.

系统属性没有设置的原因是因为你是用maven-surefire-plugin设置的。这是在 Maven 测试阶段运行所有 JUnit 测试的插件。因此,surefire 执行的任何 JUnit 测试都会使系统 属性 可用。但是,听起来您是 运行 class 的 main() 方法,而不是 JUnit,所以系统 属性 当然不存在。

我真的不清楚你到底是什么doing/expecting。此测试 运行 是您的 Maven 构建过程的一部分吗?这就是 phantomjs-maven-plugin 的目的,它不是为了将 phantomjs 嵌入到 Java 应用程序而构建的。

您可以使用 WebDriverManager。只需添加以下依赖项:

<dependency>
   <groupId>io.github.bonigarcia</groupId>
   <artifactId>webdrivermanager</artifactId>
    <version>4.4.3</version>
</dependency>

然后,在您的代码调用中:

WebDriverManager.phantomjs().setup();

WebDriverManager 下载与 Selenium WebDriver 一起使用的所需 PhantomJS 二进制文件的最新版本。