最近想要入門自動化測試,之前也寫過使用codeception進行單元測試和接口測試,UI測試部分我選擇了selenium框架,接下來我們來進行相關環境的搭建。
- PHP環境的搭建
1、進入PHP下載地址 http://windows.php.net/download 下載最新線程安全版PHP zip壓縮包,解壓縮后放在想要安裝的路徑下。(此處需要注意,win7系統不能用php7.4版本,會提示丟失 VCRUNTIME140.dll)
2、進入PHP安裝目錄,復制一份php.ini-development 改名為 php.ini 放到安裝路徑下,打開找到 ;extension_dir=ext,去掉注釋符,將值改為 PHP安裝路徑\ext。
3、右鍵計算機->屬性->高級系統設置->環境變量->系統變量下的Path,點擊編輯,在后面加上PHP的路徑D:\Software\php-7.2.28-Win32-VC15-x64;
至此,PHP安裝完成,可打開cmd查看對應的版本,如圖:
- java運行環境的搭建,這里需要說明一下selenium運行文件是一個jar包,你必須搭建好java運行的環境才能啟用selenium。
進入官網,https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html,找到適配的版本,下載jdk。具體參照:https://www.jianshu.com/p/efef80171a4a
-
下載selenium文件,http://selenium-release.storage.googleapis.com/index.html (selenium 下載地址)下載selenium-server-standalone-3.8.0.jar的jar包文件,版本可自行選擇
- 下載瀏覽器驅動文件(這里需要注意的是:一定要下載與本機安裝瀏覽器版本匹配的驅動文件) 。Google瀏覽器使用的驅動文件名為: chromedriver,https://chromedriver.storage.googleapis.com/index.html (chrome driver 下載地址)。Firefox的驅動文件名為:geckodriver.exe,https://docs.seleniumhq.org/download/(selenium官網去下載,選擇java的)
chrom和chromedriver的版本對應可查看每個版本里面的note,chrome的版本號可通過chrome://settings/help查看
注意:下載完成的驅動文件要放在php的根目錄下
- 下載 PHP+selenium 的demo文件,https://github.com/facebook/php-webdriver (里面有example.php以及 tests文件下的案例文檔共參考)。
-
寫好demo之后你就可以進行測試了,首先運行下載的selenium的jar包文件,在cmd命令行中進入你放置selenium文件的目錄然后執行以下命令(注意:需要在第二步中配置java運行環境變量) java -jar selenium-server-standalone-3.8.0.jar 。 如果你的命令行出現了以下提示那就是啟動成功了。
在執行example.php的時候,Notice: Undefined index: ELEMENT in D:\test\vendor\facebook\webdriver\lib\Remote\RemoteWebDriver.php on line 178,
經查,是因為較新版本的selenium的通信協議變動導致的,可在啟動時加上相關的參數控制:
java -jar selenium-server-standalone-3.8.0.jar -enablePassThrough false
至此,通過編寫example.php文件便可實現簡單的自動登錄流程。
運行exam.php之前,需要將ekwing下vendor目錄復制一份到phpDirver目錄下,如圖:
可修改example.php實現別的網站自動登錄,example.php如下:
<?php // An example of using php-webdriver. // Do not forget to run composer install before. You must also have Selenium server started and listening on port 4444. namespace Facebook\WebDriver; use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; require_once('vendor/autoload.php'); // This is where Selenium server 2/3 listens by default. For Selenium 4, Chromedriver or Geckodriver, use http://localhost:4444/ $host = 'http://localhost:4444/wd/hub'; $capabilities = DesiredCapabilities::chrome(); $driver = RemoteWebDriver::create($host, $capabilities); $driver->manage()->window()->maximize(); // navigate to Selenium page on Wikipedia $driver->get('http://www.baidu.com/Login/s?name=lzxx'); // write 'PHP' in the search box $driver->findElement(WebDriverBy::id('name')) // find search input element ->sendKeys('xxxx'); // fill the search box $driver->findElement(WebDriverBy::id('xxxx')) ->sendKeys('88888888'); //$driver->submit(); // submit the whole form // wait until 'PHP' is shown in the page heading element //$driver->wait()->until( // WebDriverExpectedCondition::elementTextContains(WebDriverBy::id('firstHeading'), 'PHP') //); // print title of the current page to output echo "The title is '" . $driver->getTitle() . "'\n"; // print URL of current page to output echo "The current URL is '" . $driver->getCurrentURL() . "'\n"; // find element of 'History' item in menu //$historyButton = $driver->findElement( // WebDriverBy::cssSelector('#jsLoginBtn') //); $historyButton = $driver->findElement( WebDriverBy::id('jsLoginBtn') ); // read text of the element and print it to output echo "About to click to button with text: '" . $historyButton->getText() . "'\n"; // click the element to navigate to revision history page $historyButton->click(); // wait until the target page is loaded $driver->wait()->until( WebDriverExpectedCondition::titleContains('教師首頁') ); // print the title of the current page echo "The title is '" . $driver->getTitle() . "'\n"; // print the URI of the current page echo "The current URI is '" . $driver->getCurrentURL() . "'\n"; // delete all cookies //$driver->manage()->deleteAllCookies(); // add new cookie $cookie = new Cookie('cookie_set_by_selenium', 'cookie_value'); $driver->manage()->addCookie($cookie); // dump current cookies to output $cookies = $driver->manage()->getCookies(); print_r($cookies); $driver->get('http://www.ekwing.com/exam/teacher/selflist'); // close the browser //$driver->quit();
題外話:因為selenium沒有支持PHP語言的集成框架,因此我們要使用selenium在項目中進行功能測試的話,需要自己將各個腳本組合,差不多就是寫個框架了。