Watir截屏功能的实现

发表于:2009-09-25来源:作者:点击数: 标签:watirWatirWATIR功能
Step1. 下载并安装Rmagick Step2. 下载并安装win32screenshot Step3.编辑Win32Screenshot.rb 1. module Win32 改为 module Win32Screenshot 2.找到 USER32 = DL.dlopen("user32") EnumWindows = USER32['EnumWindows', 'IPL'] GetWindowTextLength = USER32[

Step1.下载并安装Rmagick
Step2.下载并安装win32screenshot
Step3.编辑Win32Screenshot.rb
      1.

module Win32   

        改为
module Win32Screenshot

      2.找到
    USER32 = DL.dlopen("user32")
    EnumWindows = USER32['EnumWindows', 'IPL']
    GetWindowTextLength = USER32['GetWindowTextLengthA' ,'LI' ]
    GetWindowText = USER32['GetWindowTextA', 'iLsL' ]

        下面加入
    ShowWindow= USER32['ShowWindow', 'LIL' ]
    BringWindowToTop=USER32['BringWindowToTop','LI']

       3.找到
    SRCCOPY = 0xCC0020
    GMEM_FIXED = 0
    DIB_RGB_COLORS = 0

        下面加入
    SW_SHOWMAXIMIZED = 3

       4.找到
    def capture_hwnd(hwnd)
      hScreenDC = getDC(hwnd)

         两行之间插入
      ShowWindow.call(hwnd,SW_SHOWMAXIMIZED)
      setForegroundWindow(hwnd)
      BringWindowToTop.call(hwnd)

Step4.编辑Watir.rb
       1.文件开头加入
require 'win32screenshot'
require 'rubygems'
require 'RMagick'

       2.Class IE中加入方法
    def captureGif(file)
      
      width, height, bmp =Win32Screenshot::Screenshot.capture_hwnd(@ie.hwnd)
      img = Magick::Image.from_blob(bmp)
      png = img[0].to_blob do
      self.format = 'gif'
    end
    File.open(file, "wb") {|io| io.write(png)} unless file.nil?
    end

Step5.over用下面的代码试一下效果
   require 'watir'   # the watir controller
   # open the IE browser
   ie = Watir::IE.new
   ie.maximize
   # Step 1: go to the test site: http://www.google.com
   ie.goto("http://www.google.com")
   ie.captureGif('c:/google1.gif')
   # Step 2: enter 'pickaxe' in the search text field
   ie.text_field(:name, "q").set("什么")       # q is the name of the search field
   ie.captureGif('c:/google2.gif')
   # Step 3: click the 'Google Search' button
   ie.button(:name, "btnG").click                 # "btnG" is the name of the Search button
   ie.captureGif('c:/google3.gif')
   # Actual Result: Check that the 'Programming Ruby' link appears on the results page
   if  ie.contains_text("什么是-什么是什么,搜搜就知道!")  
      puts "Test Passed. Found the test string: '什么是-什么是什么,搜搜就知道!'. Actual Results match Expected Results."
   else
      puts "Test Failed! Could not find: '什么是-什么是什么,搜搜就知道!'"
   end
   # End of test: Google search

原文转自:http://www.ltesting.net