前の記事で WebDriverManager を理解したので、gradleでテストに以下のように書いた。
(1) の testCompile で依存の webdrivermanager を読み込み、
(2) で FirefoxDriverManager を import して、
(3) の @BeforeClass で セットアップする。
build.gradle
// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.7'
testCompile 'io.github.bonigarcia:webdrivermanager:1.+' // (1)
testCompile 'junit:junit:4.+'
testCompile 'org.gebish:geb-core:1.+'
testCompile 'org.seleniumhq.selenium:selenium-firefox-driver:3.+'
testCompile ('org.seleniumhq.selenium:selenium-support:3.+'){
exclude module : 'groovy-all'
}
}
test.maxParallelForks = 4
src/test/groovy/MyWebTest1.groovy
import org.junit.*
import geb.Browser
import org.openqa.selenium.Dimension
import org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.firefox.FirefoxDriver
// https://github.com/bonigarcia/webdrivermanager
import io.github.bonigarcia.wdm.FirefoxDriverManager // (2)
class MyWebTest1 {
private Browser browser
@BeforeClass
public static void setupClass() {
FirefoxDriverManager.getInstance().setup() // (3)
}
@Before
void setUp() {
DesiredCapabilities capabilities = DesiredCapabilities.firefox()
capabilities.setCapability("marionette", true)
capabilities.setCapability("timeoutInSeconds", (long)10)
browser = new Browser(driver:new FirefoxDriver(capabilities))
browser.driver.manage().window().size = new Dimension(1200,900)
... 以下略 ...
でもこれだと、テストクラスを実行するごとにドライバのセットアップが行われて無駄な上に、
並列実行すると(まだ ~/.m2 以下に geckodriver がない場合に)以下のエラーが出るようになった。
FileNotFoundException ?
ターミナル画面
MyWebTest1 > classMethod FAILED
java.lang.RuntimeException at MyWebTest1.groovy:17
Caused by: java.io.FileNotFoundException at MyWebTest1.groovy:17
MyWebTest2 > classMethod FAILED
java.lang.RuntimeException at MyWebTest2.groovy:17
Caused by: java.io.FileNotFoundException at MyWebTest2.groovy:17
MyWebTest4 > アイテム作成 FAILED
geb.waiting.WaitTimeoutException at MyWebTest4.groovy:46
MyWebTest3 > アイテム作成 FAILED
geb.waiting.WaitTimeoutException at MyWebTest3.groovy:46
4 tests completed, 4 failed
:test FAILED
並列で同時に driver をダウンロードをしようとして失敗してるっぽい。
build.gradle 内で geckodriver を設定して各テストに渡せるように、以下のように変更した。
(1) で build.gradle 内で使う依存の webdrivermanager を読み込み、
(2) でテスト実行前に geckodriver を setup する。
(3) で 設定を各テストクラスに渡す
(4) で ドライバのパスを確認
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
// https://github.com/bonigarcia/webdrivermanager
classpath 'io.github.bonigarcia:webdrivermanager:1.+' // (1)
}
}
// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.7'
testCompile 'junit:junit:4.+'
testCompile 'org.gebish:geb-core:1.+'
testCompile 'org.seleniumhq.selenium:selenium-firefox-driver:3.+'
testCompile ('org.seleniumhq.selenium:selenium-support:3.+'){
exclude module : 'groovy-all'
}
}
tasks.withType(Test) {
io.github.bonigarcia.wdm.FirefoxDriverManager.getInstance().setup() // (2)
systemProperties System.properties // (3)
}
test.maxParallelForks = 4
src/test/groovy/MyWebTest1.groovy
import org.junit.*
import geb.Browser
import org.openqa.selenium.Dimension
import org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.firefox.FirefoxDriver
class MyWebTest1 {
private Browser browser
@Before
void setUp() {
println "driver path ----- ${System.getProperty('webdriver.gecko.driver')}" // (4)
DesiredCapabilities capabilities = DesiredCapabilities.firefox()
capabilities.setCapability("marionette", true)
... 以下略 ...
build.gradle の buildscript ブロックは、 build.gralde 自身の依存関係を書くのに使う。
ひとつ勉強になった。