12
返回列表 发新帖
楼主: justforregister

Test web applications with Selenium RC

[复制链接]
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
11#
 楼主| 发表于 2010-8-27 23:57 | 只看该作者
Viewing the test results
                Now that you have the first example test written, you can see it in action by starting                         the Selenium server and running the test as a standard JUnit unit test. Start the                         Selenium server by typing the following command:               
java -jar selenium-server.jar

Running the server from the IDE                        For a fully integrated experience, you can set up an external tool configuration in                         Eclipse to start the Selenium server directly from Eclipse. Click Run                         > External Tools > External Tool Configurations to set up the                         configuration. Simply enter the command for starting the server into a new                         tool configuration.               


                After you have the Selenium server started, you can run the unit test by                right-clicking the IndexTest.java file, and then clicking Run As                    > JUnit Test. It may take a while as the Selenium server                starts up an instance of your browser and runs the tests. When the test is                finished, you will see the same output in Eclipse as with normal unit                tests.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
12#
 楼主| 发表于 2010-8-27 23:57 | 只看该作者
Digging deeper
                Now that you've used Selenium IDE to create a simple test and exported it as a Java                file, create a more complicated test to verify the functions of the                enterInfo.jsp page. The example test is shown in Listing 4.

Listing 4. The JUnit test for testing enterInfo.jsp
       
package com.example.mywebapp.tests;

import org.junit.BeforeClass;
import org.junit.Test;

import com.thoughtworks.selenium.SeleneseTestCase;

public class EnterInfoTests extends SeleneseTestCase {
   
    @BeforeClass
    public void setUp() throws Exception {
        setUp("http://localhost:8080/tested-webapp/index.jsp", "*firefox");
    }
   
    @Test
    public void testBadDate() {
        doLogin();
        selenium.type("name", "User");
        selenium.type("birthdate", "@#$#@");
        selenium.click("//input[@value='Submit']");
        selenium.waitForPageToLoad("30000");
        verifyTrue(selenium.isTextPresent("Please enter a valid date"));
    }
   
    @Test
    public void testValidDate() {
        doLogin();
        selenium.type("name", "User");
        selenium.type("birthdate", "12/2/1999");
        selenium.click("//input[@value='Submit']");
        selenium.waitForPageToLoad("30000");
        verifyFalse(selenium.isTextPresent("Please enter a valid date"));
    }
   
    private void doLogin()
    {
        selenium.open("/tested-webapp/index.jsp");
        selenium.type("username", "user");
        selenium.type("password", "secret");
        selenium.click("//input[@value='Login']");
        selenium.waitForPageToLoad("30000");
    }
}

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
13#
 楼主| 发表于 2010-8-27 23:57 | 只看该作者
The example test used the LoginTest class shown in                        Listing 3 as a starting point. The doLogin()                        function has the code to log you in to the application, which is used at the start                         of the tests to get you to the proper point.               
                The testBadDate() method is used to enter bad values                         into the birthdate field on the form, and then submit it. It                         verifies that the appropriate error message is displayed if the date is incorrect.                        The testValidDate() method tests the valid date on the                         form and makes sure the message that provides the user's age is correct.               
                Using the power of Java technology available to you in JUnit, you can loop through                         examples, add conditions to your tests, and expect exceptions. See the links in                         Resources to learn more about JUnit and unit testing.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
14#
 楼主| 发表于 2010-8-27 23:58 | 只看该作者
Automating the tests
                Now that you have the tests running in the Eclipse IDE, with the help of an Apache                Ant script and a few targets, you can automate the tests completely. After                you have this Ant script, you can use tools such as Hudson or                CruiseControl (see Resources) to run these tests                continuously.
                To automate the test, you can use an Ant script that employs the JUnit target to                execute the Selenium RC tests. The script is shown in Listing 5.

Listing 5. An Ant script for running the Selenium tests
       
<project name="tested-webapp-tests" default="run-tests" basedir=".">

    <property name="selenium.server.jar" value="path/to/selenium-server.jar" />
    <property name="src" value="${basedir}/src" />
    <property name="build" value="${basedir}/bin" />
    <property name="lib" value="${basedir}/lib" />

    <path id="classpath">
        <fileset dir="${lib}" includes="**/*.jar" />
    </path>

    <target name="start-selenium-server">
        <java jar="${selenium.server.jar}" fork="true" spawn="true">
            <arg line="-timeout 30" />
        </java>
    </target>

    <target name="compile-tests">
        <javac srcdir="${src}" destdir="${build}" fork="true" />
    </target>

    <target name="run-tests" depends="compile-tests">
        <junit printsummary="yes">
            <classpath>
                <path refid="classpath" />
            </classpath>

            <batchtest fork="yes">
                <fileset dir="${src}">
                    <include name="**/*Tests.java" />
                </fileset>
            </batchtest>
        </junit>
        <echo message="Finished running tests." />
    </target>

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

    <target name="run-all">
        <parallel>
            <antcall target="start-selenium-server">
            </antcall>
            <sequential>
                <echo taskname="waitfor" message="Wait for proxy server launch" />
                <waitfor maxwait="1" maxwaitunit="minute" checkevery="100">
                    <http url="http://localhost:4444/selenium-server/
                        driver/?cmd=testComplete" />
                </waitfor>
                <antcall target="run-tests">
                </antcall>
                <antcall target="stop-selenium-server">
                </antcall>
            </sequential>
        </parallel>
    </target>

</project>

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
15#
 楼主| 发表于 2010-8-27 23:58 | 只看该作者
The Ant script has three targets. The first target, start-selenium-server,                        starts the Selenium server in forked and spawned mode so that it runs in the                         background. The run-tests target actually executes                         the JUnit tests, and the stop-selenium-server target                         stops the server by calling a URL that sends the server a shutdown command.               
                Using an Ant script like this one, you can run the Selenium tests in an automated                         fashion by scheduling them or using the Ant script in a continuous integration                         tool. See Resources for links to more information about                         continuous integration builds.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
16#
 楼主| 发表于 2010-8-27 23:58 | 只看该作者
Running the tests in other browsers
                As written so far in this example, the tests are executed in Firefox. However, there                may be times when it is necessary to test the web application in other                browsers to make sure functions work across browsers.
                You may have noticed that when the Selenium tests where set up in the                         setUp() method of the JUnit test,                         *chrome was passed as the second parameter to the                         parent's setUp() method. This parameter starts an                        instance of the Firefox browser.               
                To run the test for a different browser, simply supply the name of the browser as                         the second argument. For example, *iexplore uses                        Internet Explorer as the browser instead of Firefox. Use *opera                        for the Opera browser or *safari for Apple Safari.               
                Note that the browser must be supported on the operating system on which you are                running the tests. You will get exceptions and, therefore, failed tests if                you attempt to execute tests for a browser that doesn't exist on your                operating system, such as *safari or *iexplore on a system running Linux®.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
17#
 楼主| 发表于 2010-8-27 23:58 | 只看该作者
Conclusion
                This article introduced Selenium and two of its components: Selenium IDE and                         Selenium RC. Selenium IDE is an easy way to start writing tests for your web                         applications. Selenium RC is a component that you can use to add features to and                        automate your tests.               
                Automated web application testing from the browser perspective can be a great way                         to augment existing unit tests that cover other parts of your application. With                         automated testing, developers can get faster feedback on problems, and testing                         regressions becomes faster.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
18#
 楼主| 发表于 2010-8-27 23:58 | 只看该作者
Resources
Learn

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表