如何用 Selenium RC 进行自动化测试
1、打开eclipse,新建一个项目tests,点击File—>New —> Java Project,输入Project name 为 tests,JRE选择Use a project specific JRE,选择jre6,点击Finish。
2、添加selenium java 文件运行需要的jar包。选中工程名tests右键—>Build Path—> add external archive ,选择需要添加的jar包。其中Junit-4.10.jar:在下载的junit 4压缩包里;Selenium-java-client-driver.jar:在下载的selenium-remote-control-1.0.3文件夹下:.(....\selenium-remote-control-1.0.3\selenium-java-client-driver-1.0.1\)Selenium-server.jar:在我们下载的selenium-remote-control-1.0.3文件夹下:.....\selenium-remote-control-1.0.3\selenium-server-1.0.3\
3、jar包加载后的效果图如下:
4、用selenium IDE 录制脚本,并将代码导出为junit 4类型的代码,保存为login.java。
5、在tests/src下,新建一个com.example.tests的包。
6、将selenium IDE里导出的login.java文件,导入到com.example.tests包里。代码内容如下:packagecom.example.tests;importcom.thoughtworks.selenium.*;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importstaticorg.junit.Assert.*;importjava.util.regex.Pattern;publicclasslogin{privateSeleniumselenium;@BeforepublicvoidsetUp()throwsException{selenium=newDefaultSelenium("localhost",4444,"*chrome","http://test.51ebill.com:55554/ebillwww/manage/login.in");selenium.start();}@TestpublicvoidtestLogin()throwsException{selenium.open("/ebillwww/manage/login.in");selenium.type("id=name","yuguotai");selenium.type("id=psw","88@88.com");selenium.click("id=submitForm");selenium.waitForPageToLoad("30000");selenium.click("link=[退出]");selenium.waitForPageToLoad("30000");}@AfterpublicvoidtearDown()throwsException{selenium.stop();}}
7、启动selenium IDE 服务:开始—>运行 —>cmd,打开命令提示符。
8、cd 定位到…selenium-remote-control-1.0.3\selenium-server-1.0.3>目录下。
9、输入:java -jar selenium-server.jar回车,selenium IDE 服务就启动成功了。
10、使用eclipse运行login.java文件,当浏览器为默认chrome浏览器时,无法调用。
11、在浏览器里输入为空,继续debug login.java文件时,左下角的错误提示信息里就会显示支持的浏览器的类型。选择合适的浏览器写入代码里,进行debug,直到能够正确调到浏览器。
12、当浏览器选择为iexplore时,能够正确调到浏览器进行运行。
13、运行时与运行结果如下图