PHPUnit + Selenium Server 的筆記


因為私下開發的玩具有點煩鎖.
所以只好參上 Selenium 了.
也來記錄一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 切到專案中的 vendor 目錄裡的執行目錄
mkdir -p project/vendor/bin
cd project/vendor/bin

# 下載 Selenium Server
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.31.0.jar

# 回到專案中執行 Selenium Server
java -jar vendor/selenium-server-standalone-2.31.0.jar -p 4444

# 再開新的視窗
vendor/phpunit tests/some_test_case.php

# 關於 some_test_case.php 內容大約如下
<?php
class Some_Test_Case extends PHPUnit_Extensions_Selenium2TestCase {
public static $browsers = array(
array(
'browserName' => 'firefox',
'host' => 'localhost',
'port' => 4444,
'sessionStrategy' => 'shared', // 同一個視窗內執行,
// 'sessionStrategy' => 'isolated' // 每個測試都開新視窗
),
array(
'browserName' => 'safari',
'host' => 'localhost',
'port' => 4444
)
);

public function setUp() {
$this->setBrowserUrl("http://localhost/labs/project");
}

public function setupSpecificBrowser($params) {
parent::setupSpecificBrowser($params);
}

public function testHasSomeForm() {
$this->url('/form');

// 可以是其他 byId, byName, byCssSelector
$email = $this->byName('email');
$password = $this->byName('password');

$this->assertEquals('', $email->value());
$this->assertEquals('', $password->value());
}

public function testSubmitInvalidEmail() {
$this->url('/form');
$this->byName('email')->value('test');
$this->byCssSelector('form')->submit();
$this->assertEquals('Error! Invalid email format', $this->byCssSelector('.alert-error')->text());
}
}
?>