PitaliumがMaven対応したのでDockerと組み合わせて使ってみる
Pitaliumはこれまでjarファイルをダウンロードして自分でセットアップする必要がありましたが、先日Mavenのリポジトリに登録されました!これで導入の手間が大幅に軽減されるはずです。
そこで今回はごく基本的なPitalium導入法に加えて、Dockerを使ってSelenium Hub/Nodeを簡単に組み合わせる方法を紹介します。
要件
まず以下の環境はあることとします。
- Docker
- Gradle
Dockerの実行
Dockerはすでにインストールされている前提とします。Selenium Hub/NodeはDockerコンテナイメージのサイズが大きいので、ダウンロードに時間がかかります。そこで、とりあえず実行しておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker run -d -p 4444:4444 –name selenium-hub selenium/hub | |
$ docker run -d -v /dev/urandom:/dev/random -P –link selenium-hub:hub selenium/node-chrome-debug |
これでどちらも実行されればSelenium Hub/Nodeが立ち上がって、Chromeが使える状態になります。
ディレクトリ構成
最低限のディレクトリ構成は次のようになります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ tree . | |
. | |
├── build.gradle | |
└── src | |
├── main | |
│ └── resources | |
│ └── capabilities.json | |
└── test | |
└── java | |
└── pitaliumtest | |
└── SampleTest.java | |
6 directories, 3 files |
capabilities.jsonはHubのブラウザ設定を記述しますが、この時点ではChromeのみなので次のようになります。IEやFirefoxなど必要に応じて追加してください。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"browserName": "chrome" | |
} | |
] |
build.gradleは次のようになります。ここでPitaliumを指定するだけです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply(plugin: 'java') | |
apply plugin: 'application' | |
version = '0.0.1-SNAPSHOT' | |
sourceCompatibility = 1.8 | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile group: 'com.htmlhifive', name: 'pitalium', version: '1.2.1' | |
} |
SampleTest.javaは今回は次のように記述しました。03.基本的なテストコードの書き方 – hifiveのままですが、Dockerで立ち上がるChromeは英語版になっていますので、アクセス先を日本語版指定としています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.hamcrest.CoreMatchers.*; | |
import static org.hamcrest.MatcherAssert.*; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebElement; | |
import com.htmlhifive.pitalium.core.PtlTestBase; | |
public class SampleTest extends PtlTestBase { | |
@Test | |
public void test() throws Exception { | |
// 1. hifiveサイトのトップページを開きます | |
driver.get("http://www.htmlhifive.com/conts/web/view/Main/?language=ja"); | |
// 2. hifiveサイトのトップページのスクリーンショットを撮影します | |
assertionView.assertView("OpenhifiveTopPage"); | |
// 3. "#about" に表示されているタイトルが正しいことをチェックします | |
WebElement about = driver.findElementById("about"); | |
WebElement title = about.findElement(By.tagName("h2")); | |
assertThat(title.getText(), is("hifiveとは")); | |
} | |
@Test | |
public void testClickAndCapture() throws Exception { | |
// 1. hifiveサイトのトップページを開きます | |
driver.get("http://www.htmlhifive.com/conts/web/view/Main/?language=ja"); | |
// 2. hifiveサイトのトップページのスクリーンショットを撮影します。 | |
assertionView.assertView("OpenhifiveTopPage"); | |
// 3. "過去のお知らせ一覧" ボタン要素を取得してクリックします。 | |
WebElement infoHistoryButton = driver.findElementByCssSelector("#news a.btn"); | |
infoHistoryButton.click(); | |
// 4. ページ遷移後のスクリーンショットを撮影します。 | |
assertionView.assertView("OpenNewsListPage"); | |
} | |
} |
実行する
後はテストを実行するだけです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gradle test |
しばらく待つと結果が表示されます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gradle test | |
:compileJava UP-TO-DATE | |
:processResources UP-TO-DATE | |
:classes UP-TO-DATE | |
:compileTestJava | |
:processTestResources UP-TO-DATE | |
:testClasses | |
:test | |
BUILD SUCCESSFUL | |
Total time: 1 mins 11.666 secs |
そしてJUnitのファイルも生成されています。
Pitaliumのテストファイルも生成され、スクリーンショットも保存されています。
Dockerコンテナイメージがダウンロードできるまでに若干の時間がかかりますが、それさえ終われば環境を整えるのはとても簡単です。ぜひPitaliumを使ってWebブラウザベースのビジュアルテスティングを推進してください!
コメントは受け付けていません。