Pitaliumで画面遷移してからスクリーンショットを撮影する
PitaliumはSeleniumをベースにしていますので、クリックイベントを使って画面遷移をしたり、フォームにデータをポストすることができます。
通常の画面遷移であれば次のように処理を書きます。
import org.junit.Test;
import org.openqa.selenium.WebElement;
import com.htmlhifive.pitalium.core.PtlTestBase;
public class SampleTest extends PtlTestBase {
@Test
public void testClickAndCapture() throws Exception {
// 1. hifiveサイトのトップページを開きます
// 2. hifiveサイトのトップページのスクリーンショットを撮影します。
assertionView.assertView(“OpenhifiveTopPage”);
// 3. “過去のお知らせ一覧” ボタン要素を取得してクリックします。
WebElement infoHistoryButton = driver.findElementByCssSelector(“#news a.btn”);
infoHistoryButton.click();
// 4. ページ遷移後のスクリーンショットを撮影します。
assertionView.assertView(“OpenNewsListPage”);
}
}
クリックされるDOM(#news a.btn)を取得し、そのクリックイベントを実行します。
フォームへのポストの場合、次のように記述します。
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 testInputAndSubmitAndCapture() throws Exception {
// 1. hifiveサイトのトップページを開きます
// 2. hifiveサイトのトップページのスクリーンショットを撮影します。
assertionView.assertView(“OpenTopPage”);
// 3. 画面右上の検索ボックスに入力して、サイト検索を実行します。
WebElement form = driver.findElementByCssSelector(“#globallinks form”);
WebElement searchBox = form.findElement(By.id(“headerglobalsearchinput”));
searchBox.sendKeys(“test search”);
form.submit();
// 4. ページ遷移後のスクリーンショットを撮影します。
assertionView.assertView(“OpenSearchResultPage”);
}
}
これはAjaxでも同じように行うことができます。
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.htmlhifive.pitalium.core.PtlTestBase;
import com.htmlhifive.pitalium.core.selenium.PtlWebDriverWait;
public class PtlSampleTest extends PtlTestBase {
@Test
public void testExclude() throws Exception {
// 1. hifiveサイトのトップページを開きます
PtlWebDriverWait wait = new PtlWebDriverWait(driver, 30);
// ロードするまで待つ
wait.untilLoad();
// 2. hifiveサイトのトップページのスクリーンショットを撮影します。
assertionView.assertView(“OpenTopPage”);
// 3. 画面上のテキストボックスにタスクを入力します
WebElement form = driver.findElementByCssSelector(“#todoRegForm”);
WebElement searchBox = form.findElement(By.id(“txtTodo”));
searchBox.sendKeys(“テスト”);
form.submit();
wait.untilLoad();
// 4. タスク登録後の画面をスクリーンショットに残します。
assertionView.assertView(“AddTaskDonePage”);
}
}
Seleniumの知識があれば、テストは自由に何度も繰り返しできるようになります。ぜひやり方を覚えてPitaliumと合わせて使ってみてください。
Pitalium(hifiveリグレッションテストライブラリ) – hifive
コメントは受け付けていません。