使用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 ...
随机推荐
- KTV项目总结
KTV项目总结 大约一个星期前吧,老湿说我们要开始做KTV项目了,说是KTV项目是贯穿整个学的内容的,会所的,要我们认真去对待,一开始,第一天搭前台界面,总是有不会的,要去问问,这个要用什么控件啊,用 ...
- noi 2989 糖果
题目链接:http://noi.openjudge.cn/ch0206/2989/ 首先,数据很大,直接用背包会re. 这里增加的是对%k 的余数维度.f[i][j] 表示前 i 种糖果取到总颗数模 ...
- OWASP top10
PhishTank 是互联网上免费提供恶意网址黑名单的组织之一,它的黑名单由世界各地的志愿者提供,且更新频繁. 1.XSS 1.1. XSS简介 跨站脚本攻击,英文全称是Cross Site Scri ...
- QA、Selenium WebDriver (Q&A)
Q:启动IE浏览器时突然报下面错误,不能正常使用. WebDriverException: Message: Unexpected error launching Internet Explo ...
- Android的UI设计
一.Android控件 1.TextView 属性:id.width.height.gravity(对齐方式).textSize(文字大小).textColor(文字颜色) 2.Button 属性:i ...
- 151102SQL语句
$condition = array($pk=>array('in',explode(',',$id)));//??? $this->model->where($condition) ...
- linuxmint 默认不启动图形界面
1.参考:http://garyu.blog.51cto.com/2838408/513772 修改/etc/X11/default-display-manager文件 该文件的内容一般是一个路径,比 ...
- metasploit模块功能介绍
metasploit的模块构成及功能分析 转载自----http://forum.cnsec.org/thread-94704-1-1.html 今天我们介绍一下metasploit的基础架构和 市 ...
- iOS 二维数组排序小算法
NSArray *tmp = @[@[@(1), @(2), @(3), @(4), @(5)], @[@(6), @(7), @(8), @(9), @(10 ...
- Android 自定义波浪动画 --"让进度浪起来~"
原文链接:http://www.jianshu.com/p/0e25a10cb9f5 一款效果不错的动画,实现也挺简单的,推荐阅读学习~ -- 由 傻小孩b 分享 waveview <Andro ...