wiki:autotest

Version 11 (modified by qinhongyuan, 14 years ago) (diff)

--

自动化测试

  • selenium

该测试框架对Firefox完美支持,其它浏览器的兼容性能有待商榷
可以使用多种语言编写测试用例,可以使用Java,但是推荐使用 ruby来编写,因为使用ruby来编写的话,语言简洁很多,而且测试人员可以很简单地去编写Ruby脚本
具有录制功能,但是只针对在firefox下,录制生成的脚本可以转为各种语言的测试用例。


安装:

  • 下载ruby 安装 , 修改系统path配置 , 命令行输入ruby -v
  • 下载DevKit 安装 (安装时,路径不要有空格)
  • 到DevKit安装目录,命令行
    > cd <DEVKIT_INSTALL_DIR>
    > ruby dk.rb init
    #生成config.yml,这里会检查将要添加DevKit支持的Ruby列表,只支持通过RubyInstaller安装的Ruby
    #如果这里列出的Ruby与你的要求不符,可以手动修改
    > ruby dk.rb review  #检查要添加DevKit支持的Ruby列表是否有误,可以略过
    > ruby dk.rb install
    [INFO] Updating convenience notice gem override for 'C:/Ruby192'
    [INFO] Installing 'C:/Ruby192/lib/ruby/site_ruby/devkit.rb'
    >
    
  • 检查是否安装成功
    > gem install selenium-webdriver
    
  • selenium-webdriver 就是测试工具,安装成功即可
    C:\Documents and Settings\pc>gem install selenium-webdriver
    Temporarily enhancing PATH to include DevKit...
    Building native extensions.  This could take a while...
    Fetching: childprocess-0.3.2.gem (100%)
    Fetching: addressable-2.2.8.gem (100%)
    Fetching: libwebsocket-0.1.3.gem (100%)
    Fetching: selenium-webdriver-2.21.2.gem (100%)
    Successfully installed ffi-1.0.11
    Successfully installed childprocess-0.3.2
    Successfully installed addressable-2.2.8
    Successfully installed libwebsocket-0.1.3
    Successfully installed selenium-webdriver-2.21.2
    5 gems installed
    Installing ri documentation for ffi-1.0.11...
    Installing ri documentation for childprocess-0.3.2...
    Installing ri documentation for addressable-2.2.8...
    Installing ri documentation for libwebsocket-0.1.3...
    Installing ri documentation for selenium-webdriver-2.21.2...
    Installing RDoc documentation for ffi-1.0.11...
    Installing RDoc documentation for childprocess-0.3.2...
    Installing RDoc documentation for addressable-2.2.8...
    Installing RDoc documentation for libwebsocket-0.1.3...
    Installing RDoc documentation for selenium-webdriver-2.21.2...
    
  • 编写第一个案例,打开netbeans ,新建ruby工程
    require 'rubygems'
    require 'selenium-webdriver'
    
    driver = Selenium::WebDriver.for :firefox
    driver.navigate.to "http://ks.pconline.com.cn/bbs.jsp"
    #sleep 3
    
    element = driver.find_element(:name, 'q')
    element.send_keys "IPhone4S"
    element.submit
    
    puts driver.title
    driver.quit
    

API使用及其例子

  • 解决中文字符问题
    require 'rubygems'
    require 'selenium-webdriver'
    require 'iconv'
    class String
    	def to_gbk
    		 Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",self).to_s
    	end
    end
    
    
    driver = Selenium::WebDriver.for :ie
    driver.navigate.to "http://ks.pconline.com.cn/bbs.jsp"
    #sleep 3
    
    element = driver.find_element(:name, 'q')
    element.send_keys "IPhone4S"
    element.submit
    
    
    puts "#{driver.title}".to_gbk
    driver.quit
    
  • 检测登录论坛登录例子
    require 'rubygems'
    require 'selenium-webdriver'
    require 'iconv'
    class String
    	def to_gbk
    		 Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",self).to_s
    	end
    end
    
    
    driver = Selenium::WebDriver.for :ie
    driver.navigate.to "http://itbbs.pconline.com.cn/mobile/f587012.html"
    #sleep 3
    
    #输入帐号密码
    username = driver.find_element(:name, 'username')
    username.send_keys "3edc4rfv"
    password = driver.find_element(:name, 'password')
    password.send_keys "123456"
    password.submit #提交数据
    
    #读取cookie查看是否登录成功
    driver.manage.all_cookies.each { |cookie|
    	  puts "pass!" if cookie[:name] == "cmu" and cookie[:value] == "3edc4rfv"
        #puts "#{cookie[:name]} => #{cookie[:value]}"
    }
    #puts "pass!" if driver.manage.cookie["cmu"]  == "3edc4rfv"
    driver.manage.delete_all_cookies #清除cookie
    #puts "#{driver.title}".to_gbk
    driver.quit