发布新日志

  • ruby gems 升级

    2008-12-15 11:29:32


    gem update --system



  • seleniumRC_ruby测试环境配置和使用简介

    2008-07-30 16:01:51

    1          什么是Selenium RC

    Selenium RC,全称 Selenium Remote Control。它启动一个本地的代理服务器,通过接收本地客户端程序发出的请求来启动、关闭浏览器,并对浏览器进行输入、点击以及对浏览器内容验证等操作。

    Ø             可以使用多种语言编写脚本

    Ø             有独立的服务器端

    Ø             测试脚本产生命令,发送给服务器端

    Ø             服务器端用JS控制浏览器

    2          如何配置Selenium RC的运行环境

    2.1       安装Java Runtime Environment (JRE)

    Selenium RC 的开发语言为java,需要运行在JRE 1.5.0或更高的版本。请到http://www.java.com 下载并安装。

    2.2       安装ruby

    安装完成后在环境变量path中添加ruby的安装路径,c:\ruby\bin

    2.3       下载SeleniumRC

    请到http://www.openqa.org/selenium-rc/download.action 下载最新的SeleniumRC。解压后,一般会有如下文件夹列表:

    selenium-dotnet-client-driver-0.9.2

    selenium-java-client-driver-0.9.2

    selenium-perl-client-driver-0.9.2

    selenium-php-client-driver-0.9.2

    selenium-python-client-driver-0.9.2

    selenium-ruby-client-driver-0.9.2

    selenium-server-0.9.2

    另:selenium-server.jar包在 selenium-server-0.9.2目录下。

    2.4       运行Demo

    1.       启动Selenium RC。打开命令行,进入selenium-server.jar所在目录,运行:java -jar selenium-server.jar

    2.       打开另一个命令行,进入\selenium-ruby-client-driver-0.9.2目录,运行 ruby selenium_example.rb即可(注:此测试用例可能运行失败)

     

    3          Firefox Selenium IDE插件的使用

    3.1       Firefox浏览器中下载安装Selenium IDE插件

    下载地址:http://www.openqa.org/selenium-ide/

    3.2       使用Selenium IDE录制测试用例脚本

    1.       使用Firefox访问 http://www.yahoo.cn

    2.       点击“工具(T)”选项,选择“Selenium IDE”,运行Selenium IDE,点击红色圆形图标开始录制

    3.       返回浏览器,在yahoo.cn的搜索框输入Selenium,点击搜索按钮

    4.       进入搜索结果页后,为测试用例增加断言,如图:

    5.       至此测试用例录制完毕,再次点击红色圆形图标结束录制。再验证用例是否可以运行成功。点击Selenium IDE上的运行按钮,如图:

    如果每行的运行结果都是绿色,表示用例运行成功。

    6.       将该用例保存为ruby格式的测试用例。选择Selenium IDEOptions->Format->ruby Selenium RC,得到ruby代码,将代码保存为test.ruby(注,文件名需要和代码里的类名相同,并且文件应保存为UTF-8编码格式)

    7.       在命令行下进入到test.ruby所在的文件夹,运行 ruby test.ruby 即可重现刚才的搜索及验证步骤。

    4          参考资料

    l       SeleniumRC 官方指南:http://openqa.org/selenium-rc/tutorial.html

    l       Selenium 中文:http://wiki.javascud.org/display/SEL/Home

     

     

  • ruby_学习笔记:读写excel文件

    2008-05-14 11:12:44

    测试工作中,批量的数据通常会放到excel表格中,测试输出的数据写回表格中,这样输入输出易于管理,同时清晰明了,

    使用ruby来操作excel文件首先需要在脚本里包含以下语句
    require 'win32ole'

    把win32ole包含进来后,就可以通过和windows下的excel api进行交互来对excel文件进行读写了.


    打开excel文件,对其中的sheet进行访问:
    excel = WIN32OLE::new('excel.Application')
    workbook = excel.Workbooks.Open('c:\examples\spreadsheet.xls')
    worksheet = workbook.Worksheets(1) #定位到第一个sheet
    worksheet.Select

    读取数据:
    worksheet.Range('a12')['Value']  #读取a12中的数据
    data = worksheet.Range('a1:c12')['Value'] #将数据读入到一个二维表


    找到第一处a列的值为空值
    line = 1
    while worksheet.Range("a#{line}")['Value']
       line=line+1
    end #line的值为第一处空白行的行数


    将第一列的值读入到一个数组中
    line = '1'
    data = []
    while worksheet.Range("a#{line}")['Value']
       data << worksheet.Range("a#{line}:d#{line}")['Value']
       line.succ!
    end


    将数据写入到excel表格中
    worksheet.Range('e2')['Value'] = Time.now.strftime '%d/%m/%Y' #单个值
    worksheet.Range('a5:c5')['Value'] = ['Test', '25', 'result']  #将一个数组写入


    调用宏定义
    excel.Run('SortByNumber')

    设置背景色
    worksheet.Range('a3:f5').Interior['ColorIndex'] = 36 #pale yellow
    # 将背景色恢复成无色
    worksheet.Range('a3:f5').Interior['ColorIndex'] = -4142 # XlColorIndexNone constant
    # 使用Excel constant 将背景色恢复成无色
    worksheet.Range('a3:f5').Interior['ColorIndex'] = ExcelConst::XlColorIndexNone


    保存
    workbook.Close(1)
    # 或
    workbook.SaveAs 'myfile.xls'
    # 默认路径是系统定义的"我的文档"


    结束会话
    excel.Quit

    一些相对完整的代码片段

    创建一个excel文件并保存
    require 'win32ole'
    excel = WIN32OLE.new("excel.application")
    excel.visible = true     # in case you want to see what happens
    workbook = excel.workbooks.add
    workbook.saveas('c:\examples\spreadsheet1.xls')
    workbook.close

    操作excel文件的几个重要元素
    Excel => workbook => worksheet => range(cell)
    我理解的是excel为类名,workbook为一个具体的(excel文件)实例,创建好实例后,worksheet是实例(workbook,工作簿)中的一个工作表,然后可

    以对工作表中的每个单元格(range(cell))进行具体的读写------------------按照这样操作肯定没有错,不过下面的这些语句又让我有些疑惑


    excel.workbooks("Mappe1").worksheets("Tabelle1").range("a1").value #读取名为Mappe1的excel文件中工作表名为Tabelle1的a1单元格中的值
    excel.worksheets("Tabelle1").range("a1").value #作用同第一条语句
    excel.activeworkbook.activesheet.range("a1").value #作用同第一条语句
    excel.activesheet.range("a1").value #作用同第一条语句
    excel.range("a1").value #作用同第一条语句

    excel可以直接操作所有的属性,默认为当前活跃的工作簿/工作表


    对单元格的操作:

    某个单元格: sheet.range("a1")

    a1到c3的值:   sheet.range("a1", "c3")  或 sheet.range("a1:c3")

    第一列:    sheet.range("a:a")

    第三行:    sheet.range("3:3")

    获得单元格的值:
    range.text  #读取值,返回为字符串格式,如果单元格内为数字,有可能会被截断小数点后的位数
    sheet.range("a1").text  

    range.value  #读取值,数字不会截断
    sheet.range("a1").value      

    对单元格设置值
    sheet.range("a1").value = 1.2345

    sheet.range("a1").value = '1.2345'

    迭代访问:

    sheet.range("a1:a10").each{|cell|puts cell.value}

    如果范围是一个矩形,则会按行循环迭代访问
    sheet.range("a1:b5").each{|cell|puts cell.value}

    block迭代,并打印出每行的第一个值
    sheet.range("b3:c7").rows.each{|r|puts r.cells(1,1).value}

     

     

  • watir:扩展IE标签

    2008-05-13 15:36:33

     

    watir:如何扩展IE标签
    我们知道watir支持link,button,textfield...这些HTML标签,但是还有些标签它目前还不支持,如font,em,i,ol,如果在测试中遇到了这些标签怎么办呢?
    可以通过扩展来解决:
    将以下代码加到脚本的前面

       module Watir
      class Em < NonControlElement
        TAG = 'em'
      end
      module Container
        def em(how, what)
          return Em.new(self, how, what) //此处Em第一个字母要大写,不然运行会报错
        end
      end
    end

    然后试试:
    ie.em(:text,'链接').click
    哈,可以工作了
    类似的可以解决其他目前不支持的标签,如font,i等.
    如果熟悉xpath,也可以用xpath来解决,这部分待研究.

     

  • ruby_学习笔记:字符串处理函数

    2008-04-22 18:42:22


    最最常用的字符串处理函数

    1.返回字符串的长度
    str.length => integer

    2.判断字符串中是否包含另一个串
    str.include? other_str => true or false

       "hello".include? "lo"   #=> true
       "hello".include? "ol"   #=> false
       "hello".include? ?h     #=> true

    3.字符串插入:
    str.insert(index, other_str) => str

       "abcd".insert(0, 'X')    #=> "Xabcd"
       "abcd".insert(3, 'X')    #=> "abcXd"
       "abcd".insert(4, 'X')    #=> "abcdX"
       "abcd".insert(-3, 'X')   #=> "abXcd"
       "abcd".insert(-1, 'X')   #=> "abcdX"

    4.字符串分隔,默认分隔符为空格
    str.split(pattern=$;, [limit]) => anArray

    " now's  the time".split        #=> ["now's", "the", "time"]
       "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
       "hello".split(//)               #=> ["h", "e", "l", "l", "o"]
       "hello".split(//, 3)            #=> ["h", "e", "llo"]
       "hi mom".split(%r{\s*})         #=> ["h", "i", "m", "o", "m"]

       "mellow yellow".split("ello")   #=> ["m", "w y", "w"]
       "1,2,,3,4,,".split(',')         #=> ["1", "2", "", "3", "4"]
       "1,2,,3,4,,".split(',', 4)      #=> ["1", "2", "", "3,4,,"]
      
    5.字符串替换
    str.gsub(pattern, replacement) => new_str
    str.gsub(pattern) {|match| block } => new_str

    "hello".gsub(/[aeiou]/, '*')              #=> "h*ll*"     #将元音替换成*号
       "hello".gsub(/([aeiou])/, '<\1>')         #=> "h<e>ll<o>"   #将元音加上尖括号,\1表示保留原有字符???
       "hello".gsub(/./) {|s| s[0].to_s + ' '}   #=> "104 101 108 108 111 "


    字符串替换二:
    str.replace(other_str) => str
       s = "hello"         #=> "hello"
       s.replace "world"   #=> "world"

    6.字符串删除:

    str.delete([other_str]+) => new_str
    "hello".delete "l","lo"        #=> "heo"
       "hello".delete "lo"            #=> "he"
       "hello".delete "aeiou", "^e"   #=> "hell"
       "hello".delete "ej-m"          #=> "ho"

    7.去掉前和后的空格
    str.lstrip => new_str

       "  hello  ".lstrip   #=> "hello  "
       "hello".lstrip       #=> "hello"

    8.字符串匹配
    str.match(pattern) => matchdata or nil

    9.字符串反转
    str.reverse => new_str

    "stressed".reverse   #=> "desserts"

    10.去掉重复的字符
    str.squeeze([other_str]*) => new_str
     "yellow moon".squeeze                  #=> "yelow mon"  #默认去掉串中所有重复的字符
     "  now   is  the".squeeze(" ")         #=> " now is the"  #去掉串中重复的空格
     "putters shoot balls".squeeze("m-z")   #=> "puters shot balls"  #去掉指定范围内的重复字符


    11.转化成数字
    str.to_i=> str
    "12345".to_i             #=> 12345


     
    chomp和chop的区别:
    chomp:去掉字符串末尾的\n或\r
    chop:去掉字符串末尾的最后一个字符,不管是\n\r还是普通字符

    "hello".chomp            #=> "hello"
       "hello\n".chomp          #=> "hello"
       "hello\r\n".chomp        #=> "hello"
       "hello\n\r".chomp        #=> "hello\n"
       "hello\r".chomp          #=> "hello"
       "hello".chomp("llo")     #=> "he"

    "string\r\n".chop   #=> "string"
       "string\n\r".chop   #=> "string\n"
       "string\n".chop     #=> "string"
       "string".chop       #=> "strin"
       "x".chop.chop       #=> ""

  • ruby_学习笔记:读写文件

    2008-04-22 18:31:11

     

    读文件:
    第一种方法:
    $result='d:\\rs.txt'
    File.open($result, "r") do |file|  
    file.each_line do |line|
         if line.length>20
         puts line.chop.length    #去掉最后一个换行字符,并显示该行实际字符串的长度
          puts line
        end
      end
      end
    第二种方法:
    filename='d:\\rs.txt'
    while File.exists?(filename) #如果源文件存在就执行下面的操作
    file=File.open(filename,'r')
      while (lines=file.gets)
    puts lines
    end


    写文件:
    $filename="C:\\Automation\\rss"+".txt"
    $logfile = File.new($filename,"a")
    iCount=0

    while(iCount<10)      //循环写入10行
    $logfile.puts "http://xxxx/rs#{iCount}.xml"
    iCount=iCount+1
    end

    今天又笨了一次,由于要比较两个文件的不同,于是考虑用ruby来写个脚本

    实现,刚开始的时候使用双重
    File.open($file1, "r") do |file1|  
    file1.each_line do |line1|
    总是报错,

    后来改成先把文件读到一个数组里,然后循环比较,成功.

    其实这是个笨方法,在unix下使用三个命令就可以完成了.

    1.先使用sort指令将文件中的数据按照要求的索引进行排序,
    2.然后使用uniq指令将重复数据去掉
    3.再使用diff命令就可以了.


     

  • ruby/watir :几个小的总结

    2008-03-14 10:50:01

    assert的几种用法 
     
    代码示例:
    1.assert(ie.contains_text('Please Sign In to Continue'))
     
    2. assert_match(forward, $ie.textField(:id, "Subject").getContents, " fails ")
     
    3.assert_equal('pass', rh['status'])
     
    怎样获得当前文件的路径
     
    File.dirname(__FILE__).to_s
     
    模拟多次发送键盘操作

    模拟多次发送键盘操作,如发送4次tab键.
    当然可以用一个for循环来实现,
    但其实使用 $ie.send_keys('{tab 4}')就可以了
     

    使用ruby,如何生成excel文档,并以当前的日期为文件名?  

    利用系统提供的saveas(sFileName)函数
     
    sDate=t1.strftime("%Y_%m_%d_%H_%M")  //生成当前时间串
    $sResultName=DIR+sDate+".xls" //文件名
     
    @excel = WIN32OLE::new('excel.Application')
    @workbook = @excel.Workbooks.Add()
    @workbook.SaveAs ($sResultName)

     
  • ruby/watir开发,eclipse环境配置

    2008-03-13 14:59:25

    Step1 Install Ruby

    step2: download Watir Gem to C:\ruby\lib\ruby\gems\1.8\gems.

    step3:launch DOS command prompt and goto directory where Watir gem is saved C:\ruby\lib\ruby\gems\1.8\gems type gem install xxxx.gem (xxx = watir gem name) or just type "gem install watir" (it will automatically install whatever package in the current dir)

    Step4config Eclipse

    (1)    Download RDT: Install Ruby Development Tool Using the Eclipse Update Manager( RDT )

     (4)   unzip the download file

    (5)   run eclipse.exe进入eclipse环境

    (6)   Help->Software Updates->Find and Install

    (7)   In the dialog choose "Search for new features to install" and push next

    (8)   Select “New Archived Site”, then select the package “RDT” just download

    (9)   Then do as the follow screenshot:


     


     


     

    (10):最后把ruby文件导入就可以了.

  • watir:键盘指令发送列表

    2008-03-13 14:40:05

    详细参见:
    www.autoitscrīpt.com/autoit3/docs/appendix/SendKeys.htm

    在watir中一般使用ie.send_keys()来模拟键盘操作,如
    ie.send_keys("{Enter}")表示发送回车

    列出一些常用的:

    Send("{TAB}") Navigate to next control (button, checkbox, etc)
    Send("+{TAB}") Navigate to previous control.
    Send("^{TAB}") Navigate to next WindowTab (on a Tabbed dialog window)
    Send("^+{TAB}") Navigate to previous WindowTab.
    Send("{SPACE}") Can be used to toggle a checkbox or click a button.
    Send("{+}") Usually checks a checkbox (if it's a "real" checkbox.)
    Send("{-}") Usually unchecks a checkbox.
    Send("{NumPadMult}") Recursively expands folders in a SysTreeView32.

    {ENTER} ENTER key on the main keyboard
    {ALT} ALT
    {BACKSPACE} or {BS} BACKSPACE
    {DELETE} or {DEL} DELETE
    {UP} Up arrow
    {DOWN} Down arrow
    {LEFT} Left arrow
    {RIGHT} Right arrow
    {HOME} HOME
    {END} END
    {ESCAPE} or {ESC} ESCAPE

    连续多次发送某个键盘指令,可以使用以下格式:
    如:Send("{DEL 4}") 表示模拟连续按4次del键
  • watir :Tips and Tricks

    2008-03-12 14:28:16

    怎样运行测试但使浏览器不显示

    如果不想让浏览器在运行时显示,可以使用-b选项

    如:

    my_test.rb -b

    也可以使用下面的方法来隐藏浏览器:

    $HIDE_IE = true
    ie = Watir::IE.new()

    怎样并发运行测试

    见下面的代码示例,可以启动多个线程,利用线程来调用方法。

    # demonstrate ability to run multiple tests concurrently

    require 'thread'

    require 'watir' 

    def test_google

     ie = Watir::IE.start('http://www.google.com')

     ie.text_field(:name, "q").set("pickaxe")   

     ie.button(:value, "Google Search").click  

     ie.close

    end

    # run the same test three times concurrently in separate browsers

    threads = []

    3.times do

     threads << Thread.new {test_google}

    end

    threads.each {|x| x.join}

    怎样获得rubywatir的版本号

    For the Watir version type:

    ruby -e 'require "watir"; puts Watir::IE::VERSION'

    For the Ruby version type:

    ruby -v

    术语

    实例:程序所创建的对象,存储在内存中。

    消息:可以发送给对象的动作描述,如果对象能够识别消息,会给出响应及相应的行为动作。

    对象:包含有属性,并根据接收到的消息有相应的行动

    Ruby, 面向对象脚本语言

    Watir, Ruby编写的web应用程序测试框架Web Application Testing in Ruby

Open Toolbar