首頁  >  文章  >  後端開發  >  selenium php環境怎麼搭建

selenium php環境怎麼搭建

藏色散人
藏色散人原創
2022-11-06 10:27:421423瀏覽

selenium php環境搭建方法:1、下載最新線程安全版PHP zip壓縮包;2、複製一份「php.ini-development」改名為「php.ini」放到安裝路徑下;3 、設定係統變數下的Path為「D:\Software\php-7.2.28-Win32-VC15-x64;」即可。

selenium php環境怎麼搭建

本教學操作環境:windows7系統、PHP8.1版、Dell G3電腦。

selenium php環境怎麼搭建?

windows環境下的PHP selenium環境建置

#最近想要入門自動化測試,之前也寫過使用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。

  •  下載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的版本對應可查看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文件便可实现简单的自动登录流程。
###運行在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(&#39;vendor/autoload.php&#39;);// This is where Selenium server 2/3 listens by default. For Selenium 4, Chromedriver or Geckodriver, use http://localhost:4444/$host = &#39;http://localhost:4444/wd/hub&#39;;$capabilities = DesiredCapabilities::chrome();$driver = RemoteWebDriver::create($host, $capabilities);$driver->manage()->window()->maximize();// navigate to Selenium page on Wikipedia$driver->get(&#39;http://www.baidu.com/Login/s?name=lzxx&#39;);// write &#39;PHP&#39; in the search box$driver->findElement(WebDriverBy::id(&#39;name&#39;)) // find search input element->sendKeys(&#39;xxxx&#39;); // fill the search box$driver->findElement(WebDriverBy::id(&#39;xxxx&#39;))    ->sendKeys(&#39;88888888&#39;);//$driver->submit(); // submit the whole form

// wait until &#39;PHP&#39; is shown in the page heading element
//$driver->wait()->until(
//    WebDriverExpectedCondition::elementTextContains(WebDriverBy::id(&#39;firstHeading&#39;), &#39;PHP&#39;)
//);

// print title of the current page to outputecho "The title is &#39;" . $driver->getTitle() . "&#39;\n";// print URL of current page to outputecho "The current URL is &#39;" . $driver->getCurrentURL() . "&#39;\n";// find element of &#39;History&#39; item in menu
//$historyButton = $driver->findElement(
//    WebDriverBy::cssSelector(&#39;#jsLoginBtn&#39;)
//);$historyButton = $driver->findElement(
    WebDriverBy::id(&#39;jsLoginBtn&#39;)
);// read text of the element and print it to outputecho "About to click to button with text: &#39;" . $historyButton->getText() . "&#39;\n";// click the element to navigate to revision history page$historyButton->click();// wait until the target page is loaded$driver->wait()->until(
    WebDriverExpectedCondition::titleContains(&#39;教师首页&#39;)
);// print the title of the current pageecho "The title is &#39;" . $driver->getTitle() . "&#39;\n";// print the URI of the current pageecho "The current URI is &#39;" . $driver->getCurrentURL() . "&#39;\n";// delete all cookies
//$driver->manage()->deleteAllCookies();

// add new cookie$cookie = new Cookie(&#39;cookie_set_by_selenium&#39;, &#39;cookie_value&#39;);$driver->manage()->addCookie($cookie);// dump current cookies to output$cookies = $driver->manage()->getCookies();print_r($cookies);$driver->get(&#39;http://www.ekwing.com/exam/teacher/selflist&#39;);// close the browser
//$driver->quit();
## #題外話:因為selenium沒有支援PHP語言的整合框架,因此我們要使用selenium在專案中進行功能測試的話,需要自己將各個腳本組合,差不多就是寫個框架了。 ######推薦學習:《###PHP影片教學###》###

以上是selenium php環境怎麼搭建的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn