使用Maven进行硒测试自动化

今天,我想帮助您更好地管理自动GUI测试(Selenium)。 在过去,我已经看到人们处理此问题的许多不同方式。 有些人只是使用Selenium-IDE编写这些普通HTML TestCases,将其存储在HDD上的某个位置,并在需要时手动运行。 其他人甚至不使用Selenium-IDE。 他们为Example编写纯Java,并使用JUnit自动执行。 我今天的解决方案介于两者之间。

前提

  • 我想要用Selenium-IDE创建的纯HTML TestCases。 这样一来,几乎没有编程技能的人仍然可以创建它们。
  • 我希望这些GUI测试在构建过程中自动运行,因此我的CI工具可以通知我有关错误。
  • 由于测试随源一起增长,因此我还希望项目存储库中的Versioncontrol下的所有TestCases。
  • 我希望付出最少的努力,取得最高的结果。 因此,我不想从HTML TestCases中导出JUnit测试,因为它可能是重复的,并且我想坚持DRY原则。

首先,我在我的项目中为Selenium-Tests创建一个文件夹。

资料夹结构

文件夹结构

TestSuite示例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
    <title>Test Suite</title>
</head>
<body>
<table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium">
    <tbody>
    <tr>
        <td><b>Test Suite</b></td>
    </tr>
    <tr>
        <td><a href="./SomeTest1.html">SomeTest1</a></td>
    </tr>
    <tr>
        <td><a href="./SomeTest2.html">SomeTest2</a></td>
    </tr>
    </tbody>
</table>
</body>
</html>
示例测试
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="selenium.base" href=""/>
    <title>SomeTest1.html</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
    <thead>
    <tr>
        <td rowspan="1" colspan="3">SomeTest1</td>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>open</td>
        <td>/</td>
        <td></td>
    </tr>
    <tr>
        <td>waitForElementPresent</td>
        <td>//div[@id='someId']</td>
        <td></td>
    </tr>
    <tr>
        <td>click</td>
        <td>css=button.create</td>
        <td></td>
    </tr>
    <!-- Some Steps -->
    <tr>
        <td>assertText</td>
        <td>
            //div[@id='someId']
        </td>
        <td>${expectedText}</td>
    </tr>
    </tbody>
</table>
</body>
</html>
设置Web服务器

所以我有我的TestSuite。 但是我该如何运行它们? 最重要的是,它应该在Maven Build Process中运行,因此它也可以在Jenkins-CI或其他任何版本上运行。 在针对实际运行的WebApp进行测试时,这是每个定义的IntegrationTest。 在Maven中,我们有机会在集成测试阶段中运行此类测试。 如果您想了解有关Maven Build生命周期及其各个阶段的更多信息,请查看this 。 因此,我们需要某种WebServer来运行我们的WebApp,否则测试将无法进行。 WebServer应该在集成测试阶段之前启动,然后再停止。 例如,我们可以使用Tomcat7Jetty 。 在此示例中,我将使用tomcat7-maven-plugin。 我将pom.xml配置为启动Tomcat7 pre-integration-test

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <port>8080</port>
                <fork>true</fork>
            </configuration>
        </execution>
        <execution>
            <id>tomcat-shutdown</id>
            <goals>
                <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
        </execution>
    </executions>
</plugin>

如果在CI服务器上运行多个项目,则可以考虑为每个项目使用不同的端口号。

最后:运行测试

最后但并非最不重要的一点是,我们需要运行测试。 幸运的是,有可用的selenium-maven-plugin可以完成这项工作。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>selenium-maven-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <browser>*firefox</browser>
        <suite>src/test/selenium/TestSuite.html</suite>
        <startURL>http://localhost:8080</startURL>
    </configuration>
    <executions>
        <execution>
            <id>run-selenium-tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>selenese</goal>
            </goals>
        </execution>
    </executions>
</plugin>

现在,无论何时我们在控制台中执行mvn clean验证甚至mvn clean安装 ,都会运行测试并将报告存储在目标目录中。 这也将由您的CI工具完成。

结论

我们确实有完整干净的安装程序。

  • 我们有一个地方可以存储我们的测试,
  • 它们在Sourcecode和Version控件内
  • 它们可以由CI-Tools自动运行
  • 甚至非开发人员也可以添加新的TestCases

顺便说一句:如果某些事情没有按预期进行,请不要放弃。 硒似乎有点问题,有时候您必须掏点钱才能解决问题。 但它确实有效,我想通了。

参考: Be Be a Better Developer博客上的JCG合作伙伴 Gregor Riegler 提供的Maven硒测试自动化

翻译自: https://www.javacodegeeks.com/2013/07/selenium-test-automation-with-maven.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值