はじめに
最近RPAは流行っているようですね。テスト自動化もRPAの範疇にあるかと思います。
テスト自動化にいろんなツールがありますが、無料で使えるツールは
- Selenium
- RobotFramework
などは有名です。
Seleniumとは
Seleniumはいくつかのツールで構成したテストシステムです。
WebDriver
ライブラリ導入
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
// https://mvnrepository.com/artifact/commons-io/commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.6'
ローカルのWebDriver起動してテストしてみる
package selenium;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
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 Selenium {
private static DateFormat DF = new SimpleDateFormat("yyyyMMdd HHmmssSSS");
public static void main(String[] args) throws Exception {
// http://chromedriver.chromium.org/downloads
// ドライバを設定
System.setProperty("webdriver.chrome.driver", "/selenium/76_driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// ブラウザ起動して、yahooをアクセスする
driver.get("https://www.yahoo.co.jp/");
// yahooのトップページのエビデンスを作成する
makeEvidence(driver);
// キーワード入力欄の要素を取得する
WebElement searchBox = driver.findElement(By.name("p"));
// キーワード「blockchain」を入力する
searchBox.sendKeys("blockchain");
// キーワードを入力した状態のエビデンスを取得する
makeEvidence(driver);
// 検索を行う
searchBox.submit();
// 検索結果のエビデンスを作成する
makeEvidence(driver);
// ブラウザを閉じる
driver.quit();
}
/**
* エビデンスを作成する
*
* @param webdriver Webドライバ
* @throws Exception
*/
public static void makeEvidence(WebDriver webdriver) throws Exception {
String fileName = "c:/data/evidence/" + DF.format(new Date()) + ".png";
File src = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE);
File dest = new File(fileName);
// ファイルを移動
FileUtils.moveFile(src, dest);
}
}
リモートのWebDriver起動してテストしてみる
Selenium Gridサービスを構築して、リモートサーバにてテストできます。
Docker、jenkinsでもSelenium Gridサービスを簡単に作成できます。
// Selenium Gridがある場合はリモートサーバで実行できます。
// Jenkinsの場合はwebdriverの設定が必要です。
DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
//chromeCapabilities.setVersion("78");
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), chromeCapabilities);
WebDriverのバージョンと実際インストールしたバージョンが合わない場合は下記のエラーが出ます。
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 78
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
${JENKINS_HOME}/Jenkins/tools/chromedriverのドライバーを差し替えで対応する必要があります。
参考URL:https://wiki.jenkins.io/display/JENKINS/Selenium+Plugin
Selenium IDE
インストール
Chrome: https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd?hl=ja
Firefox: https://addons.mozilla.org/ja/firefox/addon/selenium-ide/
使ってみる
START RECORDINGボタンを押すと、画面操作を行う。停止するとプロジェクトに反映されます。
RobotFrameworkとは
Robot Framework is a generic open source automation framework for acceptance testing, acceptance test driven development (ATDD), and robotic process automation (RPA).
インストール
pip install robotframework
実行例
robot QuickStart.rst
API
サンプル
以上