使用PHPUnit + Selenium进行自动化测试
project: blog
target: how-to-use-phpunit-selenium-test.md
date: 2015-12-22
status: publish
tags:
- Selenium
- PHPUnit
- Test
categories:
- Selenium
第一步,安装PHPUnit
最简单的方式莫过于到PHPUnit的官网下载PHPUnit,然后把那个下载了的phpunit.phar加到PATH中。
此外,还可以通过 pear 来安装phpunit:
pear clear-cache
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
pear install -a -f phpunit/PHPUnit
参考:http://www.cnblogs.com/wanfox/p/4613517.html
然而使用 pear 安装方式,我却是没有成功!大概是因为天朝的神奇的GFW吧!
第二步,下载Selenium Standalone Server
传送门:http://docs.seleniumhq.org/download/
下载下来一般是一个selenium-server-standalone-2.48.2.jar这样的文件(版本号可能会有所更新)。
第三步,下载WebDriver for Chrome
传送门: https://sites.google.com/a/chromium.org/chromedriver/downloads
这个要根据电脑是Windows、Linux还是MAC,来下载相应的驱动,并解压缩。
第四步,启动Server
前面两步的东西都下载好了,就可以启动这个server了:
# 假设1:已经装了java, 而且将java加到了PATH中
# 假设2:前面两步下载的东东都在当前目录下
java -jar selenium-server-standalone-2.42.2.jar -Dwebdriver.chrome.driver=./chromedriver
p.s. 对于Windows的用户,后面的./chromedriver需要修改为.\chromedriver.exe
第四步,下载PHP的WebDriver封装库
本人喜欢使用Facebook出品的这个库.
鉴于composer在国内的悲惨现状,还是直接下载zip包,然后自行加载吧。
下面是我常用的自动加载器:
// file: tests/bootstrap.php
call_user_func(function(){
$namespaces = array(
'Facebook\WebDriver' => __DIR__.'/../php-webdriver/lib',
);
spl_autoload_register(function($class) use ($namespaces){
$class = ltrim($class, '\\');
foreach ($namespaces as $ns => $dir) {
if (strncmp($class, $ns, strlen($ns)) === 0){
$file = str_replace('\\', DIRECTORY_SEPARATOR, $dir . DIRECTORY_SEPARATOR . ltrim(substr($class, strlen($ns)), '\\')) . '.php';
if (is_file($file)){
include_once($file);
}
}
}
});
});
这个自动加载器可以放到bootstrap.php中,以便phpunit跑用例前加载。
第五步,编写一个简单的测试用例
下面这个测试用例测试下百度是否能正常打开,检查百度的标题和URL是否符合预期。
// file: tests/sample/BaiduTest.php
class BaiduTest extends PHPUnit_Framework_TestCase
{
public function testTheTitleUrl()
{
// create a browser
$browser = create_browser();
// open baidu via GET
$browser->get('https://www.baidu.com/');
// get title
$this->assertEquals('百度一下,你就知道', $browser->getTitle());
// get the current URL
$this->assertEquals('https://www.baidu.com/', $browser->getCurrentURL());
// close the browser
$browser->quit();
}
}
注意:文件名和类名要以Test结尾,而其中的测试函数要以test开头,这样子方便phpunit查找测试用例。
此外,其中create_browser是放在bootstrap.php中的一个辅助函数:
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
/**
* @return RemoteWebDriver
*/
function create_browser(){
return RemoteWebDriver::create('http://localhost:4444/wd/hub',
DesiredCapabilities::chrome(),
5000);
}
第六步,跑一下测试用例试试
phpunit --bootstrap tests/bootstrap.php tests/sample/BaiduTest.php

Nice~ 成功跑通~
最后,用phpunit.xml定义测试套,跑起来更便捷~
老是敲那么一长串的命令也挺麻烦的,要是能像make一样只要敲一个make就搞定就好了 —— phpunit刚好提供了一个phpunit.xml让我们能定义常用的测试套之类的选项。
详细的介绍在官方文档中已经很详细了,我这里就不啰嗦了。只秀个我认为较为简洁有效的配置:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "tests/bootstrap.php" >
<testsuites>
<testsuite>
<directory>tests/sample</directory>
</testsuite>
<!-- ... -->
</testsuites>
</phpunit>
把上述内容写入phpunit.xml中后,跑phpunit就变得非常简单了,一个phpunit就搞定了:

Well done. 收工~
使用PHPUnit + Selenium进行自动化测试的更多相关文章
- [软件测试基础2]基于selenium的自动化测试
这次上机我们主要使用Selenium进行自动化测试,首先我们需要下载selenium-java的依赖项. 若使用maven管理项目,则在.pom文件中加入如下依赖项: <dependency&g ...
- selenium + python 自动化测试环境搭建
selenium + python 自动化测试 —— 环境搭建 关于 selenium Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操 ...
- Selenium终极自动化测试环境搭建(二)Selenium+Eclipse+Python
Selenium终极自动化测试环境搭建(二)Selenium+Eclipse+Python 前面举例了Selenium+Eclipse+Junit+TestNG自动化测试环境的搭建,在前一篇的基础上, ...
- Selenium终极自动化测试环境搭建(一) Selenium+Eclipse+Junit+TestNG
Selenium终极自动化测试环境搭建(一)Selenium+Eclipse+Junit+TestNG 第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/tec ...
- selenium+python自动化测试
F12: 右键 选择复制 path 在selenium+python自动化测试(一)–环境搭建中,运行了一个测试脚本,脚本内容如下: from selenium import webdriver ...
- Selenium 2自动化测试实战
Selenium 2自动化测试实战 百度网盘 链接:https://pan.baidu.com/s/1aiP3d8Y1QlcHD3fAlEj4sg 提取码:jp8e 复制这段内容后打开百度网盘手机Ap ...
- 【转载】selenium与自动化测试成神之路
Python selenium —— selenium与自动化测试成神之路 置顶 2016年09月17日 00:33:04 阅读数:43886 Python selenium —— selenium与 ...
- Selenium 页面自动化测试 面试 问题汇总
1. 专业技术 在学习完Selenium的大部分接口或者方法之后,你可能会去面试自动化测试,主要是Selenium的自动化测试.下面这些问题总结,可能会对你有所帮助. 什么是Selenium? S ...
- selenium + python自动化测试unittest框架学习(五)webdriver的二次封装
因为webdriver的api方法很长,再加上大多数的定位方式是以xpath方式定位,更加让代码看起来超级长,为了使整体的代码看起来整洁,对webdriver进行封装,学习资料来源于虫师的<se ...
随机推荐
- Python—装饰器
装饰器 1.普通函数 #简单的函数和调用 def a1(): print("i am zhangsan") def a2(): print("i am lisi" ...
- 如何做个简单安卓App流程
有同学做毕业设计,问怎样做个简单安卓App流程,我是做服务端的,也算是经常接触app,想着做app应该很简单吧,不就做个页面,会跳转,有数据不就行了,我解释了半天,人家始终没听懂,算了,我第二天问了下 ...
- 【java报错】Could not instantiate listener
这个错误以前出现过好几次,莫名其妙的出现,莫名其妙的解决掉...昨天好好的,今天又出现了,记下来 2015-03-03 09:38:45.105:INFO:oejs.Server:jetty-8.1. ...
- TextGrocery中文文本分类处理
详细使用说明:http://textgrocery.readthedocs.io/zh/latest/index.html TextGrocery是一个基于LibLinear和结巴分词的短文本分类工具 ...
- lievent源码分析:evbuffer
struct evbuffer定义在evbuffer-internal.h文件中. evbuffer结构内部保存一个以evbuffer-chain结构为节点的链表,evbuffer内部有两个分别指向首 ...
- struts_表单得到数据
在大家学习struts表达式语言的时候经常会遇到,从表单的提交上面得到数据, 而如何将表单的数据得到呢? 下面就介绍其中的一种方式: :以类的方式进行注入我们以login为例子 首先可以在struts ...
- UseFul Collection
....... 啦啦啦,test... http://bbs.csdn.net/topics/380175613 http://zhidao.baidu.com/question/580940892. ...
- 【leetcode❤python】 396. Rotate Function
#-*- coding: UTF-8 -*- #超时# lenA=len(A)# maxSum=[]# count=0# while count ...
- c读写文件
#include<stdio.h> void main(void) { // locate ]; scanf("%s", filename); getchar(); / ...
- 解决方法 test: database removal failed: ERROR: database "test" is being accessed by other users
select * from pg_stat_activity where datname='test'; 找出哪个进程用了这个数据库,然后删除这个进程(pid) kill -9 47182 然后再删除 ...