使用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 ...
随机推荐
- CentOS7安装mysql5.6.26
linux系统CentOS7 到http://mirrors.sohu.com/mysql/下载想要的mysql版本 这里用到的是 mysql-5.6.26-linux-glibc2.5-x86_64 ...
- Visual Studio 2015简体中文企业版/专业版下载+有效激活密钥
Visual Studio 2015是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.所写的目标代码适用于微软支持的所有 ...
- 第二章:Posix IPC
2.1:概述 以下三种类型的IPC合称为“Posix IPC”: Posix消息队列 Posix信号量 Posix共享内存区 Posix IPC在访问它们的函数和描述它们的信息上有一些类似点.本章讲述 ...
- selenium操作滚动条方法
/*** 滚动条滚到最下方,和滚到指定位置*/@Test(priority =1 ) public void scrollingToBottomo(){ //使用JavaScri ...
- spring+mybatis+druid+mysql+maven事务配置
1.首先pom.xml文件里面需要用到的jar配置: <!-- spring事务,包含了@Transactional标注 --> <dependency> <groupI ...
- python virtualenv
一 安装 pip install virtualenvwrapper - 把下面这句加到~/.bash_profile里面,如不嫌麻烦,也可以每次都手动执行.source /usr/local/bi ...
- C# 使用lock关键字lock不同的对象
c# lock关键字的本质 是调用Monitor.Enter(object obj)并且在finally的时候调用Monitor.Exit(obj) 在obj是不同数据类型的时候会出现不同的情况 1. ...
- 深入浅出设计模式——状态模式(State Pattern)
模式动机 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做状态,这样的对象叫做有状态的 (stateful)对象,这样的对象状态是从事先定义好的一系列值中取出的.当一个这样的 ...
- LTE Module User Documentation(翻译10)——网络连接(Network Attachment)
LTE用户文档 (如有不当的地方,欢迎指正!) 16 Network Attachment(网络连接) 正如前面章节 Basic simulation program 所述,连接用户到基站时通过调 ...
- 翻译:打造基于Sublime Text 3的全能python开发环境
原文地址:https://realpython.com/blog/python/setting-up-sublime-text-3-for-full-stack-python-development/ ...