`
gq913
  • 浏览: 167325 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

阅读更多

本页面是关于在一个持续集成的系统中使用 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"
                                     dest="result.txt" ignoreerrors="true"/>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>  
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>openqa</id>
            <url>http://maven.openqa.org</url>
        </repository>
    </repositories>

    <properties>        
        <selenium-version>0.8.1</selenium-version>
    </properties>
</project>

Maven2 用户: Binil Thomas

JAVA

启动服务器的另一种方法 (引自 Forum thread) :

当然,把 selenium-server.jar 加到 classpath 中去,创建一个 SeleniumServer 对象并 .start() 它。

 这典型应用在 TestNG 的 SetUp() 中。

LINUX 

这里有一系列的有用的 Unix 命令做这件事:

 启动 Xvfb 作为 display 1:

启动一个 Selenium Server 并且并在此 display (display 1) 上启动 Web 浏览器:

截取 xvfb display 下的一个屏幕快照看看发生了什么 (调试模式下很有用):

注意: '%' 是 Unix shell 提示符。'DISPLAY=:1" 是一个命令行设置的环境变量。你可以很简单的设置 DISPLAY 到例如 foo.bar.com:2,代表 Selenium Web 浏览器会出现在 foo.bar.com 的第二个 X Windows display 上。

Linux 用户 : obinho.


This information is adapted from the following pages:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics