= 自动化测试 = - selenium 该测试框架对Firefox完美支持,其它浏览器的兼容性能有待商榷[[BR]] 可以使用多种语言编写测试用例,可以使用Java,但是推荐使用[http://www.ruby-lang.org/zh_cn/documentation/ ruby]来编写,因为使用ruby来编写的话,语言简洁很多,而且测试人员可以很简单地去编写Ruby脚本[[BR]] 具有录制功能,但是只针对在firefox下,录制生成的脚本可以转为各种语言的测试用例。 ---- == 安装:== - [http://files.rubyforge.vm.bytemark.co.uk/rubyinstaller/rubyinstaller-1.9.2-p290.exe window-下载ruby][[BR]] - [http://cloud.github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe window-DevKit.exe版] [http://rubyforge.org/frs/download.php/66888/devkit-3.4.5r3-20091110.7z window-DevKit] [https://github.com/oneclick/rubyinstaller/downloads 更多DevKit] - 下载ruby 安装 , 修改系统path配置 , 命令行输入ruby -v - 下载DevKit 安装 (安装时,路径不要有空格) - 到DevKit安装目录,命令行 {{{ > cd > 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 }}}