温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

selenium php环境如何搭建

发布时间:2022-11-07 09:48:35 来源:亿速云 阅读:253 作者:iii 栏目:编程语言

这篇“selenium php环境如何搭建”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“selenium php环境如何搭建”文章吧。

selenium php环境搭建方法:1、下载最新线程安全版PHP zip压缩包;2、复制一份“php.ini-development”改名为“php.ini”放到安装路径下;3、设置系统变量下的Path为“D:\Software\php-7.2.28-Win32-VC15-x64;”即可。

windows环境下的PHP+selenium环境搭建

最近想要入门自动化测试,之前也写过使用codeception进行单元测试和接口测试,UI测试部分我选择了selenium框架,接下来我们来进行相关环境的搭建。

  • PHP环境的搭建

1、进入PHP 下载最新线程安全版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查看对应的版本,如图:

selenium php环境如何搭建

  • java运行环境的搭建,这里需要说明一下selenium运行文件是一个jar包,你必须搭建好java运行的环境才能启用selenium。

进入官网,找到适配的版本,下载jdk。

selenium php环境如何搭建

  • 下载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。Firefox的驱动文件名为:geckodriver.exe,https://docs.seleniumhq.org/download/(selenium官网去下载,选择java的)

    chrom和chromedriver的版本对应可查看每个版本里面的note,chrome的版本号可通过chrome://settings/help查看

    selenium php环境如何搭建

    selenium php环境如何搭建

    注意:下载完成的驱动文件要放在php的根目录下

    selenium 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 。     如果你的命令行出现了以下提示那就是启动成功了。

    selenium php环境如何搭建

    在执行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 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、执行速度快。

    2、具有很好的开放性和可扩展性。

    3、PHP支持多种主流与非主流的数据库。

    4、面向对象编程:PHP提供了类和对象。

    5、版本更新速度快。

    6、具有丰富的功能。

    7、可伸缩性。

    8、功能全面,包括图形处理、编码与解码、压缩文件处理、xml解析等。

    以上就是关于“selenium php环境如何搭建”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI