转Selenium 总结说明

上一篇 / 下一篇  2013-07-09 09:20:14 / 个人分类:技术点

Selenium thoughtworks公司的一个集成测试的强大工具。最近参与了一个系统移植的项目,正好用到这个工具,

  把一些使用心得分享给大家,希望大家能多多使用这样的强大的,免费的工具,来保证我们的质量。

  Selenium的文档现存的不少,不过都太简单了。使用Selenium的时候,我更多的是直接去看API文档,好在API不错,

  一个一个看,就能找到所需要的:-)  官方网站:http://www.openqa.org/selenium/

  好,下面进入正题!

  一、Selenium的版本

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

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

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

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

  二、一些准备工作

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

  2学习一下xpath的知识。有个教程:http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html

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

  3、安装jdk1.5

  三、selenium-rc一些使用方法

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

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

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

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

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

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

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

   1. public class TestPage2 extends TestCase {  
   2.   private Selenium selenium;  
   3.  
   4.   protected void setUp() throws Exception {  
   5.      String url = “http://xxx.xxx.xxx.xxx/yyy”;  
   6.      selenium = new DefaultSelenium("localhost", SeleniumServer.getDefaultPort  
   7.                                 (), "*iexplore", url);  
   8.      selenium.start();  
   9.               
  10.      super.setUp();                       
  11.           
  12.   }  
  13.  
  14.   protected void tearDown() throws Exception {  
  15.           
  16.       selenium.stop();  
  17.       super.tearDown();  
  18.               
  19.  
  20.   }  
  21.  
  22. } 

  代码十分简单,作用就是初始化一个Selenium对象。其中:

  url :就是你要测试的网站

  localhost: 可以不是localhost,但是必须是selenium server启动的地址

  *iexplore : 可以是其它浏览器类型,可以在网站上看都支持哪些。

  下面我就要讲讲怎么使用selenium这个对象来进行测试。

 1、测试文本输入框

  假设页面上有一个文本输入框,我们要测试的内容是在其中输入一些内容,然后点击一个按钮,看看页面的是否跳转到需要的页面。

   1. public void test1() {   
   2.           
 
   3.     selenium.open("http://xxx.xxx.xxx/yyy");  
 
   4.           
 
   5.     selenium.type("xpath=//input[@name='userID']", "test-user");  
 
   6.     selenium.click("xpath=//input[@type='button']");  
 
   7.     selenium.waitForPageToLoad("2000");  
 
   8.     assertEquals(selenium.getTitle(), "Welcome");  
 
   9. } 

  上面的代码是这个意思:

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

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

  3)等待页面载入

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

  2、测试下拉框

   1. public void test1() {       
   2.               
 
   3.     selenium.open("http://xxx.xxx.xxx/yyy");      
 
   4.               
 
   5.     selenium.select("xpath=//SELECT[@name='SBBUSYO']", "index=1");  
 
   6.     selenium.click("xpath=//input[@type='button']");      
 
   7.     selenium.waitForPageToLoad("2000");      
 
   8.     assertEquals(selenium.getTitle(), "Welcome");      
 
   9. }    

  可以看到,我们可以使用select方法来确定选择下拉框中的哪个选项。

  select方法还有很多用法,具体去看看文档吧。

  3、测试check box

   1. public void test1() {           
   2.                   
 
   3.     selenium.open("http://xxx.xxx.xxx/yyy");          
 
   4.                   
 
   5.     selenium.check("xpath=//input[@name='MEICK_000']");    
 
   6.     selenium.click("xpath=//input[@type='button']");          
 
   7.     selenium.waitForPageToLoad("2000");          
 
   8.     assertEquals(selenium.getTitle(), "Welcome");          
 
   9. }     

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

4、得到文本框里的文字

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

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

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

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

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

  

TAG:

 

评分:0

我来说两句

Open Toolbar