Seleniumをラップし、より洗練された記述ができるというFluentLeniumで環境を構築してみました。
mavenのインストール
Macでの構築手順
1.mavenのバージョン確認
$ brew search maven maven maven-shell homebrew/completions/maven-completion homebrew/versions/maven31 homebrew/versions/maven2 homebrew/versions/maven32 homebrew/versions/maven30
2.mavenのインストール
1で見つかったもののうち任意のバージョンをインストール
$ brew install homebrew/versions/maven32 ==> Tapping homebrew/versions Cloning into '/usr/local/Library/Taps/homebrew/homebrew-versions'... remote: Counting objects: 250, done. remote: Compressing objects: 100% (237/237), done. remote: Total 250 (delta 24), reused 106 (delta 13), pack-reused 0 Receiving objects: 100% (250/250), 267.60 KiB | 0 bytes/s, done. Resolving deltas: 100% (24/24), done. Checking connectivity... done. Tapped 247 formulae (271 files, 1.5M) ==> Installing maven32 from homebrew/homebrew-versions ==> Downloading https://www.apache.org/dyn/closer.cgi?path=maven/maven-3/3.2.5/b ==> Best Mirror http://ftp.meisei-u.ac.jp/mirror/apache/dist/maven/maven-3/3.2.5 ######################################################################## > 100.0% 🍺 /usr/local/Cellar/maven32/3.2.5: 85 files, 9.1M, built in 5 seconds ```
3.バージョン確認
インストールが成功しているかバージョンを確認
$ mvn --version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-15T02:29:23+09:00)
Javaプロジェクト作成
1.mvnのプロジェクト作成
下記のコマンドでmvnのプロジェクト作成、groupID、artifactId は任意のもので
mvn archetype:generate -DgroupId=_sample_ -DartifactId=_sample_fluentlenium_ -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2.pomの編集
pomにJUnitとFluentLeniumを組み込んでやる。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample</groupId>
<artifactId>sample_fluentlenium</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sample_fluentlenium</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-core</artifactId>
<version>0.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-assertj</artifactId>
<version>0.10.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
テストコード作成
FluentLeniumでは「ページオブジェクトパターン」と呼ばれる1ページ毎のオブジェクトを作成してテストコードに組み込んでいくことがベストプラクティスの様です。ここでは、実際にページオフジェクトを作成してテストに組み込んでみます。
ページオブジェクト
import org.fluentlenium.core.FluentPage;
/**
* 検索画面を表すPage Objectクラス。
*/
public class BingPage extends FluentPage {
@Override
public String getUrl(){
return "http://www.bing.com";
}
/*
* 検索処理実行
*/
public void search(String keyword) {
fill("#sb_form_q").with(keyword);
submit("#sb_form_go");
}
}
テストコード
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import org.fluentlenium.adapter.FluentTest;
import org.fluentlenium.core.annotation.Page;
public class BingTest extends FluentTest {
@Page
public BingPage page;
@Test
public void title_of_bing_should_contain_search_query_name() {
goTo(page);
page.search("FluentLenium");
assertThat(title()).contains("FluentLenium");
}
}
テスト実行
下記のコマンドでテスト実施
$ mvn test
成功時は[BUILD SUCCESS]が失敗時は[BUILD FAILURE]が表示されます。