每天学习一点点,每天进步一点点

selenium脚本

上一篇 / 下一篇  2011-01-05 11:50:32 / 个人分类:功能测试

一、Selenium RC 原理及简介


  Selenium RC模式,是客户端利用各种编程语言,通过网络向Selenium Server发送指令,Selenium Server接收到测试指令后,启动浏览器并向其发出JavaScript调用实现对Html页面的全面追踪,并通过网络把执行结果返回给调用者。
  Selenium Server的实现原理是其打开浏览器时,把自己的JavaScript文件嵌入网页中。然后Selenium的网页通过frame嵌入目标网页。这样,就可以使用Selenium的JavaScript对象来控制目标网页。
  Selenium客户端一般使用单元测试技术实现,通过判断返回的结果与预期是否一致来决定程序是否运行正确。
如下图:
 Eclipse中Selenium Rc的测试脚本 - lynn - lynnajoy


  二、准备工作

  1、 语言选择
  Selenium支持下列语言:

Language
 Selenium Remote Control
 
C#
 Library ("driver") support
 
Java
 Library ("driver") support
 
Perl
 Library ("driver") support
 
PHP
 Library ("driver") support
 
Python
 Library ("driver") support
 
Ruby
 Library ("driver") support
 
Others
 Commands via HTTP requests**
 



  你可以用以上任意一种你熟悉的语言,编写Selenium测试脚本。本文以目前普及最广的Java语言为基础,来介绍Selenium RC

  2、 软件准备
  Selenium Remote Control Server
  下载地址:http://seleniumhq.org/download/
  Eclipse
  下载地址:http://www.eclipse.org/downloads/

  3、 启动Selenium Remote Control Server
  首先在下载的RC包里,解压后找到selenium-server.jar,然后在命令行窗口里启动它,具体启动格式如下(中括号内为可选参数):
  java -jar selenium-server.jar [-interactive] [options]
  -port <nnnn>: selenium服务器使用的端口号(默认 4444)
  -timeout <nnnn>: 我们放弃前(超时)所等待的秒数
  -interactive: 进入交互模式。参考教程获取更多信息
  -multiWindow: 进入被测试网站都在单独窗口打开的模式,并且selenium支持frame.
  -forcedBrowserMode <browser>: 设置浏览器模式(例如,所有的会话都使用"*iexplore",不管给getNewBrowserSession传递什么参数)
  -userExtensions <file>: 指定一个被载入到selenium的JavaScript文件
  -browserSessionReuse: 停止在测试间重新初始化和替换浏览器。
  -alwaysProxy: 默认情况下,我们尽量少的进行代理;设置这个标志将会强制所有的浏览器通讯都通过代理
  -firefoxProfileTemplate <dir>: 一般情况,我们在每次启动之前都生成一个干净的Firefox设置。您可以指定一个目录来让我们将您的设置拷贝过来,代替我们生成的。
  -debug: 进入debug模式,会有更多的跟踪调试信息
  -htmlSuite <browser> <startURL> <suiteFile> <resultFile>: 使用指定的浏览器(例如"*firefox")在指定的URL(例如"http://www.google.com"),运行一个单独的HTML Selenese (Selenium Core)测试套件然后立即退出。您需要指定HTML测试套件的绝对路径还有我们将会生成的HTML测试结果文件的路径。
  -proxyInjectionMode: 进入代理注入模式,这个模式中selenium服务器作为进入测试程序的所有内容的代理服务器。在这个模式下,可以跨多个域访问,并且还支持如下附加参数:
  -dontInjectRegex <regex>: 附加的正则表达式,代理注入模式能够使用它决定是否进行注入
  -userJsInjection <file>: 指定一个JavaScript文件,将它注入到所有页面中
  -userContentTransformation <regex> <replacement>: 一个正则表达式,对所有被测HTML内容进行匹配;第二个string将会对替换所有匹配的内容。这个标志能够使用多次。一个简单的适合使用这个参数的例子:如果你添加"-userContentTransformation https http"那么测试应用程序的HTML中的所有"https"字符串都会被替换为"http"。
  另外,还支持两种Java系统属性:-Dhttp.proxyHost 和 -Dhttp.proxyPort。使用Selenium服务器作为代理服务器,Selenium RC一般重载你的代理服务器配置。使用这个参数适合在使用Selenium服务器代理的同时使用你自己的代理服务器。使用代理服务器时这样配置:
  java -Dhttp.proxyHost=myproxy.com -Dhttp.proxyPort=1234 -jar selenium-server.jar
   如果你的HTTP代理服务器需要验证,你还可以在http.proxyHost和http.proxyPort后面设置-Dhttp.proxyUser 和 -Dhttp.proxyPassword。
  java -Dhttp.proxyHost=myproxy.com -Dhttp.proxyPort=1234 -Dhttp.proxyUser=joe -Dhttp.proxyPassword=example -jar selenium-server.jar

  三、编写Selenium RC脚本

  1、 新建一个project,在Project里引入selenium-java-client-driver.jar
  2、 新建一个java class,输入如下代码:
package Selenium.Test;   
import com.thoughtworks.selenium.*;   
 
public class seleniumTest {   
    private Selenium selenium;   
public void setUp() {   
     selenium = new DefaultSelenium("10.5.41.55",   
            4444, "*iexplore", "http://www.google.com/");   
     selenium.start();   
    }   
    public void testGoogle() {   
         selenium.open("/");         
  selenium.type("q", "selenium");   
  selenium.click("btnG");   
  selenium.waitForPageToLoad("30000");   
  boolean testResult = (selenium.isTextPresent("Selenium web application testing system"));   
        if (testResult){   
         //用例成功   
         System.out.print("Search selenium web is ok!");   
        } else {   
         //用例失败   
         System.out.print("selenium web not found!");        
        }   
    }   
    public static void main(String[] args) {   
     seleniumTest st = new seleniumTest();   
     st.setUp();   
     st.testGoogle();     
    }   
}  
package Selenium.Test;
import com.thoughtworks.selenium.*;

public class seleniumTest {
    private Selenium selenium;
public void setUp() {
     selenium = new DefaultSelenium("10.5.41.55",
            4444, "*iexplore", "http://www.google.com/");
     selenium.start();
    }
    public void testGoogle() {
         selenium.open("/");      
  selenium.type("q", "selenium");
  selenium.click("btnG");
  selenium.waitForPageToLoad("30000");
  boolean testResult = (selenium.isTextPresent("Selenium web application testing system"));
        if (testResult){
         //用例成功
         System.out.print("Search selenium web is ok!");
        } else {
         //用例失败
         System.out.print("selenium web not found!");     
        }
    }
    public static void main(String[] args) {
     seleniumTest st = new seleniumTest();
     st.setUp();
     st.testGoogle();  
    }
}

 

  试着运行一下,看下结果。上段代码,做了如下几件事情:

? l  启动Selenium事例
  selenium = new DefaultSelenium("服务器地址",
            端口号, "启动模式", "被测对象URL--BaseURL");
  selenium.start();
  其中,启动模式即以何种方式启动浏览器,其中常用到的有*firefox, *iehta, *iexplore. 全部模式如下:
  *konqueror
  *firefox:启动Firefox作为测试浏览器
  *iexploreproxy
  *firefoxproxy
  *safari
  *safariproxy
  *iexplore:启动IE作为测试浏览器
  *pifirefox
  *chrome
  *firefox2
  *piiexplore
  *googlechrome
  *iehta:以本地应用程序hta模式测试
  *firefox3
  *mock
  *opera
  *custom

  l   通过Selenium Server向浏览器发送指令模拟用户操作
  在浏览器里打开baseURL,即http://www.google.cn
   selenium.open("/");     
  在id是q的text输入框里,输入“selenium”
   selenium.type("q", "selenium");
  点击id or name 是btnG的button,即点击google的“Google搜索”按钮
   selenium.click("btnG");
  等待页面载入,参数单位是毫秒
   selenium.waitForPageToLoad("30000");

? l  判断结果
  isTextPresent是从当前页面查找指定的文本,返回值是布尔型的。当然,关于结果的判断有多种方式。可以参考Selenium reference (http://release.seleniumhq.org/selenium-remote-control/1.0-beta-2/doc/java/)
  boolean testResult = (selenium.isTextPresent("Selenium web application testing system"));

  四、总结

  通过上面的介绍,我想你已经可以用Selenium RC来编写测试脚本了。当然,开始前我们还需要详细考虑测试脚本的可重用性、易维护性等需求,对测试的project进行详细规划,如数据驱动、脚本模块化、Test Suite等问题。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/iamqa/archive/2009/08/05/4409209.aspx

TAG:

 

评分:0

我来说两句

日历

« 2024-05-01  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 17084
  • 日志数: 40
  • 书签数: 1
  • 建立时间: 2010-06-30
  • 更新时间: 2011-03-15

RSS订阅

Open Toolbar