溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

selenium php環(huán)境如何搭建

發(fā)布時間:2022-11-07 09:48:35 來源:億速云 閱讀:273 作者:iii 欄目:編程語言

這篇“selenium php環(huán)境如何搭建”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“selenium php環(huán)境如何搭建”文章吧。

selenium php環(huán)境搭建方法:1、下載最新線程安全版PHP zip壓縮包;2、復(fù)制一份“php.ini-development”改名為“php.ini”放到安裝路徑下;3、設(shè)置系統(tǒng)變量下的Path為“D:\Software\php-7.2.28-Win32-VC15-x64;”即可。

windows環(huán)境下的PHP+selenium環(huán)境搭建

最近想要入門自動化測試,之前也寫過使用codeception進行單元測試和接口測試,UI測試部分我選擇了selenium框架,接下來我們來進行相關(guān)環(huán)境的搭建。

  • PHP環(huán)境的搭建

1、進入PHP 下載最新線程安全版PHP zip壓縮包,解壓縮后放在想要安裝的路徑下。(此處需要注意,win7系統(tǒng)不能用php7.4版本,會提示丟失 VCRUNTIME140.dll)

2、進入PHP安裝目錄,復(fù)制一份php.ini-development 改名為 php.ini 放到安裝路徑下,打開找到 ;extension_dir=ext,去掉注釋符,將值改為 PHP安裝路徑\ext。

3、右鍵計算機->屬性->高級系統(tǒng)設(shè)置->環(huán)境變量->系統(tǒng)變量下的Path,點擊編輯,在后面加上PHP的路徑D:\Software\php-7.2.28-Win32-VC15-x64;

至此,PHP安裝完成,可打開cmd查看對應(yīng)的版本,如圖:

selenium php環(huán)境如何搭建

  • java運行環(huán)境的搭建,這里需要說明一下selenium運行文件是一個jar包,你必須搭建好java運行的環(huán)境才能啟用selenium。

進入官網(wǎng),找到適配的版本,下載jdk。

selenium php環(huán)境如何搭建

  • 下載selenium文件,http://selenium-release.storage.googleapis.com/index.html (selenium 下載地址)下載selenium-server-standalone-3.8.0.jar的jar包文件,版本可自行選擇

  • 下載瀏覽器驅(qū)動文件(這里需要注意的是:一定要下載與本機安裝瀏覽器版本匹配的驅(qū)動文件) 。Google瀏覽器使用的驅(qū)動文件名為:   chromedriver,https://chromedriver.storage.googleapis.com/index.html。Firefox的驅(qū)動文件名為:geckodriver.exe,https://docs.seleniumhq.org/download/(selenium官網(wǎng)去下載,選擇java的)

    chrom和chromedriver的版本對應(yīng)可查看每個版本里面的note,chrome的版本號可通過chrome://settings/help查看

    selenium php環(huán)境如何搭建

    selenium php環(huán)境如何搭建

    注意:下載完成的驅(qū)動文件要放在php的根目錄下

    selenium php環(huán)境如何搭建

    • 下載 PHP+selenium 的demo文件,https://github.com/facebook/php-webdriver (里面有example.php以及 tests文件下的案例文檔共參考)。

    • 寫好demo之后你就可以進行測試了,首先運行下載的selenium的jar包文件,在cmd命令行中進入你放置selenium文件的目錄然后執(zhí)行以下命令(注意:需要在第二步中配置java運行環(huán)境變量)           java -jar selenium-server-standalone-3.8.0.jar 。     如果你的命令行出現(xiàn)了以下提示那就是啟動成功了。

    selenium php環(huán)境如何搭建

    在執(zhí)行example.php的時候,Notice: Undefined index: ELEMENT in D:\test\vendor\facebook\webdriver\lib\Remote\RemoteWebDriver.php on line 178,

    經(jīng)查,是因為較新版本的selenium的通信協(xié)議變動導(dǎo)致的,可在啟動時加上相關(guān)的參數(shù)控制:

    java -jar selenium-server-standalone-3.8.0.jar -enablePassThrough false至此,通過編寫example.php文件便可實現(xiàn)簡單的自動登錄流程。

    運行exam.php之前,需要將ekwing下vendor目錄復(fù)制一份到phpDirver目錄下

    可修改example.php實現(xiàn)別的網(wǎng)站自動登錄,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語言的集成框架,因此我們要使用selenium在項目中進行功能測試的話,需要自己將各個腳本組合,差不多就是寫個框架了。

    php有什么特點

    1、執(zhí)行速度快。

    2、具有很好的開放性和可擴展性。

    3、PHP支持多種主流與非主流的數(shù)據(jù)庫。

    4、面向?qū)ο缶幊蹋篜HP提供了類和對象。

    5、版本更新速度快。

    6、具有豐富的功能。

    7、可伸縮性。

    8、功能全面,包括圖形處理、編碼與解碼、壓縮文件處理、xml解析等。

    以上就是關(guān)于“selenium php環(huán)境如何搭建”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI