---------------------------------------------------------------------------------------------------------javascript
1、Selenium-RC 是 selenium-remote control 缩写,是使用具体的语言来编写测试类。css
2、准备工做: 1,下载 selenium 了,到http://www.openqa.org/selenium/下载就能够了,记得选择selenium-rc 的版本html
2, 学习一下xpath 的知识。有个教程:http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.htmljava
3, 安装 jdk1.5css3
3、selenium-rc 一些使用方法chrome
1,解压selenium-rc压缩包浏览器
2,启动服务器服务器
Selenium Server是用JAVA实现的,相应的库文件在HOME/server/selenium-server.jar。运行以下代码从命令行启动:dom
java 代码 : java -jar selunium-server.jar工具
4、编写测试用例
须要的JAR: selenium-java-client-driver.jar;junit
编写一个JUNIT的单元测试,要构建一个Selenium,包括以下步骤:
* 构建一个Selenium实例
* 启动Selenium实例
* 执行Selenium命令,并验证结果。
* 关闭Selenium实例
以下是一个示例,用来测试http://www.google.com/,查找selenium,指望结果中包含"OpenQA: Selenium"
1. package com.thoughtworks.selenium;
2.
3. import junit.framework.*;
4.
5. import org.openqa.selenium.server.*;
6.
7. public class GoogleTest extends TestCase
8. {
9. private Selenium selenium;
10.
11. public void setUp() throws Exception {
12. String url = "http://www.google.com";
13. selenium = new DefaultSelenium("localhost", SeleniumServer.getDefaultPort(), "*firefox", url);
14. selenium.start();
15. }
16.
17. protected void tearDown() throws Exception {
18. selenium.stop();
19. }
20.
21. public void testGoogleTestSearch() throws Throwable {
22. selenium.open("/intl/zh-CN/");
23. selenium.type("q", "selenium");
24. selenium.click("btnG");
25. selenium.waitForPageToLoad("30000");
26. assertEquals("selenium - Google 搜索", selenium.getTitle());
27.
28. }
29.
30. }
5、多环境测试
package test; import org.junit.Test; import junit.framework.TestCase; import com.thoughtworks.selenium.DefaultSelenium; @Test private void script(Selenium selenium) throws Exception { |
6、如何选取元素
selenium提供以下强大的定位元素的方法。
id=id |
1 经过ID,name选择元素 : 如 selenium.type("id=q","百度"); selenium.type("name=search","百度")
2 link= 根据连接文字来操做:如 selenium.click("link=我的资料");
3 根据XPath来选择元素 : XPath Checker
* xpath=//img[@alt='The image alt text'] |
4 dom选择
* dom=document.forms['myForm'].myDropdown |
5 css选择器
这个不经常使用,它能够支持css2, css3选择器
* css=a[href="#id3"] |
7、使用selenium 这个对象来进行测试
1 获取标 : assertEquals("Insert title here", selenium.getTitle());
2 判断页面是否存在一个user.email元素 :assertTrue(selenium.isElementPresent("xpath=//input[@name='user.email']"));
3 获得文本框里的文字: assertEquals(selenium.getValue("xpath=//input[@name='user.username']"),"xxxaas");
4 测试check box : assertTrue(selenium.isChecked("xpath=//input[(@name='user.sex')and(@value='男')]"));
5 点击提交按钮 : selenium.click("xpath=//input[@type='button']");
6 等待页面载入 : selenium.waitForPageToLoad("2000");
7 验证指定文本出如今提交给用户的页面上: assertTrue(selenium.isTextPresent("验证码输入有误,请核实后再输入"));
8 判断下拉框里选择了哪一个选项 :assertEquals(selenium.getSelectedIndex("xpath=//SELECT[@name='HATIMING']"), "1");
9 如何测试一些错误消息的显示? assertTrue(selenium.getBodyText().indexOf("错误消息")>=0);
getBodyText 返回的时浏览器页面上的文字,不回包含html 代码的,若是要显示html 代码,用下面这个:selenium.getHtmlSource();
8、Firefox 的插件
1 XPath Checker :能够用这个工具测试简化咱们的xpath表达式
2 Firebug
3 Selenium IDE
4 Execute JS