【问题标题】:Integrating Jenkins with phantomjs for selenium webdriver test将 Jenkins 与 phantomjs 集成以进行 selenium webdriver 测试
【发布时间】:2014-08-20 10:12:03
【问题描述】:

我正在将 jenkins 与 phantomjs 集成以运行我的 selenium 测试脚本。 Phantomjs 安装在我的 jenkins 服务器中,并且 ghost 驱动程序在端口 8090 中运行。但是,我的测试仍然被跳过,它抛出了一个异常

驱动程序可执行文件的路径必须由 phantomjs.binary.path 能力/系统属性/PATH 变量;为了 更多信息,请参阅https://github.com/ariya/phantomjs/wiki。这 最新版本可以从 http://phantomjs.org/download.html"

我的 jenkins 在 centos 中运行。

我的代码如下所示,

@BeforeClass
  public void setUp() throws Exception {
      dCaps = new DesiredCapabilities();
      driver = new PhantomJSDriver(dCaps);
      baseUrl = "";
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

【问题讨论】:

  • 那么设置 PhantomJS 二进制文件路径的代码在哪里?
  • Tim,现在我添加了一个名为 env injection 的插件到 CI 环境中,并在其中注入了 phantomjs 二进制路径。现在它对我来说很好用。谢谢你的关心蒂姆..

标签: jenkins selenium-webdriver phantomjs ghostdriver


【解决方案1】:

驱动程序可执行文件的路径必须由 phantomjs.binary.path 能力/系统属性/PATH 变量;

您应该明确指出将 phantomJm exe 放置在应该执行测试的机器上的位置。所以我找到了两种解决方法:

1) 可能性 #1(在代码中明确指出)

@BeforeClass
    public void seleniumGrridUponGhostDriver() throws MalformedURLException {

        File phantomjs = new File(System.getProperty("java.io.tmpdir")+File.separator+"phantomjs-1.9.7");


        DesiredCapabilities dcaps = new DesiredCapabilities();
        dcaps.setCapability("takesScreenshot", true);


        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());

   this.driver = new RemoteWebDriver(new URL("http://162.243.175.134:8080"), dcaps);


        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

        //page instances init()
        loginPage = PageFactory.initElements(driver, LoginPage.class);
        homePage = PageFactory.initElements(driver, FacebookUserPage.class);
    }

2) 可能性 #2,使用PhantomJS Windows/Mac OS X/Linux native binary embedder

pom.xml 依赖:

<!--substituting   phanbedder  with local Phanbedder implementation-->
                     <dependency>
                 <groupId>net.anthavio</groupId>
                 <artifactId>phanbedder-1.9.7</artifactId>
                 <version>1.0.0</version>
             </dependency>



             <dependency>
                 <groupId>com.github.detro.ghostdriver</groupId>
                 <artifactId>phantomjsdriver</artifactId>
                 <version>1.1.0</version>
             </dependency>

代码:

 import net.anthavio.phanbedder.Phanbedder;

 @BeforeClass
    public void seleniumGrridUponGhostDriver() throws MalformedURLException {


       File phantomjs = Phanbedder.unpack(); //Phanbedder to the rescue!
  DesiredCapabilities dcaps = new DesiredCapabilities();
        dcaps.setCapability("takesScreenshot", true);


        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());

   this.driver = new RemoteWebDriver(new URL("http://162.243.175.134:8080"), dcaps);


        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

        //page instances init()
        loginPage = PageFactory.initElements(driver, LoginPage.class);
        homePage = PageFactory.initElements(driver, FacebookUserPage.class);
    }

希望对你有帮助

【讨论】:

    【解决方案2】:

    我也为此苦苦挣扎,并且已经在 Linux 上构建了 Jenkins,并希望使用该安装。使用系统属性不起作用(对于服务,甚至 Jenkins 中的全局 Maven 属性)。

    我不喜欢上面的程序化方法,因为它将解决方案与特定于平台的配置联系在一起。

    最后,起作用的是把它放到 POM 中。

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <systemProperties>
                                <phantomjs.binary.path>/usr/local/phantomjs/bin/phantomjs</phantomjs.binary.path>
                            </systemProperties>
                        </configuration>
                    </plugin>
    

    以下是对我有用的更详细的描述(不是我的文章): http://balamaci.ro/continous-integration-with-jenkins-docker-ansible-webdriver/

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 2015-09-12
      • 2016-12-04
      • 2013-03-08
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      相关资源
      最近更新 更多