前言
selenium可用于界面UI自动化测试,因此也可用于来做一些自动化方面的事情。下面简单总结概括一下,对于一位新手,学习和使用selenium的基本过程。
本文只要针对:selenium自动化测试+ubuntu系统+php语言+firefox/chrome浏览器
第一步,浏览器在Selenium的录制
对于FireFox浏览器,有Selenium的相关插件,可用于录制和回放你在浏览器上的操作,包括但不限于:打开网页、在表单的输入、链接点击和跳转等。
例如火狐插件中,由selenium官方提供的扩展Selenium IDE(第一个);以及另一个开源社区提供的Kantu(第二个)。
另外,在Chrome浏览器,也可以看到类似的扩展。如下:
装好扩展后,为了让大家一个直观感观的认识,我们可以打开Selenium IDE扩展,然后开始录制对浏览器的操作。以Chrome的Selenium IDE扩展为例,打开扩展后,依次操作:
开始录制一个项目
对新项目进行命名
(中间省略……),录制过程吕,可以看到实时产生纪录信息。最后,点击停止完成录制。
通过播放回放,可以看到会自动执行刚才的操作。
关于Selenium IDE更多参考资料,请见Selenium官方文档说明:https://www.seleniumhq.org/docs/02_selenium_ide.jsp
第二步,使用PHP开发语言,模拟操作浏览器
前面是普通用户在浏览器进行录制的过程和说明,下面说一下,作为技术编程开发人员,又如何使用Selenium完成自动化操作的事情。
Selenium支持很多客户端开发语言,包括有:PHP、Java、C#、Python、Ruby、Perl、PHP、JavaScript,这些客户端的主要作用是连接服务端,并通过指令将需要进行的操作发送给服务端远程进行。
这里重点以PHP为例,首页下载对应的Selenium PHP SDK包,有三个版本,可根据需要选择,我备注了一下各个SDK的应用场景:
第一个版本:WebDriver-PHP(适用于PHPUnit单元测试)
By Chibimagic,Github地址是:https://github.com/chibimagic/WebDriver-PHP/第二个版本:php-webdriver-bindings(适用于旧版本普通的PHP开发)
By Lukasz Kolczynski,地址是:https://code.google.com/archive/p/php-webdriver-bindings/第三个版本:php-webdriver(适用于composer包安装方式)
By the Facebook,Github地址是:https://github.com/facebook/php-webdriver
这里选择了第二个版本,里面example.php文件中的示例代码是:
require_once "phpwebdriver/WebDriver.php";$webdriver = new WebDriver("localhost", "4444");$webdriver->connect("internet explorer"); $webdriver->get("http://google.com");$element = $webdriver->findElementBy(LocatorStrategy::name, "q");if ($element) { $element->sendKeys(array("php webdriver" ) ); $element->submit();}$webdriver->close();
可以看出,它主要做的事情是,打开谷歌首页,然后在关键字输入中输入“php webdriver”,然后谷歌搜索:php webdriver。
如果这时候用PHP直接执行这个PHP脚本,肯定会报错,一如:
$ php example.phpPHP Notice: Undefined offset: 1 in phpwebdriver\WebDriver.php on line 58PHP Notice: Trying to get property of non-object in phpwebdriver\WebDriver.php on line 64X-Powered-By: PHP/7.0.18Content-type: text/html; charset=UTF-8
原因很简单,就类似操作MySQL数据库一样,因为Selenium服务端还没启动,连接不上,做任何操作都是毫无意义的。
下面,花一点时间,搭建并启动Selenium Server服务端。
更多关于Selenium WebDriver的资料,请参考Selenium官方文档:https://www.seleniumhq.org/docs/03_webdriver.jsp
第三步,在Ubuntu搭建并启动Selenium Server服务端
这里的服务器系统,选择了Ubuntu,因为运行浏览器需要支持GUI操作,同时又要考虑开源,所以最好选择就推荐Ubunut啦。
两个前提条件,不言而喻:
第一个前提条件,假设你已经安装好Ubuntu系统
第二个前提条件,假设你已经在系统上搭建好Java运行环境(因为Selenium服务端是Java写的)
接下来,就是下载Selenium 服务端 Selenium Standalone Server 3.141.59,下载地址:
https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
成功下载后,使用以下命令启动:
$ java -jar ./selenium-server-standalone-3.141.59.jar -role hub
成功启动后,可以看到以下提示:
10:13:17.358 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d35810:13:17.900 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Selenium Grid hub on port 44442019-01-18 10:13:19.037:INFO::main: Logging initialized @3181ms to org.seleniumhq.jetty9.util.log.StdErrLog10:13:19.885 INFO [Hub.start] - Selenium Grid hub is up and running10:13:19.886 INFO [Hub.start] - Nodes should register to http://192.168.174.132:4444/grid/register/10:13:19.886 INFO [Hub.start] - Clients should connect to http://192.168.174.132:4444/wd/hub
Selenium默认侦听的端口是4444,。但是,不要开心太早,虽然看到提示是成功的,但如果打开:http://192.168.174.132:4444/wd/hub (题外话:它提示客户端应该连接到此,前面第二步PHP的客户端代码中也用到了这个链接),你可能会看到以下JSON数据。看到这堆JSON信息,说明启动是失败的。
看到提示了空指针错误,具体原因不详。解决方案是,重新启动Selenium Server,并且不带-role参数,即用以下命令启动:
$ java -jar ./selenium-server-standalone-3.141.59.jar
启动后,再访问:http://192.168.174.132:4444/wd/hub (它会自动跳转一下),可以看到出来页面啦!!
但依然别开心太早,在没完全全部正常运行起来之前,都还有可能会遇到其他问题,并且折磨你很久。
果不然,我就遇到了一个查了很久的问题,最终才得到解决。。。。。。
此文章发布于开源中国dogstar的博客上:https://my.oschina.net/dogstar/blog/3003125,如果有问题,可随时反馈给我修正哦~
此部分,关于Selenium Server的更多资料,请参考Selenium官方文档:https://www.seleniumhq.org/download/
第四步,启动FireFox/Chrome浏览器
最大的问题,在于启动浏览器时遇到了很大的阻碍。问题并不在于本身技术有多难,在而于对于整个流程的环节不是很熟悉,并在最初存在一定的误解。
这里的问题就是,在完成第三步,成功启动Selenium Server后,再回到第二步,使用PHP连接Selenium服务端时,会提示以下错误:
Empty pool of VM for setup Capabilities {browserName: firefox, javascriptEnabled: true, nativeEvents: false, version: }
又或者提示创建会话失败:
Selenium unable to create new session
在查了很多资料后,依然未能解决。以为是Selenium Server和浏览器版本不兼容,又以为是PHP客户端版本与服务端版本不兼容,又或者其他原因。
不管是Ubuntu上的FireFox还是Chrome,都有同样的问题,因此排除了是浏览器的问题。
最后,才意识到,是驱动的问题,服务端也需要安装WebDriver驱动!!
因此,快速在Ubuntu上安装了FireFox和Crhome的驱动,分别是:
1)FireFox的WebDriver驱动安装
到这里:https://github.com/mozilla/geckodriver/releases,下载:https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz
解压后,再移动到/usr/bin/geckodriver。
dogstar@ubuntu-16selenium$ tar -xzvf ./geckodriver-v0.23.0-linux64.tar.gz ./geckodriverdogstar@ubuntu-16selenium$ sudo mv ./geckodriver /usr/bin/
2)Chrome的WebDriver驱动安装
安装类似,请使用以下命令:
$ wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip$ unzip chromedriver_linux64.zip$ sudo mv chromedriver /usr/bin/chromedriver$ sudo chown root:root /usr/bin/chromedriver$ sudo chmod +x /usr/bin/chromedriver
万事俱备,只差东风啦!
回到刚才的操作页面:http://192.168.174.132:4444/wd/hub/static/resource/hub.html,新建一个会话,可以看到创建成功,并且在服务端同步响应了此请求,在服务端自动打开了一个新浏览器窗口。
服务端自动响应:
成功创建浏览器会话后,
关于WebDriver,更多请参考Selenium官方文档:https://www.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-s-drivers
第五步,完美运行
刚才我们失意在第二步,使用PHP开发语言,模拟操作浏览器。对于PHPer来说,确实是不少的打击。但究其原因,是因为我们步子迈得太大了,服务端还有很多事情还没完成。例如Selenium Server服务端的安装和启动、浏览器驱动的安装。
现在全部都安装好后,我们再来重新执行PHP代码,不过要稍微修改一下。把连接的地址改成自己的,并且把谷歌改为百度(因为国内经常访问不到谷歌)。
require_once "phpwebdriver/WebDriver.php";$webdriver = new WebDriver("192.168.174.132", "4444");//$webdriver->connect("firefox"); $webdriver->connect("chrome"); $webdriver->get("http://wwww.baidu.com");$webdriver->close();
这里做的事情很简单,就只是打开百度首页而已,其他什么也不做。
这时,可以完美运行Selenium了,即通过PHP客户端自动化操作远程的Selenium服务端,从而在服务端自动完成浏览器的操作。当然,这只是一个简单的例子。
下面,来看一个稍微复杂,但经常会用到的示例——自动用户登录。
第六步,用PHP自动登录开源中国
通过浏览器插件,先看下登录开源中国,需要做哪些事情。让我们来录制一下。录制后,保存的纪录文件JSON数据如下:
{ "id": "e3691656-e021-48b0-be5e-ccae30d21f56", "version": "1.1", "name": "oschina", "url": "https://www.oschina.net", "tests": [{ "id": "02a7d248-2335-4858-beb5-e3efbf5e5fe4", "name": "oschina-login", "commands": [{ "id": "a162a0bd-2b6e-41f4-b3b6-35166a270819", "comment": "", "command": "open", "target": "/home/login", "targets": [], "value": "" }, { "id": "401d8657-a91c-4658-ae61-d52b807e634f", "comment": "", "command": "setWindowSize", "target": "1262x710", "targets": [], "value": "" }, { "id": "52f32d65-4ff5-4577-8b5f-6173dbd73b07", "comment": "", "command": "click", "target": "id=userMail", "targets": [ ["id=userMail", "id"], ["css=#userMail", "css"], ["css=#userMail", "css:finder"], ["xpath=//input[@id='userMail']", "xpath:attributes"], ["xpath=//div[@id='account_login']/form/div/div/div/input", "xpath:idRelative"], ["xpath=//input", "xpath:position"] ], "value": "" }, { "id": "6dc7c72e-4f50-4349-ab2e-85322f64cb50", "comment": "", "command": "type", "target": "id=userMail", "targets": [ ["id=userMail", "id"], ["css=#userMail", "css"], ["css=#userMail", "css:finder"], ["xpath=//input[@id='userMail']", "xpath:attributes"], ["xpath=//div[@id='account_login']/form/div/div/div/input", "xpath:idRelative"], ["xpath=//input", "xpath:position"] ], "value": "demo" }, { "id": "a0e75dd5-4b61-420a-9ef5-2d388e1ed5d9", "comment": "", "command": "click", "target": "id=userPassword", "targets": [ ["id=userPassword", "id"], ["css=#userPassword", "css"], ["css=#userPassword", "css:finder"], ["xpath=//input[@id='userPassword']", "xpath:attributes"], ["xpath=//div[@id='account_login']/form/div/div[2]/div/input", "xpath:idRelative"], ["xpath=//div[2]/div/input", "xpath:position"] ], "value": "" }, { "id": "f63ee060-40ac-45cb-b00a-d24eeea3c6bb", "comment": "", "command": "type", "target": "id=userPassword", "targets": [ ["id=userPassword", "id"], ["css=#userPassword", "css"], ["css=#userPassword", "css:finder"], ["xpath=//input[@id='userPassword']", "xpath:attributes"], ["xpath=//div[@id='account_login']/form/div/div[2]/div/input", "xpath:idRelative"], ["xpath=//div[2]/div/input", "xpath:position"] ], "value": "123456" }, { "id": "9388fe28-4d49-4b8a-bae4-1ea37a5d136f", "comment": "", "command": "click", "target": "css=.btn-green", "targets": [ ["css=.btn-green", "css:finder"], ["xpath=//button[@type='button']", "xpath:attributes"], ["xpath=//div[@id='account_login']/form/div/div[5]/button", "xpath:idRelative"], ["xpath=//button", "xpath:position"] ], "value": "" }] }], "suites": [{ "id": "4f9b7142-4648-487f-982b-28666492f5a7", "name": "Default Suite", "persistSession": false, "parallel": false, "timeout": 300, "tests": ["02a7d248-2335-4858-beb5-e3efbf5e5fe4"] }], "urls": ["https://www.oschina.net/"], "plugins": []}
对应页面效果是:
别看JSON数据很多,实际做的事情很简单:
1、打开登录页面
2、输入账号:demo
3、输入密码:123456
4、点击登录按钮
当然咯,这里的账号和密码都是错误的 ^_^。
在以前,我记得Selenium IDE在录制完成后,是可以导出PHP代码,例如这样(网上找的图):
可惜,现在不知是不是因为新版的扩展没有了还是怎样。只好人工开发转为PHP代码了,那怎么转呢?
那么,如果想用PHP代码(或其他代码)来自动实现登录,需要怎么做呢?
最后,附一张官方的架构图: