您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
使用PHPUnit+Selenium進行自動化測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2016/3/28 16:51:18 ] 推薦標簽:自動化測試 功能測試

  第一步,安裝PHPUnit
  簡單的方式莫過于到PHPUnit的官網(wǎng)下載PHPUnit,然后把那個下載了的phpunit.phar加到PATH中。
  此外,還可以通過 pear 來安裝phpunit:
  pear clear-cache
  pear channel-discover pear.phpunit.de
  pear channel-discover components.ez.no
  pear channel-discover pear.symfony-project.com
  pear install -a -f  phpunit/PHPUnit
  參考:http://www.cnblogs.com/wanfox/p/4613517.html
  然而使用 pear 安裝方式,我卻是沒有成功!大概是因為天朝的神奇的GFW吧!
  第二步,下載Selenium Standalone Server
  傳送門:http://docs.seleniumhq.org/download/
  下載下來一般是一個selenium-server-standalone-2.48.2.jar這樣的文件(版本號可能會有所更新)。
  第三步,下載WebDriver for Chrome
  傳送門: https://sites.google.com/a/chromium.org/chromedriver/downloads
  這個要根據(jù)電腦是Windows、Linux還是MAC,來下載相應(yīng)的驅(qū)動,并解壓縮。
  第四步,啟動Server
  前面兩步的東西都下載好了,可以啟動這個server了:
  # 假設(shè)1:已經(jīng)裝了java, 而且將java加到了PATH中
  # 假設(shè)2:前面兩步下載的東東都在當前目錄下
  java -jar selenium-server-standalone-2.42.2.jar -Dwebdriver.chrome.driver=./chromedriver
  p.s. 對于Windows的用戶,后面的./chromedriver需要修改為.chromedriver.exe
  第四步,下載PHP的WebDriver封裝庫
  本人喜歡使用Facebook出品的這個庫.
  鑒于composer在國內(nèi)的悲慘現(xiàn)狀,還是直接下載zip包,然后自行加載吧。
  下面是我常用的自動加載器:
// file: tests/bootstrap.php
call_user_func(function(){
$namespaces = array(
'FacebookWebDriver' =>  __DIR__.'/../php-webdriver/lib',
);
spl_autoload_register(function($class) use ($namespaces){
$class = ltrim($class, '\');
foreach ($namespaces as $ns => $dir) {
if (strncmp($class, $ns, strlen($ns)) === 0){
$file = str_replace('\', DIRECTORY_SEPARATOR, $dir . DIRECTORY_SEPARATOR . ltrim(substr($class, strlen($ns)), '\')) . '.php';
if (is_file($file)){
include_once($file);
}
}
}
});
});
  這個自動加載器可以放到bootstrap.php中,以便phpunit跑用例前加載。
  第五步,編寫一個簡單的測試用例
  下面這個測試用例測試下百度是否能正常打開,檢查百度的標題和URL是否符合預期。
// file: tests/sample/BaiduTest.php
class BaiduTest extends PHPUnit_Framework_TestCase
{
public function testTheTitleUrl()
{
// create a browser
$browser = create_browser();
// open baidu via GET
$browser->get('https://www.baidu.com/');
// get title
$this->assertEquals('百度一下,你知道', $browser->getTitle());
// get the current URL
$this->assertEquals('https://www.baidu.com/', $browser->getCurrentURL());
// close the browser
$browser->quit();
}
}
  注意:文件名和類名要以Test結(jié)尾,而其中的測試函數(shù)要以test開頭,這樣子方便phpunit查找測試用例。

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd