selenium test

1,下载selenium.把selenium-java-client-driver文件下的selenium-java-client- driver.jar和selenium-server下的selenium-server.jar拷到项目的lib下并添加到classpath中。

2,在ant 中启动selenium,如下:

<target name="start.selenium.server" description="start selenium proxy server">
        <if>
            <not>
                <socket server="localhost" port="4444" />
            </not>
            <then>
                <echo message="start Selenium Proxy Server..." />
                <java dir="${lib.dir}" jar="${lib.dir}/selenium-server.jar" spawn="true" fork="true" />
                <waitfor maxwait="10" maxwaitunit="minute" checkevery="1" checkeveryunit="second">
                    <and>
                        <socket server="localhost" port="4444" />
                    </and>
                </waitfor>
            </then>
        </if>
    </target>

其中有用到if命令,不是ant支持的,所以必须把ant-contrib-1.0b3.jar拷到项目中,并在ant中添加如下代码:

<taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${basedir}/../tomcat/lib/ant-contrib-1.0b3.jar" />
        </classpath>
</taskdef>

3,写一个basefunctiontest

public class BaseFunctionalTestCase {
    private static final int CLICK_AND_WAIT_TIME = 30 * 1000;
    protected static Selenium selenium;

    static {
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost:4444");
        selenium.start();
    }

    protected void clickAndWait(String locator) {
        selenium.click(locator);
        selenium.waitForPageToLoad(String.valueOf(CLICK_AND_WAIT_TIME));
    }

    protected void openWelcomePage() {
        selenium.open("http://localhost/");
    }
}

4,一个测试例子:
public class icbcServerTest extends BaseFunctionalTestCase {
   
    @Test
    public void testhappyPath(){
        selenium.open("http://localhost:8080/icbc-server/input.html");
        selenium.type("id", "1");
        selenium.type("money", "1000");
        selenium.click("submit");
        selenium.waitForPageToLoad("6000");
        assertTrue(selenium.isElementPresent("Congratulation!"));
       
    }

}
posted @ 2008-10-29 17:41  Earl_86  阅读(440)  评论(0编辑  收藏  举报