您的位置:首页 > 其它

自动化测试工具selenium使用介绍

2011-08-22 00:48 3827 查看
最近公司Test部门开了个讲座介绍如何做好WEB自动化测试,由于我是做开发的,但对测试人员如何工作不是很了解,就去听了下。讲座中一个测试工具还不错,于是就深入自学了下,内容如下

一、Selenium 的版本

Selenium 现在存在2个版本,一个叫 selenium-core, 一个叫selenium-rc 。

selenium-core 是使用HTML的方式来编写测试脚本,你也可以使用 Selenium-IDE来录制脚本,但是目前Selenium-IDE

只有 FireFox 版本。

Selenium-RC 是 selenium-remote control 缩写,是使用具体的语言来编写测试类。

selenium-rc 支持的语言非常多,这里我们着重关注java的方式。这里讲的也主要是 selenium-rc,因为个人还是喜欢这种

方式 :-)

二、一些准备工作

1、当然是下载 selenium 了,到 http://www.openqa.org/selenium/ 下载就可以了,记得选择selenium-rc 的版本。

2、学习一下 xpath 的知识。贴给大家个教程http://blog.csdn.net/terryzero/archive/2009/09/06/4523834.aspx

一定要学习这个,不然你根本看不懂下面的内容!

3、安装 jdk1.5

三、selenium-rc 一些使用方法

在 selenium-remote-control-0.9.0/server 目录里,我们运行 java -jar selenium-server.jar

之后你就会看到一些启动信息。要使用 selenium-rc ,启动这个server 是必须的。

当然,启动的时候有许多参数,这些用法可以在网站里看看教程,不过不加参数也已经足够了。

selenium server 启动完毕了,那么我们就可以开始编写测试类了!

我们先有个概念,selenium 是模仿浏览器的行为的,当你运行测试类的时候,你就会发现selenium 会打开一个

浏览器,然后浏览器执行你的操作。

好吧,首先生成我们的测试类:

view plaincopy to clipboardprint?

public class TestPage2 extends TestCase {
private Selenium selenium;

protected void setUp() throws Exception {
String url = “http://xxx.xxx.xxx.xxx/yyy”;
selenium = new DefaultSelenium("localhost", SeleniumServer.getDefaultPort
(), "*iexplore", url);
selenium.start();

super.setUp();

}

protected void tearDown() throws Exception {

selenium.stop();
super.tearDown();

}

}

view plaincopy to clipboardprint?

public void test1() {

selenium.open("http://xxx.xxx.xxx/yyy");

selenium.type("xpath=//input[@name='userID']", "test-user");
selenium.click("xpath=//input[@type='button']");
selenium.waitForPageToLoad("2000");
assertEquals(selenium.getTitle(), "Welcome");
}

public void test1() {

selenium.open("http://xxx.xxx.xxx/yyy");

selenium.type("xpath=//input[@name='userID']", "test-user");
selenium.click("xpath=//input[@type='button']");
selenium.waitForPageToLoad("2000");
assertEquals(selenium.getTitle(), "Welcome");
}


上面的代码是这个意思:

1、调用 selenium.open 方法,浏览器会打开相应的页面

2、使用 type 方法来给输入框输入文字

3、等待页面载入

4、看看新的页面标题是不是我们想要的。

其中的selenium 脚本 firefox插件selenium IDE 来录制生成。如果你测试IE也可以先用firefox录制然后经过略微更改便能使用。

2、测试下拉框

view plaincopy to clipboardprint?

public void test1() {

selenium.open("http://xxx.xxx.xxx/yyy");

selenium.select("xpath=//SELECT[@name='SBBUSYO']", "index=1");
selenium.click("xpath=//input[@type='button']");
selenium.waitForPageToLoad("2000");
assertEquals(selenium.getTitle(), "Welcome");
}

view plaincopy to clipboardprint?

public void test1() {

selenium.open("http://xxx.xxx.xxx/yyy");

selenium.check("xpath=//input[@name='MEICK_000']");
selenium.click("xpath=//input[@type='button']");
selenium.waitForPageToLoad("2000");
assertEquals(selenium.getTitle(), "Welcome");
}

public void test1() {

selenium.open("http://xxx.xxx.xxx/yyy");

selenium.check("xpath=//input[@name='MEICK_000']");
selenium.click("xpath=//input[@type='button']");
selenium.waitForPageToLoad("2000");
assertEquals(selenium.getTitle(), "Welcome");
}


我们可以使用 check 方法来确定选择哪个radio button

4、得到文本框里的文字

assertEquals(selenium.getValue("xpath=//input[@name='WNO']"), "1");

getValue 方法就是得到文本框里的数值,可不是 getText 方法,用错了可就郁闷了。

5、判断页面是否存在一个元素

assertTrue(selenium.isElementPresent("xpath=//input[@name='MEICK_000']"));

一般这个是用来测试当删除一些数据后,页面上有些东西就不会显示的情况。

6、判断下拉框里选择了哪个选项

assertEquals(selenium.getSelectedIndex("xpath=//SELECT[@name='HATIMING']"), "1");

这个可以用来判断下拉框显示的选项是否是期望的选项。

7、如果有 alert 弹出对话框怎么办?

这个问题弄了挺长时间,可以这样来关闭弹出的对跨框:

if(selenium.isAlertPresent()) {

selenium.getAlert();

}

其实当调用 selenium.getAlert() 时,就会关闭 alert 弹出的对话框。

也可以使用 System.out.println(selenium.getAlert()) 来查看对跨框显示的信息。

在测试的时候,有的人会显示许多alert 来查看运行时的数据,那么我们可以用下面的方式来关闭那些 alert:

while(selenium.isAlertPresent()) {

selenium.getAlert();

}

8、如何测试一些错误消息的显示?

assertTrue(selenium.getBodyText().indexOf("错误消息")>=0);

切记: getBodyText 返回的时浏览器页面上的文字,不回包含html 代码的,如果要显示html 代码,用下面这个:

System.out.println(selenium.getHtmlSource());

以上就是最常用的几个方法了,例如 click, type, getValue 等等。

还有就是一定要学习 xpath, 其实xpath 也可以有“与、或、非”的操作:

selenium.check("xpath=//input[(@name='KNYKBN')and(@value='Y')]");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: