gradleでselenium
Gradleを使ってSeleniumベースなテストをサーバー起動を含め自動化しちゃいたい場合のメモ
テストケース (SampleTestCase.java)
package sample.test.integration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import static org.junit.Assert.*;
public class SampleTestCase {
private Selenium selenium;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium(
"localhost",
4444,
"*chrome",
"http://localhost:8081"
);
selenium.start();
}
@Test
public void testIndex() {
selenium.open("/");
assertEquals("test", selenium.getTitle());
}
@After
public void tearDown() {
selenium.stop();
}
}
build.gradle
(ベースは以前作ったSAStrutsのプロジェクトベース)
buildDir = "target"
apply {
plugin "war"
plugin "jetty"
plugin "eclipse"
}
repositories {
mavenCentral()
maven {
url "http://maven.seasar.org/maven2"
}
}
configurations {
selenium
}
dependencies {
compile "org.seasar.sastruts:sa-struts:1.0.4-sp9"
compile "org.apache.geronimo.specs:geronimo-ejb_3.0_spec:1.0"
compile "org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.0"
compile "org.apache.geronimo.specs:geronimo-jpa_3.0_spec:1.0"
compile "mysql:mysql-connector-java:5.1.21"
testCompile "org.seleniumhq.selenium:selenium-java:2.28.0"
selenium "org.seleniumhq.selenium:selenium-server:2.28.0"
}
sourceSets {
main {
output.classesDir = "${buildDir}/classes"
output.resourcesDir = "${buildDir}/classes"
}
test {
output.classesDir = "${buildDir}/test-classes"
output.resourcesDir = "${buildDir}/test-classes"
}
}
[jettyRun, jettyRunWar]*.contextPath = ""
[jettyRun, jettyRunWar]*.httpPort = 8081
jettyStop.stopPort = 8081
jettyStop.stopKey = "stopKey"
test {
// integrationパッケージは通常テストから除外
exclude "**/integration/**"
}
task integrationTest(type: Test) {
include "**/integration/**"
doFirst {
// integrationTest前にJettyをデーモンモードで起動
jettyRun.daemon = true
jettyRun.execute()
}
doLast {
// 終了後にJettyサーバーを停止
jettyStop.execute()
}
}
// Selenium ServerをintegrationTest時に起動する
task startSeleniumServer {
ant.java(
classname: "org.openqa.selenium.server.SeleniumServer",
classpath: configurations.selenium.asPath,
fork: true,
spawn: true
)
}
integrationTest.dependsOn startSeleniumServer
eclipse {
project {
natures "org.springsource.ide.eclipse.gradle.core.nature"
}
classpath.file {
whenMerged { classpath ->
classpath.entries.findAll { entry ->
entry.kind == "output"
}*.path = "target/classes"
}
}
}
gradle integrationTestを行う事でSelenium Server及びJettyサーバーを起動してテストを実行、その後サーバーをシャットダウン(但し、Seleniumサーバーはダウンしない)
な感じでテスト出来る模様。