您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium webdriver系列教程(6)?如何捕獲彈出窗口
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/9/22 15:32:43 ] 推薦標(biāo)簽:

  在web自動化測試中點擊一個鏈接然后彈出新窗口是比較司空見慣的事情。

  webdriver中處理彈出窗口跟處理frame差不多,以下面的html代碼為例

window.html
<html>
<head><title>Popup Window</title></head>
<body>
<a id = "soso" href = http://www.soso.com/ target = "_blank">click me</a>
</body>
</html>

  下面的代碼演示了如何去捕獲彈出窗口

require 'rubygems'
require 'pp'
require 'selenium-webdriver'
dr = Selenium::WebDriver.for :firefox
frame_file = 'file:///'.concat File.expand_path(File.join(File.dirname(__FILE__), 'window.html'))
dr.navigate.to frame_file
dr.find_element(:id =>'soso').click
# 所有的window handles
hs = dr.window_handles
# 當(dāng)前的window handle
ch = dr.window_handle
pp hs
pp ch
hs.each do |h|
unless h == ch
dr.switch_to.window(h)
p dr.find_element(:id => 's_input')
end
end

  捕獲或者說定位彈出窗口的關(guān)鍵在于獲得彈出窗口的handle。

  在上面的代碼里,使用了windowhandles方法獲取所有彈出的瀏覽器窗口的句柄,然后使用windowhandle方法來獲取當(dāng)前瀏覽器窗口的句柄,將這兩個值的差值是新彈出窗口的句柄。

  在獲取新彈出窗口的句柄后,使用switchto.window(newwindow_handle)方法,將新窗口的句柄作為參數(shù)傳入既可捕獲到新窗口了。

  如果想回到以前的窗口定位元素,那么再調(diào)用1次switch_to.window方法,傳入之前窗口的句柄既可達(dá)到目的。

相關(guān)鏈接:
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd