テスト自動化(5) Selenium Grid

みなさん こんにちは。
ヒューマンクレスト浅黄です。

テスト自動化を行った時、テストケースの増加によって、実行時間が長くなります。特に、クロスブラウザテストでは、倍々で増えていきます。
UI周りのテストでSeleniumを利用している場合、テストケースを並行(並列,分散)実行させることによって実行時間の増加傾向を抑える一助になるのが、Selenium Gridです。

今回は、Selenium Gridの環境構築について。
イメージは以下の図のような環境を構築し、テストスクリプトをGrid Server宛にすることにより複数の環境で並行したテストが可能になります。

Selenium Grid 環境
Selenium Grid 環境

(※上図以外では、並行実行のため、タスクスケジューラ(Windows),Cron(linux), Jenkinsなども必要になります)

既にテストスクリプトがありローカルPCでの実績があるならば、以下の手順で簡単にGrid化できます。

  1. Selenium Grid Server を立てる
  2. Selenium Nodeを追加する
  3. テストスクリプトをRemoteWebDriverに書き換える

1. Selenium Grid Serverを立てる

java -jar selenium-server-standalone-2.39.0.jar -role hub
    ※Grid ServerのIPアドレス:192.168.2.10

2. Selenium Nodeを追加する

java -jar selenium-server-standalone-2.39.0.jar -role node -nodeConfig node.json -Dwebdriver.ie.driver=C:\temp\lib\IEDriverServer.exe -Dwebdriver.chrome.driver=C:\temp\lib\chromedriver.exe
    IEやchromeを同時に利用する場合は、以下の引数を含めます。

  • IE利用:-Dwebdriver.ie.driver=C:\temp\lib\IEDriverServer.exe
  • chrome利用:-Dwebdriver.chrome.driver=C:\temp\lib\chromedriver.exe
  • ※パスは、実環境にあわせる

必要なブラウザ毎の情報は、引数にしているnode.jsonに記載します。

node.json

{
  "capabilities":
      [
        {
          "browserName": "firefox",
          "maxInstances": 2,
          "version": "26.0",
          "seleniumProtocol": "WebDriver"
        },
        {
          "browserName": "chrome",
          "maxInstances": 2,
          "version": "31.0",
          "seleniumProtocol": "WebDriver"
        },
        {
          "platform": "WINDOWS",
          "browserName": "internet explorer",
          "maxInstances": 1,
          "version": "10",
          "seleniumProtocol": "WebDriver"
        }
      ],
  "configuration":
  {
    "hub": "https://192.168.2.10:4444/grid/register"
  }
}

Selenium Grid Server Consoleは以下のようになります。

Grid Console node1

また、ブラウザのバージョン毎にクライアントPCを別にする場合、
他のPCにて、node.jsonの内容を書き換えて、Nodeを追加します。

{
  "capabilities":
      [
        {
          "browserName": "chrome",
          "maxInstances": 2,
          "version": "32.0",
          "seleniumProtocol": "WebDriver"
        },
        {
          "platform": "WINDOWS",
          "browserName": "internet explorer",
          "maxInstances": 1,
          "version": "9",
          "seleniumProtocol": "WebDriver"
        }
      ],
  "configuration":
  {
    "hub": "https://192.168.2.10:4444/grid/register"
  }
}

Grid Console node 2

また、スマートフォン(AndroidやiPhone)を追加すると Grid Consoleは以下のようになります。

Grid Console node sp

3.環境にあわせてテストスクリプトを書き換える。
サンプルスクリプト  Selenium 2 WebDriverの場合

package org.openqa.selenium.example;

import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) throws Exception {

//        WebDriver driver = new FirefoxDriver();
    	DesiredCapabilities capability = DesiredCapabilities.firefox();
    	capability.setVersion("26.0");
    	WebDriver driver = new RemoteWebDriver(new URL("https://192.168.2.10:4444/wd/hub"), capability);

        driver.get("https://www.google.co.jp");

        WebElement element = driver.findElement(By.name("q"));

        element.sendKeys("Cheese!");

        element.submit();

        System.out.println("Page title is: " + driver.getTitle());
        
        driver.quit();
    }
}

16行目:DesiredCapabilities capability = DesiredCapabilities.firefox();
ターゲットになるブラウザの種類に合わせて編集します。

  • firefoxの場合:DesiredCapabilities.firefox()
  • chromeの場合:DesiredCapabilities.chrome()
  • IEの場合:DesiredCapabilities.InternetExplorer()
  • androidの場合:DesiredCapabilities.android()
  • iphoneの場合:DesiredCapabilities.iphone()

また、

    	DesiredCapabilities capa = new DesiredCapabilities();
    	capa.setBrowserName("firefox");

のように書くことも可能です。

17行目:capability.setVersion(“26.0”);
バージョンまで指定したい場合に設定します。
但し、ブラウザとバージョンの組み合わせがない場合、エラーとなります。
バージョンを指定しない場合、Grid Serverが勝手にブラウザの設定からNodeを選択します。

18行目:WebDriver driver = new RemoteWebDriver(new URL(“https://192.168.2.10:4444/wd/hub”), capability)
RemoteWebDriverに、ブラウザ(バージョン)を設定したDesiredCapabilitiesを引数にセットする。

さらに、一つのスクリプトで複数環境に対応する場合、ブラウザ種類やバージョンを外部パラメータ化すると便利になります。

今回の一連の環境構築において、設定するIPアドレスは、Grid ServerのIPのみです。
仮にクライアントPCが壊れたり、WindowsUpdate中でレスポンスが悪いなどの理由により、クライアントPCを切り離す場合でも、違うPCにNodeの設定を行えば、すぐに同一環境が構築できます。テストスクリプトに手を入れる必要もありません。

最後にテストケースを並行実行した場合、操作やチェック項目の整合性、テストデータの整合性が保たれるか? 検討が必要になると思います。
例えば、1つのケースでは、「○○を追加」しているのに、他方では、「○○の削除」を操作、同時にテストするとどちらか一方でエラーになる。そのようなことがないように、並行実行の可否、実行順序やテストデータの生成方法等を検討します。しかしテスト設計からやり直すと、スクリプトにも手を加える必要がある場合もでてきます。

様々な制限があると思いますが、Selenium Grid を活用するこにより、正確にテスト実行しつつ、より早く終わる(テストの生産性が高い!)テスト自動化を目指すことができるのではないでしょうか。

このエントリーをはてなブックマークに追加

3件のコメント

ただいまコメントは受け付けていません。