2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

テスト自動化の便利ツール

Last updated at Posted at 2019-10-05

はじめに

最近RPAは流行っているようですね。テスト自動化もRPAの範疇にあるかと思います。
テスト自動化にいろんなツールがありますが、無料で使えるツールは

  • Selenium
  • RobotFramework

などは有名です。

Seleniumとは

Seleniumはいくつかのツールで構成したテストシステムです。
image.png

WebDriver

ライブラリ導入

build.gradle
// 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起動してテストしてみる

Selenium.java
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.java
// 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/

使ってみる

・起動
image.png

・Create a new project
image.png

・プロジェクトの画面
image.png

・レコーディング
image.png

START RECORDINGボタンを押すと、画面操作を行う。停止するとプロジェクトに反映されます。
image.png

・再生
image.png

・再生結果:問題なく実行されました。
image.png

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

サンプル

以上

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?