在web ui自動(dòng)化測(cè)試中,frame一直是令人頭痛的問(wèn)題,就像上班必須擠公車坐地鐵一般,frame的問(wèn)題總是令人氣悶糾結(jié)為之黯然神傷。
以前在使用watir 1.6x的時(shí)候,frame也是頗為棘手的一個(gè)問(wèn)題。不但要照本宣科的進(jìn)行一系列的設(shè)置,而且在進(jìn)行實(shí)際代碼編寫的過(guò)程中會(huì)遇到各種奇奇怪怪的問(wèn)題。frame就像中國(guó)男足的后防線,問(wèn)題多多難以解決。
selenium webdriver處理frame比較簡(jiǎn)單,這點(diǎn)比某些測(cè)試工具要先進(jìn)一些,令人身心愉悅。
以下面的html代碼為例,我們看一下如何定位frame上的元素。
frame.html <html> <head> <title>Frame</title> <style> #f_1 {width: 10em; height: 10em; border: 1px solid #ccc; } #f_2 {display: none} </style> </head> <body> <p id = "p">Outside frame</p> <iframe id = "f_1" f1" src = "part1.htm"></iframe> <iframe id = "f_2" src = "part2.htm"></iframe> </body> </html> part1.htm <html> <head><title>Part1</title></head> <body> <p id = "f_p">This is part 1</p> <input id = "btn" type = "button" value = "click me" onclick = "alert('hello')" /> </body> </html> |
switch_to方法會(huì)new1個(gè)TargetLocator對(duì)象,使用該對(duì)象的frame方法可以將當(dāng)前識(shí)別的"主體"移動(dòng)到需要定位的frame上去。
require 'rubygems' require 'selenium-webdriver' dr = Selenium::WebDriver.for :firefox frame_file = 'file:///'+File.expand_path(File.join(File.dirname(__FILE__), 'frame.html')) dr.navigate.to frame_file # 定位default content上的p元素 p dr.find_element(:id => 'p') # 將當(dāng)前識(shí)別主體移動(dòng)到id為f_1的frame上去 dr.switch_to.frame('f_1') # 點(diǎn)擊frame上的button dr.find_element(:id =>'btn').click # --> a alert will popup # 此時(shí)再去定位frame外的p元素將出現(xiàn)錯(cuò)誤 p dr.find_element(:id => 'p') # --> error # 將識(shí)別的主體切換出frame dr.switch_to.default_content p dr.find_element(:id => 'p') # --> ok |
webdriver的frame處理方式讓人感覺(jué)那個(gè)不痛越來(lái)越輕松,這點(diǎn)進(jìn)步值得肯定。
下一節(jié)我們將介紹如何定位彈出的新窗口
相關(guān)文章:
Selenium webdriver系列教程(4)—如何定位測(cè)試元素
Selenium webdriver系列教程(6)—如何捕獲彈出窗口