用Watir測試QTP的Demo程式Mercury Tours
參考:
http://wiki.openqa.org/display/WTR/Watir+demo+on+Mercury+Tours
require 'test/unit'
require 'watir'
require 'watir/screen_capture'
class Demo < Test::Unit::TestCase
include Watir
include Watir::ScreenCapture
def setup
@ie = Watir::IE.new_process
@ie.goto("http://newtours.demoaut.com/")
end
def test_logon
@ie = Watir::IE.attach(:url,"http://newtours.demoaut.com/" )
@ie.maximize()
assert_equal(@ie.title, "Welcome: Mercury Tours")
@ie.text_field(:name, "userName").set "test"
@ie.text_field(:name, "password").set "test"
@ie.button(:name,"login").click
screen_capture("logon.jpg",true)
assert_equal(@ie.title, "Find a Flight: Mercury Tours:")
assert(@ie.radio(:name,"tripType", "oneway").exists?)
assert(@ie.select_list(:name,"passCount").exists?)
assert(@ie.select_list(:name,"fromPort").exists?)
assert(@ie.select_list(:name,"fromMonth").exists?)
assert(@ie.select_list(:name,"fromDay").exists?)
assert(@ie.select_list(:name,"toPort").exists?)
assert(@ie.radio(:name, "servClass","Business").exists?)
assert(@ie.select_list(:name,"airline").exists?)
assert(@ie.form(:name, "findflight").exists?)
# booking page 1
@ie.radio(:name,"tripType", "oneway").set
@ie.select_list(:name,"passCount").select "1"
@ie.select_list(:name,"fromPort").select "Acapulco"
@ie.select_list(:name,"fromMonth").select "March"
@ie.select_list(:name,"fromDay").select "10"
@ie.select_list(:name,"toPort").select "Frankfurt"
@ie.radio(:name, "servClass","Business").set
@ie.select_list(:name,"airline").select "Unified Airlines"
@ie.form(:name, "findflight").submit
screen_capture("page1.jpg",true)
# booking page 2
assert(@ie.radio(:name, "outFlight","Blue Skies Airlines$360$270$5:03").exists?)
assert(@ie.radio(:name, "outFlight","Blue Skies Airlines$361$271$7:10").exists?)
assert(@ie.radio(:name, "outFlight","Pangea Airlines$362$274$9:17").exists?)
assert(@ie.radio(:name, "outFlight","Unified Airlines$363$281$11:24").exists?)
@ie.radio(:name, "outFlight","Pangea Airlines$362$274$9:17").set
assert(@ie.radio(:name, "inFlight","Blue Skies Airlines$631$273$14:30").exists?)
@ie.radio(:name, "inFlight","Blue Skies Airlines$631$273$14:30").set
assert(@ie.form(:name,"results").exists?)
@ie.form(:name,"results").submit
screen_capture("page2.jpg",true)
#booking page 3 -- purchase
assert(@ie.text_field(:name,"passFirst0").exists?)
assert(@ie.text_field(:name,"passLast0").exists?)
assert(@ie.text_field(:name,"creditnumber").exists?)
assert(@ie.form(:name,"bookflight").exists?)
@ie.text_field(:name,"passFirst0").set "samuel"
@ie.text_field(:name,"passLast0").set "luo"
@ie.text_field(:name,"creditnumber").set "1234"
@ie.form(:name,"bookflight").submit
screen_capture("page3.jpg",true)
end
def teardown
@ie.close()
end
end
這個例子測試QTP的WEB樣例程式,使用了screen_capture包來實現螢幕擷取,開啟這個包(C:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.2/watir/screen_capture.rb)可以看到,所謂的螢幕擷取就是使用Win32API進行硬拷貝,然後開啟畫圖程式儲存成檔案:
require 'Win32API'
module Watir
module ScreenCapture
KEYEVENTF_KEYUP = 0x2
SW_HIDE = 0
SW_SHOW = 5
SW_SHOWNORMAL = 1
VK_CONTROL = 0x11
VK_F4 = 0x73
VK_MENU = 0x12
VK_RETURN = 0x0D
VK_SHIFT = 0x10
VK_SNAPSHOT = 0x2C
VK_TAB = 0x09
GMEM_MOVEABLE = 0x0002
CF_TEXT = 1
# this method saves the current window or whole screen as either a bitmap or a jpeg
# It uses paint to save the file, so will barf if a duplicate filename is selected, or the path doesnt exist etc
# * filename - string - the name of the file to save. If its not fully qualified the current directory is used
# * active_window - boolean - if true, the whole screen is captured, if false, just the active window is captured
# * save_as_bmp - boolean - if true saves the file as a bitmap, saves it as a jpeg otherwise
def screen_capture(filename , active_window_only=false, save_as_bmp=false)
keybd_event = Win32API.new("user32", "keybd_event", ['I','I','L','L'], 'V')
vkKeyScan = Win32API.new("user32", "VkKeyScan", ['I'], 'I')
winExec = Win32API.new("kernel32", "WinExec", ['P','L'], 'L')
openClipboard = Win32API.new('user32', 'OpenClipboard', ['L'], 'I')
setClipboardData = Win32API.new('user32', 'SetClipboardData', ['I', 'I'], 'I')
closeClipboard = Win32API.new('user32', 'CloseClipboard', [], 'I')
globalAlloc = Win32API.new('kernel32', 'GlobalAlloc', ['I', 'I'], 'I')
globalLock = Win32API.new('kernel32', 'GlobalLock', ['I'], 'I')
globalUnlock = Win32API.new('kernel32', 'GlobalUnlock', ['I'], 'I')
memcpy = Win32API.new('msvcrt', 'memcpy', ['I', 'P', 'I'], 'I')
filename = Dir.getwd.tr('/','//') + '//' + filename unless filename.index('//')
if active_window_only ==false
keybd_event.Call(VK_SNAPSHOT,0,0,0) # Print Screen
else
keybd_event.Call(VK_SNAPSHOT,1,0,0) # Alt+Print Screen
end
winExec.Call('mspaint.exe', SW_SHOW)
sleep(1)
# Ctrl + V : Paste
keybd_event.Call(VK_CONTROL, 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?V), 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?V), 1, KEYEVENTF_KEYUP, 0)
keybd_event.Call(VK_CONTROL, 1, KEYEVENTF_KEYUP, 0)
# Alt F + A : Save As
keybd_event.Call(VK_MENU, 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?F), 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?F), 1, KEYEVENTF_KEYUP, 0)
keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
keybd_event.Call(vkKeyScan.Call(?A), 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?A), 1, KEYEVENTF_KEYUP, 0)
sleep(1)
# copy filename to clipboard
hmem = globalAlloc.Call(GMEM_MOVEABLE, filename.length+1)
mem = globalLock.Call(hmem)
memcpy.Call(mem, filename, filename.length+1)
globalUnlock.Call(hmem)
openClipboard.Call(0)
setClipboardData.Call(CF_TEXT, hmem)
closeClipboard.Call
sleep(1)
# Ctrl + V : Paste
keybd_event.Call(VK_CONTROL, 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?V), 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?V), 1, KEYEVENTF_KEYUP, 0)
keybd_event.Call(VK_CONTROL, 1, KEYEVENTF_KEYUP, 0)
if save_as_bmp == false
# goto the combo box
keybd_event.Call(VK_TAB, 1, 0, 0)
keybd_event.Call(VK_TAB, 1, KEYEVENTF_KEYUP, 0)
sleep(0.5)
# select the first entry with J
keybd_event.Call(vkKeyScan.Call(?J), 1, 0, 0)
keybd_event.Call(vkKeyScan.Call(?J), 1, KEYEVENTF_KEYUP, 0)
sleep(0.5)
end
# Enter key
keybd_event.Call(VK_RETURN, 1, 0, 0)
keybd_event.Call(VK_RETURN, 1, KEYEVENTF_KEYUP, 0)
sleep(1)
# Alt + F4 : Exit
keybd_event.Call(VK_MENU, 1, 0, 0)
keybd_event.Call(VK_F4, 1, 0, 0)
keybd_event.Call(VK_F4, 1, KEYEVENTF_KEYUP, 0)
keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
sleep(1)
end
end
end
#screenCapture( "f.bmp", false , true)
相關文章
- QTP測試多個Windows應用程式的解決方案QTWindows
- QTP測試Yahoo郵箱QT
- 用QTP進行GMail郵箱的自動化測試QTAI
- QTP測試QQ登入介面QT
- QTP測試Windows計算器QTWindows
- QTP測試WinToolbar控制元件QT控制元件
- 自動化測試工具QTPQT
- QTP測試AJAX時的等待問題QT
- Jmeter介面測試demoJMeter
- QTP自動化測試Google地圖QTGo地圖
- QTP測試.NET控制元件CheckedListBoxQT控制元件
- QTP測試資料管理-Excel+DictionaryQTExcel
- 自動化測試QTP知識框架QT框架
- 使用QTP進行非GUI的自動化測試QTGUI
- 《QTP自動化測試進階》樣章QT
- QTP測試CodeJock Xtreme Suite控制元件QTREMUI控制元件
- 使用QTP進行WEB頁面效能測試QTWeb
- 軟體測試工具QTP學習小結QT
- 《QTP自動化測試進階》51CTO試讀QT
- 【軟體測試自動化-QTP系列講座 5】== DOM技術的應用 ==QT
- 《QTP自動化測試進階》準備加印!QT
- QTP呼叫外部應用程式的4種方法QT
- 試用Linux的法寶--DemoLinux試用(轉)Linux
- QTP測試多個瀏覽器視窗的解決方案QT瀏覽器
- 一個簡單的介面測試框架 demo框架
- 【iCore3 雙核心板】DEMO 1.0 測試程式釋出
- jmeter壓力測試報告 - DEMOJMeter測試報告
- 《QTP自動化測試進階》一書附帶的原始碼QT原始碼
- Sencha應用程式的UI測試 薦UI
- QTP專案實戰課程測試指令碼下載QT指令碼
- 程式碼測試用例指南
- 《QTP自動化測試進階》第二次印刷QT
- QC、QTP等測試工具已經放到網站資源共享QT網站
- 《QTP自動化測試實踐》再次加印2000冊!QT
- 【iCore4 雙核心板】DEMO V1.0 測試程式釋出
- 靜態應用程式安全測試
- Web應用程式完全測試指南Web
- Android應用程式測試-Alltesting|澤眾雲測試Android