您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

selenium php环境怎么搭建

2024/3/30 23:31:56发布17次查看
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框架,接下来我们来进行相关环境的搭建。
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的版本对应可查看每个版本里面的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 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视频教程》
以上就是selenium php环境怎么搭建的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product