LoginSignup
5
3

More than 5 years have passed since last update.

Selenium WebDriverでテスト作業の効率化を図る

Last updated at Posted at 2017-03-22

はじめに

既存システムの保守作業の中で、
シナリオテストなど業務に沿った検証を行うために
既存のWebアプリケーションを実行しテストデータを作成する場合がある。

そこで頻繁にテストデータ作成に使われるWebアプリケーションについては
Selenium WebDriverでブラウザ操作を再現するテストコードを用意し、
都度の手作業によるブラウザ操作を削減する。

実行環境について

今回はSelenium WebDriverをJunit上で起動させるケースについて記載します。
使用するブラウザはInternet Explorerです。

テストメソッドの実行前後処理について

  • setUpBeforeClass

一番最初のテストメソッド実行前(setUp()よりも前)に呼ばれるメソッド

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    System.setProperty("webdriver.ie.driver", "./driver/IEDriverServer.exe");
}
  • setUp

各テストメソッドの実行前に呼ばれるメソッド

@Before
public void setUp() throws Exception {
}
  • tearDown

各テストメソッドの実行後に呼ばれるメソッド

@After 
public void tearDown() throws Exception {
}
  • tearDownAfterClass

一番最後のテストメソッド実行後(tearDown()よりも後)に呼ばれるメソッド

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

ブラウザで操作する要素の指定方法について

要素の指定方法はID、cssセレクタなどありますが、今回はxpathで指定する方法を記載します。

xpathの調べ方

Google Chromeデベロッパーツールを使ってxpathを調べる場合

1.Google Chromeでxpathを調べたいWebページを表示する。
2.(動作環境がWindowsの場合)F12キーを押下する。
3.下記スクリーンショットのようにデベロッパーツールが表示されるので
  Webページ上の要素をマウスを使って選択する。
4.右クリックメニューからCopyXpathをクリックするとXpathを取得できる。

image

よく使うブラウザ操作

  • テキストフィールドの入力
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

WebDriver driver = new InternetExplorerDriver();
WebElement element = driver.findElement(By.xpath("操作対象のxpath"));
element.sendKeys("入力したい文字列");
  • プルダウンの選択
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

WebDriver driver = new InternetExplorerDriver();
Select element = new Select(driver.findElement(By.xpath("操作対象のxpath")));
element.selectByVisibleText("選択したい選択肢の文字列");
  • クリック
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

WebDriver driver = new InternetExplorerDriver();
driver.findElement(By.xpath("操作対象のxpath")).click();
  • クリア
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

WebDriver driver = new InternetExplorerDriver();
driver.findElement(By.xpath("操作対象のxpath")).clear();
  • ウィンドウの切り替え
import java.util.Set;
import org.openqa.selenium.WebDriver;

// XXX入力画面から@@@選択画面へ
WebDriver driver = new InternetExplorerDriver();
String currentWindowHandle = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();

windowHandles = driver.getWindowHandles();
windowHandles.remove(currentWindowHandle);
driver.switchTo().window(windowHandles.iterator().next());

//指定した画面側の操作

// XXX入力画面の処理へ戻る
driver.switchTo().window(currentWindowHandle);
  • フレームの選択
import org.openqa.selenium.WebDriver;

// 指定するフレームへ
WebDriver driver = new InternetExplorerDriver();
driver.switchTo().frame("操作したいフレームのname");
  • 操作したい画面の表示
import org.openqa.selenium.WebDriver;

WebDriver driver = new InternetExplorerDriver();
driver.get("操作したい画面のURL");
5
3
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
5
3