Arquillian测试框架快速上手教程(四)- 使用Arquillian + Drone + Selenium + Graphene 进行Web自动化测试
本系列教程共五篇,分别是:
一、环境搭建、容器适配、单元测试
二、JBoss Forge、ShrinkWarp快速指南
三、使用 Arquillian 进行Java持久化测试
四、使用 Arquillian + Drone + Selenium + Graphene 进行Web自动化测试
五、使用 Arquillian 在云端进行测试
本文所涉及到的内容有:
1. Arquillian
2. Arquillian Drone
3. Arquillian WebDriver(Selenium WebDriver)
4. Arquillian Graphene
5. ShrinkWrap
前言
开发Web应用时,最传统也是最常用的测试方法,就是构建、部署、打开浏览器、用鼠标点相关链接,最后通过人眼去看来判断功能是否正常。由于人操作的速度是远远慢于程序执行速度的,所以这种人工测试的方式其实会占用大量的时间。除此之外人还会偷懒,可能测3次、4次后就不愿意再重复这样的过程了。幸运的是,现在,我们可以使用程序代码来让这一过程自动化,即可以通过编写代码的方式来模拟人在使用浏览器时点击链接、填写表单、提交按钮,甚至是等待页面载入的过程。
在Arquillian之前,有一个名为 Selenium 的工具能够实现测试自动化,并支持Java、PHP、JavaScript、Ruby、Python等主流编程语言。但其在使用时代码量较大,过程比较繁琐(稍后将通过对比来说明这一点)。Arquillian Drone 和 Arquillian Graphene 简化了Web应用功能测试的编写,它们通过更高一层的封装,并利用Java注解的特性,减少了测试代码量。下面我们就通过实例工程来看看如何使用。
客户模式(Client Mode)简介
In the test, we’ve injected the instance of the bean, then asserted that the injection has occurred. Arquillian first enriches the archive with the test infrastructure. It then connects to the server to deploy the archive and execute the test method inside of the container by launching the JUnit test runner a second time inside that environment. Finally, Arquillian retrieves the results from that remote execution. This example demonstrates the default run mode of Arquillian: in-container. In a sense, the local JUnit runner is a client of the container, being driven by Arquillian.
The other run mode in Arquillian is the client run mode. In this mode, Arquillian deploys the test archive ‘as is’ to the container. It then allows the tests to run in the same JVM as the test runner. Now your test case is a client of the container. The test no longer runs inside the container, yet Arquillian still handles the life cycle of the container as well as deployment of the test archive. It’s the ideal mode for web UI testing.
用Arquillian写功能测试必须使用 Client Mode,其方法有两种:
@Deployment(testable = false)public static Archive<?> createDeployment() {// ... ...}
2. 在@Test 注解方法上增加 @RunAsClient注解。如:
@Test@RunAsClientpublic void should_run_as_client() {// executed from the client JVM}
其中方法2灵活性较强,因为我们可以把一部分@Test方法指定为Client Mode,另一部分不指定。如果使用方法1,那么所有的@Test都会以客户模式运行。
Graphene 和 Drone
配置依赖,以Seam项目为例
<dependencies><!-- arquillian and Drone --><dependency><groupId>org.jboss.arquillian</groupId><artifactId>arquillian-bom</artifactId><version>1.1.2.Final</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.jboss.arquillian.extension</groupId><artifactId>arquillian-drone-bom</artifactId><version>1.2.3.Final</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.jboss.arquillian.selenium</groupId><artifactId>selenium-bom</artifactId><version>2.39.0</version><type>pom</type><scope>import</scope></dependency>
注意,顺序非常重要,务必把这3个dependency作为 dependencyManagement节点下的前3个子节点. 然后在dependencyManagement 外的 dependencies中添加以下内容:
<dependency><groupId>org.jboss.arquillian.graphene</groupId><artifactId>graphene-webdriver</artifactId><version>2.0.1.Final</version><type>pom</type><scope>test</scope></dependency>
注意,版本非常重要,如果版本号与上面不一致,很可能就会出现各种奇怪的错误。这里插一个小故事,如果把 org.jboss.arquillian:arquillian-bom:1.1.2.Final 改为 org.jboss.arquillian:arquillian-bom:1.1.3.Final,即换一个新一点的版本,那么在运行时 WebDriver 就会奇怪地扔 NullPointerExcepton异常。我在StackOverflow上提问,结果最后被证明是一个Bug, Arquillian 项目组的人看到后已经提交了Bug报告,预计会在下一个版本中修复。
编写测试代码
@Deployment(testable = false)public static Archive<?> createDeployment() {EnterpriseArchive er = ShrinkWrap.create(ZipImporter.class, "ftc.ear").importFrom(new File("../ftc-ear/target/ftc.ear")) // 将ftc.ear文件导入.as(EnterpriseArchive.class);WebArchive web = er.getAsType(WebArchive.class, "ftc-web.war");web.addClasses(BeanTest.class);return er;}
@Droneprivate WebDriver browser; // 注入 WebDriver,用来操作浏览器@ArquillianResourceprivate URL url; // 得到项目部署到JBoss上时的访问地址@FindBy(id = "username")private WebElement username;@FindBy(id = "password")private WebElement password;@FindBy(id = "submit")private WebElement submitBtn;@FindBy(id = "home-page-identifier")private WebElement page;@Test @InSequence(1)public void isTestReady() {Assert.assertNotNull(browser);Assert.assertNotNull(url);}@Test @InSequence(2)public void functionalTest() {browser.get(url.toExternalForm()); // 打开浏览器,导航至刚刚部署的项目username.sendKeys("Neo"); // 在username的input标签上输入 "Neo"password.sendKeys("123456"); // 在密码框中输入 "123456"Graphene.guardHttp(submitBtn).click(); // "点击" 登陆按钮//username.submit(); // 另一种“点击”方式Assert.assertEquals("领域知识框架构建", page.getText().trim()); // 判断是否真的导航到了home.xhtml}
}
说明:
指定测试浏览器
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://jboss.org/schema/arquillian"xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd"><extension qualifier="webdriver"><property name="browser">firefox</property> <!-- 使用firefox浏览器 --></extension></arquillian>
然后为了方便起见,我们可以在pom.xml 文件中新建多个 profile,这样在运行测试的时候只需要指定不同的profile就可以在不同的浏览器中进行测试了。
<profile><id>firefox</id><properties><browser>firefox</browser></properties></profile><profile><id>chrome</id><properties><browser>chrome</browser></properties></profile>... ...
运行测试
mvn clean install -Dmaven.test.skip=true
mvn test -Parq-jbossas-remote
之后,我会们发现浏览器一闪而过,测试成功。
Arquillian测试框架快速上手教程(四)- 使用Arquillian + Drone + Selenium + Graphene 进行Web自动化测试相关推荐
- Angular 初学者快速上手教程
课程介绍 本课程是一个系列基础教程,目标是带领读者上手实战,课程以新版本 Angular 的 3 个核心概念作为主线:组件.路由.模块,加上业务开发过程中必须用到的特性:工具.指令.表单.RxJS.i ...
- Debussy 快速上手教程
本文为转载,刚瞄了一眼,应该是在LINUX下的应用快速上手教程.Debussy是个很强大的看V代码及Debug V代码工具.你能方便地看到信号从哪里来到哪里去,比较强大.简单地用,用起来也比较简单.在 ...
- 前端测试框架Jest系列教程 -- Expect(验证)
写在前面 在编写测试时,我们通常需要检查值是否满足某些条件,Jest中提供的expect允许你访问很多"Matchers",这些"匹配器"允许您验证不同的东西. ...
- 申请 Let's Encrypt 数字证书,并安装cerbot快速上手教程~~
申请 Let's Encrypt 数字证书,并安装cerbot快速上手教程~~ Let's Encrypt SSL证书介绍 如今,越来越多的网站已经由HTTP迁移到HTTPS安全协议了.HTTPS不但 ...
- 【Endnote】EndnoteX9快速上手教程
想得美,自己慢慢摸索才是硬道理,哪有什么快速上手教程,链接在下面 链接:https://pan.baidu.com/s/1Vtnaz90Iwp3I17M8ijxMWg 密码:ems7
- 嵌入式SQL编程快速上手教程
嵌入式SQL编程快速上手教程 声明:我这里标题虽是<嵌入式SQL编程快速上手教程>,但只是嵌入式SQL编程的冰山一角罢了,我会通过举一道简单例题来教小白快速上手嵌入式SQL编程 第一:题目 ...
- 基于Selenium实现的web自动化测试框架
目录 一.什么是Selenium? 二.自动化测试框架 三.自动化框架的设计和实现 四.需要改进的模块 五.总结 一.什么是Selenium? Selenium是一个基于浏览器的自动化测试工具,它提供 ...
- 基于Selenium+Python的web自动化测试框架(附框架源码+项目实战)
目录 一.什么是Selenium? 二.自动化测试框架 三.自动化框架的设计和实现 四.需要改进的模块 五.总结 总结感谢每一个认真阅读我文章的人!!! 重点:配套学习资料和视频教学 一.什么是Sel ...
- 基于python的界面自动化测试-基于Selenium+Python的web自动化测试框架
一.什么是Selenium? Selenium是一个基于浏览器的自动化测试工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.S ...
- 基于Selenium+Python的web自动化测试框架
一.什么是Selenium? Selenium是一个基于浏览器的自动化测试工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.S ...
最新文章
- Breakpad查C++崩溃问题
- thinkphp文件上传在$_files里面看不到
- sharepoint column中的图片链接不能显示为图片
- 基础的shell编程问题(一)
- 05-sqlyog的安装与基本使用
- 某大型银行深化系统技术方案之十一:核心层之业务活动监控
- 最简单的基于FFmpeg的内存读写的例子:内存播放器
- 大数据开发笔记(四):Hive分区详解
- 云服务器 信息安全,云服务器怎么保证信息安全
- 普元EOS UTP自动化测试 关闭工作流
- VMplayer创建虚拟机
- python处理地震sac数据_[数据处理] 地震数据及相关软件概述
- zabbix server更换IP地址
- 曾经写过得太监小说2《天道》
- Android 颜色透明度大全
- mantis使用介绍
- 基于OpenCV的图像透视变换详解(从理论到实现再到实践)
- DHCP spoofing,DHCP snooping,傻傻分不清
- VMware+Ubuntu安装使用说明
- FAT32文件删除与恢复