tanaka's Programming Memo

プログラミングについてのメモ。

PHPUnit_Selenium 書籍のJavaのサンプルをPHPUnitで書く

実践 Selenium WebDriver

実践 Selenium WebDriver

とても分かりやすい良書。JavaからWebDriverを操作する方法がすらすら分かる。ここでは解説されているコードをPHPUnitでどのように表記するかをまとめる。

PHPUnit/Seleniumのドキュメント

注意

本サンプルは2014/10/6時点のものである。GoogleのIDなどは変更されることがあるようで、動作するタイミング次第ではサンプルが最後まで動作しない可能性が考えられる。テストが失敗する場合は、ID名などの不一致を確認してほしい。

1章 WebDriverとWebElementの紹介

最初のコード(p11)

以下で、Firefoxを起動して、Googleのページを表示させることができる。

<?php
class WebDriverTest extends PHPUnit_Extensions_Selenium2TestCase
{
	protected function setUp()
	{
		$this->setBrowser('firefox');
		$this->setBrowserUrl('http://www.google.co.jp');
	}

	public function test12()
	{
		$this->url('http://www.google.co.jp');
	}
}
?>

PHPUnitでは、環境を構築するためのsetUp()が必須になる。setUp()内でSelenium Serverセッションの設定を行う。詳細はhttps://phpunit.de/manual/current/ja/selenium.html#selenium.selenium2testcase

1.3 WebElement(p13)

1.3.1.1 WebDriverでのWebElementの指定

FirefoxからGoogleのページを開き、検索ボックスに文字を入力して実行するコードは以下の通り。

<?php
class WebDriverTest extends PHPUnit_Extensions_Selenium2TestCase
{
	protected function setUp()
	{
		$this->setBrowser('firefox');
		$this->setBrowserUrl('http://www.google.co.jp');
	}

public function test1311()
	{
		$this->url('http://www.google.co.jp');
		$searchBox = $this->byName('q');
		$searchBox->value('Packt Publishing');
		$searchBox->submit();
	}
}
?>
1.3.1.2 findElements()メソッド

findElements()には、elements()が対応。以下、inputタグを全て取り出すサンプルコード。tag以外の取り出し方は未調査。

<?php
class WebDriverTest extends PHPUnit_Extensions_Selenium2TestCase
{
	protected function setUp()
	{
		$this->setBrowser('firefox');
		$this->setBrowserUrl('http://www.google.co.jp');
	}

	public function test1312()
	{
		$this->url('http://www.google.co.jp');
		$input = $this->elements($this->using('css selector')->value('input'));
		$this->assertEquals(10,count($input));
		$this->assertEquals('input',$input[0]->name());
	}
}
?>
1.3.1.4 Byによる指定

抜き出し方を列挙したコードは以下の通り。タグ名を取り出している。linkだけはlinkの文章を取り出し。

<?php
class WebDriverTest extends PHPUnit_Extensions_Selenium2TestCase
{
	protected function setUp()
	{
		$this->setBrowser('firefox');
		$this->setBrowserUrl('http://www.google.co.jp');
	}
	
	public function test1314()
	{
		$this->url('http://www.google.co.jp');
		// By.name()
		$this->assertEquals('input',$this->byName('btnK')->name());
		// By.id()
		$this->assertEquals('div',$this->byId('viewport')->name());
		// By.tagName()
		$this->assertEquals('button',$this->byTag('button')->name());
		// By.className()
		$this->assertEquals('div',$this->byClassName('ctr-p')->name());
		// By.linkText()
		$this->assertEquals('Googleについて',$this->byLinkText('Googleについて')->text());
		// By.partialLinkText()
		$this->assertEquals('Googleについて',$this->byPartialLinkText('Google')->text());
		// By.xpath()
		$this->assertEquals('div',$this->byXPath("//*[@id='viewport']")->name());
		// By.cssSelector()
		$this->assertEquals('div',$this->byCssSelector('#viewport')->name());
	}
}
?>
1.3.2 WebElement上でのアクション
<?php
class WebDriverTest extends PHPUnit_Extensions_Selenium2TestCase
{
	protected function setUp()
	{
		$this->setBrowser('firefox');
		$this->setBrowserUrl('http://www.google.co.jp');
	}
	
	public function test132()
	{
		$this->url('http://www.google.co.jp');
		$input = $this->byId('lst-ib');

		// getAttribute()
		$this->assertEquals('q',$input->attribute('name'));
		$this->assertEquals('lst-ib',$input->attribute('id'));
		$this->assertEquals('gsfi lst-d-f',$input->attribute('class'));
		
		// sendKeys() / clear()
		//// 入力ボックス(テキストエリアも同様)を選択
		$input->value("");
		//// キー入力
		$this->keys("packt publishing");
		$this->assertEquals('packt publishing',$input->attribute('value'));
		//// 入力ボックスをクリア
		$input->clear();
		//// 特殊キー入力
		$this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::SHIFT."packt ".PHPUnit_Extensions_Selenium2TestCase_Keys::SHIFT."publishing");
		$this->assertEquals('PACKT publishing',$input->attribute('value'));
		
		// 入力ボックスに直接文字入力
		$input->clear();
		$input->value("packt publishing");
		$this->assertEquals('packt publishing',$input->attribute('value'));

		// getCssValue()
		$this->assertEquals('arial,sans-serif',$input->css('font-family'));
		
		// getLocation()
		$this->assertGreaterThan(0,$input->location()['x']);
		$this->assertGreaterThan(0,$input->location()['y']);
		
		// getSize()
		$this->assertGreaterThan(0,$input->size()['width']);
		$this->assertGreaterThan(0,$input->size()['height']);
		
		// getText()
		$this->assertEquals('Google 検索',$this->byName('btnK')->attribute('value'));
		
		// getTagName()
		$this->assertEquals('input',$input->name());
		
		// isDisplayed()
		$this->assertTrue($input->displayed());
		
		// isEnabled()
		$this->assertTrue($input->enabled());
		
		// isSelected()
		$this->assertFalse($input->selected());
	}
}
?>
keysの使い方

$this->keys(送りたい文字列)で、現在選択されているエレメントに文字を入力できる。

エレメントを選択するには、clear()でそのエレメントの文字を消したり、value("")としてその要素に空文字を追加する。

value()は差し替えではなく追加なので、value("")としてもその要素が消えることはない。

getText()

書籍ではgetText()でボタンなどに表示されている文字が取得できるとあるが、属性としてはvalueなので、PHPUnit_Seleniumではattribute('value')で取得できる。


1章は以上。