山pの楽しいお勉強生活

勉強の成果を垂れ流していきます

PhantomJSDriverは「com.codeborne」を使用する

概要

GebでChromeからPhantomJSに変えようとしたらハマったのでメモします。

PhantomJSを使う場合、「phantomjsdriver」のグループIDは「com.codeborne」を使用しましょう。

Web上でサンプルを探すと、1.1は「com.github.detro.ghostdriver」、1.2は「com.github.detro」のようなグループIDがでてきますが、ghostdriverのリポジトリを見るともうサポートできないと書かれています。 で、色々forkされていますが、2017/02/06現在は「com.codeborne」を使用するのがベターみたいです。(codeborneはselenideを開発しているようなので、これからもアップデートされる確率は高いでしょう。)

Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies

とか言われて泣いている人は試してみてください。

コード

@Grapes([
    @Grab("org.gebish:geb-core:1.1"),
    @Grab("com.codeborne:phantomjsdriver:1.3.0"),
])

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.phantomjs.PhantomJSDriverService
import org.openqa.selenium.phantomjs.PhantomJSDriver

import geb.Browser
import geb.Configuration

println "start ${new Date().format('yyyy/MM/dd HH:mm:ss')}"

def caps = new DesiredCapabilities()
// phantomjs.exeは前もって準備が必要。
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"./driver/phantomjs.exe");

def conf = new Configuration()
conf.driver = new PhantomJSDriver(caps)

Browser.drive(conf) {
  go "http://gebish.org"
  assert title == "Geb - Very Groovy Browser Automation"
  assert $("h1#logo").text() == "Geb (pronounced “jeb”)"
}

println "end ${new Date().format('yyyy/MM/dd HH:mm:ss')}"

余談

ハマったので「geb-example-gradle」で試してみた所、PhantomJSは失敗します。理由はPhantomJS自体をダウンロードして配置してるのですが、中身が階層化されるように変更されたのか、取得できないという。。。(Windowsしか試していません。)

説明下手ですね。
geb-example-gradle\build\webdriver\phantomjs\phantomjs.exe」を期待しているのに、
geb-example-gradle\build\webdriver\phantomjs\bin\phantomjs.exe」となるので失敗します。

多分この辺りなので、余裕のある方はプルリクでも出せば取り込まれるような気がします。

更に余談

Geb自体も過去に「org.codehaus.geb」から「org.gebish」にグループIDが変更されていてハマった事があったりします。こういうのって誰もがハマるものなんですかね。