在持续集成系统中使用Selenium-RC

在持续集成系统中使用Selenium-RC
关键字: selenium

本页面是关于在一个持续集成的系统中使用 Selenium-RC - 通过命令行、Ant 或者 TestNG 来运行 Selenium 测试。

首先,我们收集信息。然后我们把它们合理的整理好。关键问题是有很多种方式启动测试并且收集结果, 所以你不得不配合好你所使用的工具 (ANT, TestNG, CC, ...)

如此使用 Selenium-RC 就需要了解很多事情:
Selenium 服务器 (selenium-server.jar) 是实际上启动 Web 浏览器的程序。这非常重要,因为如果你想通过命令行参数传递参数给浏览器,你就要通过改变服务器的环境来做。
使用 Xvfb (X Windows Virtual Frame Buffer):如果你想让 Selenium 运行在 Unix 服器上 - 而不需要使用 X Windows 显示 - 或者你不想看到 Web 浏览器弹出,使用 xvfb。这是一个仅运行在内存里面的 X Server。
ANT

启动服务器用的 target :
<java jar="${selenium-server.jar}" fork="true" spawn="true" />

停止服务器用的 target (引自 Forum thread) :
<target name="stop-server">
    <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"
        dest="result.txt" ignoreerrors="true" />
    <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
</target>

用于启动 Selenese 测试的 target (引自 Forum thread) :
<target name="runSeleniumTests">
    <java jar="${acceptanceTestLibDir}/selenium-server.jar" fork="true">
        <arg line="-htmlSuite &quot;${firefox}&quot;"/>
        <arg line="&quot;${baseURL}&quot;"/>
        <arg line="&quot;${acceptanceTestListDir}/testSuite.html&quot;"/>
        <arg line="&quot;${acceptanceTestReportDir}/results.html&quot;"/>
        <arg line="-timeout 30"/>
    </java>
</target>

一个完整的例子 :
<?xml version="1.0" encoding="UTF-8"?>
<project name="Run Test" default="run_test" basedir=".">

    <property name="test.dir" value="src\test" />

    <property name="testLibDir" value="lib" />

    <path id="run.cp">
        <pathelement path="build"/>
        <fileset dir="build/">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="lib"/>
        <fileset dir="lib/">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="run_test" description="Start Proxy ; Run TestNG ; stop Proxy">
        <parallel>
            <antcall target="start-server"></antcall>
            <sequential>   
                <echo taskname="waitfor" message="Wait for proxy server launch" />
                <waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
                    <http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete"/>
                </waitfor>
                    <antcall target="run_testNG"></antcall>
                    <antcall target="stop-server"></antcall>
            </sequential>
        </parallel>
    </target>

    <target name="run_testNG" description="Run TestNG">   
        <testng classpathref="run.cp" haltOnfailure="false">
            <xmlfileset dir="." includes="testng.xml" />
        </testng>
    </target>

    <target name="start-server">
        <java jar="lib/selenium-server.jar" fork="true">
            <arg line="-timeout 30"/>
            <jvmarg value="-Dhttp.proxyHost=proxy.corporate.com"/>
            <jvmarg value="-Dhttp.proxyPort=3128"/>
        </java>
    </target>

    <target name="stop-server">
        <get taskname="selenium-shutdown"
            src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"   
            dest="result.txt" ignoreerrors="true" />
        <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
    </target>

    <taskdef resource="testngtasks" classpath="lib/testng-5.0-jdk15.jar" />

</project>


MAVEN 2

想要在集成测试 前/后 启动/停止 Selenium RC,编辑 pom.xml 包含下面的内容: (引自 Maven 用户列表 此帖 )。
<?xml version="1.0"?>
<project>
    <dependencies>
        <dependency>
            <groupId>org.openqa.selenium.client-drivers</groupId>
            <artifactId>selenium-java-client-driver</artifactId>
            <version>${selenium-version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.openqa.selenium.server</groupId>
            <artifactId>selenium-server</artifactId>
            <version>${selenium-version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>dependency-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.openqa.selenium.server</groupId>
                                    <artifactId>selenium-server</artifactId>
                                    <version>${selenium-version}</version>
                                    <type>jar</type>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <destFileName>selenium-server.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>start-selenium</id>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <tasks>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>
                                <echo taskname="selenium" message=" Starting Selenium Remote Control                        "/>
                                <echo taskname="selenium" message=" Please make sure that FireFox executable is on the PATH "/>
                                <java jar="${project.build.directory}/selenium-server.jar" fork="yes" spawn="true"/>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>                               
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-selenium</id>
                        <phase>post-integration-test</phase>
                        <configuration>
                            <tasks>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>
                                <echo taskname="selenium" message=" Shutting down Selenium Remote Control                   "/>
                                <echo taskname="selenium" message=" DGF Errors during shutdown are expected                 "/>
                                <get taskname="selenium"
                                     src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"
&nbs ...
posted @ 2008-11-02 23:31  Earl_86  阅读(375)  评论(0编辑  收藏  举报