selenium php環境搭建方法:1、下載最新線程安全版PHP zip壓縮包;2、複製一份「php.ini-development」改名為「php.ini」放到安裝路徑下;3 、設定係統變數下的Path為「D:\Software\php-7.2.28-Win32-VC15-x64;」即可。
本教學操作環境:windows7系統、PHP8.1版、Dell G3電腦。
selenium php環境怎麼搭建?
windows環境下的PHP selenium環境建置
#最近想要入門自動化測試,之前也寫過使用codeception進行單元測試和介面測試,UI測試部分我選擇了selenium框架,接下來我們來進行相關環境的建構。
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查看對應的版本,如圖:
## chrom與chromedriver的版本對應可查看chrome每個版本裡面的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文件便可实现简单的自动登录流程。
<?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 outputecho "The title is '" . $driver->getTitle() . "'\n";// print URL of current page to outputecho "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 outputecho "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 pageecho "The title is '" . $driver->getTitle() . "'\n";// print the URI of the current pageecho "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環境怎麼搭建的詳細內容。更多資訊請關注PHP中文網其他相關文章!