> Selenium是用于自動化測試工具,它是在Apache許可證2.0許可的開放源代碼工具。Selenium是一套工具,它有助于自動化Web應用程序測試。
>* 框架底層使用JavaScript模擬真實用戶對瀏覽器進行操作。測試腳本執行時,瀏覽器自動按照腳本代碼做出點擊,輸入,打開,驗證等操作,就像真實用戶所做的一樣,從終端用戶的角度測試應用程序。
>* 使瀏覽器兼容性測試自動化成為可能,盡管在不同的瀏覽器上依然有細微的差別。
>* 使用簡單,可使用Java,Python等多種語言編寫用例腳本。
[TOC]
#### 1.通過composer安裝Selenium:
~~~
composer require facebook/webdriver
~~~
#### 2.下載Selenium Server并啟動:
~~~
//到http://www.seleniumhq.org/download/ 下找到Selenium Standalone Server并下載,或到https://chromedriver.storage.googleapis.com/index.html 上面下載。
//到命令提示符里啟動以下命令(前提需要安裝jdk,確保java命令能夠運行)
java -jar selenium-server-standalone-2.42.2.jar
~~~
#### 3.運行測試代碼
> 另啟命令提示符,運行php test.php 命令
> 示例腳本test.php:
~~~
<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
header("Content-Type: text/html; charset=UTF-8");
// start Firefox with 5 second timeout
$waitSeconds = 15; //需等待加載的時間,一般加載時間在0-15秒,如果超過15秒,報錯。
$host = 'http://localhost:4444/wd/hub'; // this is the default
//這里使用的是chrome瀏覽器進行測試,需到http://www.seleniumhq.org/download/上下載對應的瀏覽器測試插件
//我這里下載的是win32 Google Chrome Driver 2.25版:https://chromedriver.storage.googleapis.com/index.html?path=2.25/
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
// navigate to 'http://docs.seleniumhq.org/'
$driver->get('https://www.baidu.com/');
echo iconv("UTF-8","GB2312",'標題1').":" . $driver->getTitle() . "\n"; //cmd.exe中文亂碼,所以需轉碼
$driver->findElement(WebDriverBy::id('kw'))->sendKeys('wwe')->submit();
// 等待新的頁面加載完成....
$driver->wait($waitSeconds)->until(
WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::partialLinkText('100shuai')
)
);
$driver->findElement(WebDriverBy::partialLinkText('100shuai'))->sendKeys('xxx')->click(); //一般點擊鏈接的時候,擔心因為失去焦點而拋異常,則可以先調用一下sendKeys,再click
switchToEndWindow($driver); //切換至最后一個window
// 等待加載....
$driver->wait($waitSeconds)->until(
WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::partialLinkText('SmackDown收視率創歷史新低')
)
);
echo iconv("UTF-8","GB2312",'標題2').":" . $driver->getTitle() . "\n"; //cmd.exe中文亂碼,所以需轉碼
$driver->findElement(WebDriverBy::partialLinkText('SmackDown收視率創歷史新低'))->click();
switchToEndWindow($driver); //切換至最后一個window
// 等待加載....
$driver->wait($waitSeconds)->until(
WebDriverExpectedCondition::titleContains('SmackDown收視率創歷史新低')
);
echo iconv("UTF-8","GB2312",'標題3').":" . $driver->getTitle() . "\n"; //cmd.exe中文亂碼,所以需轉碼
//關閉瀏覽器
$driver->quit();
//切換至最后一個window
//因為有些網站的鏈接點擊過去帶有target="_blank"屬性,就新開了一個TAB,而selenium還是定位到老的TAB上,如果要實時定位到新的TAB,則需要調用此方法,切換到最后一個window
function switchToEndWindow($driver){
$arr = $driver->getWindowHandles();
foreach ($arr as $k=>$v){
if($k == (count($arr)-1)){
$driver->switchTo()->window($v);
}
}
}
?>
~~~
下載資料:
1.http://selenium-release.storage.googleapis.com/index.html (selenium 下載地址)
2.https://chromedriver.storage.googleapis.com/index.html (chrome driver 下載地址)
3.http://phantomjs.org/download.html (PhantomJS Driver 下載地址)
4.http://www.cnbeta.com/articles/soft/563605.htm (chrome 下載地址,建議使用這個版本或者以下版本,其他最新版本,瀏覽器識別了是否為測試軟件,對于個別用途的軟件需要注意,如果僅僅是為了做測試,那就無所謂了。)
5.http://blog.csdn.net/huilan_same/article/details/51896672 (chromedriver.exe版本對應的chrome版本)
參考文檔:
1.https://github.com/facebook/php-webdriver (里面有example.php以及 tests文件下的案例文檔共參考)
2.https://github.com/facebook/php-webdriver/wiki 快速開始教程
3.http://facebook.github.io/php-webdriver/namespaces/default.html API文檔
4.http://www.yiibai.com/selenium/ 易百教程
6.https://github.com/chibimagic/WebDriver-PHP/
7.https://code.google.com/archive/p/php-webdriver-bindings/
8.https://github.com/Element-34/php-webdriver
9.https://github.com/Nearsoft/php-selenium-client
10.http://pan.baidu.com/s/1eR31pM6 selenium_webdriver(python)第一版.pdf