本系列教程共五篇,分别是:
一、环境搭建、容器适配、单元测试
二、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注解的特性,减少了测试代码量。下面我们就通过实例工程来看看如何使用。
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() {
// ... ...
}
@Test
@RunAsClient
public void should_run_as_client() {
// executed from the client JVM
}
org.jboss.arquillian
arquillian-bom
1.1.2.Final
pom
import
org.jboss.arquillian.extension
arquillian-drone-bom
1.2.3.Final
pom
import
org.jboss.arquillian.selenium
selenium-bom
2.39.0
pom
import
org.jboss.arquillian.graphene
graphene-webdriver
2.0.1.Final
pom
test
@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;
}
@Drone
private WebDriver browser; // 注入 WebDriver,用来操作浏览器
@ArquillianResource
private 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
}
}
firefox
firefox
firefox
chrome
chrome
... ...
mvn clean install -Dmaven.test.skip=true
mvn test -Parq-jbossas-remote