テスト自動化について
今回は、Webの試験自動化に向けて最近調べたことを整理していみたいと思います。
まず、Webの試験自動化に向けてですが、幾つかのツールがあるので、 紹介しておきたい思います。私が知っているものや調査して見つけたものを記載しておりますので、 他にもあるとかコメントがあれば通りすがりにコメントを残してもらえると助かります。
ツールの紹介
まず、Webの試験自動化に向けてですが、幾つかのツールがあるので、 紹介しておきたい思います。私が知っているものや調査して見つけたものを記載しておりますので、 他にもあるとかコメントがあれば通りすがりにコメントを残してもらえると助かります。
Selenium | 今回は、このツールを実際に試してみます。 |
Sikuli | スクリプトはjythonで記載します。 Webブラウザに限らず、デスクトップのそうさも行えるようです。 画像認識による評価ができるのがちょっといいかも |
Oracle Functional Testing OepnScritpt | 有償ツール、お金がある人はどうぞお試しあれ。 |
Selenium + WebDriver
いずれのツールも特徴はきっとあるのですが、有償ツールなどは試せていないので、 ひとまず今回はOSSのSeleniumにターゲットを移して試してみたいと思います。Seleniumをダウンロード
サイトからダウンロードする場合は以下のサイトをどうぞhttp://www.seleniumhq.org/download/
pom.xmlで取得する場合は、以下の記載でダウンロードできます。
This file contains hidden or 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
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-chrome-driver</artifactId> | |
<version>2.53.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>htmlunit-driver</artifactId> | |
<version>2.21</version> | |
</dependency> |
Google Chrome Driver
BtoBの実行をChrome Browserで実行するためのドライバをダウンロードします。以下のサイトから
![]() |
送信者 2016年6月26日 |
![]() |
送信者 2016年6月26日 |
EclipseからJUnitのテストケースを作成する。
さあ、準備は整いましたので、テストプログラムを作成してみます。 このサンプルーコードでは、以下の操作を自動化しています。- Googleのページを起動
- 検索キーワードを入力し、Googleで検索を実施
- 検索結果からリンクを探し出して、クリックする。
This file contains hidden or 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
/* | |
* (C) 2016 Akitoshi Tada | |
*/ | |
package selenium.test; | |
import java.io.File; | |
import java.io.IOException; | |
import org.apache.commons.io.FileUtils; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.OutputType; | |
import org.openqa.selenium.TakesScreenshot; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
public class ChromeTest { | |
private WebDriver driver = null; | |
@Before | |
public void createDriver() { | |
// ダウンロードしたchromedriverの場所を指定します。 | |
System.setProperty("webdriver.chrome.driver", "/Users/akechi/bin/SeleniumHQ-WebDriver/chromedriver"); | |
driver = new ChromeDriver(); | |
} | |
@After | |
public void quitDriver() { | |
driver.quit(); | |
} | |
@Test | |
public void testGoogleSearch() throws InterruptedException, IOException { | |
// googleのページを起動 | |
driver.get("http://www.google.com/xhtml"); | |
Thread.sleep(1000); | |
// 検索の実施 | |
WebElement searchBox = setElementById("q", "akechiのIT技術者日誌"); | |
searchBox.submit(); | |
Thread.sleep(1000); | |
driver.findElement(By.linkText("akechiのIT技術者日誌")).click(); | |
Thread.sleep(1000); | |
snapshot(); | |
} | |
private void snapshot() throws IOException { | |
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); | |
FileUtils.copyFile(file, new File("/Users/akechi/bin/test.jpeg")); | |
} | |
private WebElement setElementById(String id, String value) { | |
WebElement searchBox = driver.findElement(By.name(id)); | |
searchBox.sendKeys(value); | |
return searchBox; | |
} | |
} |