Watir Webdriver 对JS Dialog的处理

上一篇 / 下一篇  2013-01-28 10:20:52 / 个人分类:测试

Watir webdriver内置了如何处理javascript. dialog的方法,以及从dialog获得所需值的方法。

# 判断alert是否存在
browser.alert.exists?
 
# 获得alert的值
browser.alert.text
 
# 关闭alert
browser.alert.ok
browser.alert.close

# 接受confirm
browser.alert.ok
 
# 取消confirm
browser.alert.close

# 输入内容到prompt
browser.alert.set "Prompt answer"
 
# 接受prompt
browser.alert.ok
 
# 取消prompt
browser.alert.close

如果上述方法无效,我们还有一些替代的方法:

# 对于alert,overide从而使其不返回任何值
browser.execute_script("window.alert = function() {}")
 
# 返回用户在prompt输入的值
browser.execute_script("window.prompt = function() {return 'my name'}")
 
# 返回空值,用于模拟点击prompt的cancel
browser.execute_script("window.prompt = function() {return null}")
 
# 返回true,用于模拟模拟点击confirm的ok
browser.execute_script("window.confirm = function() {return true}")
 
# 返回false,用于模拟点击confirm的cancel
browser.execute_script("window.confirm = function() {return false}")
 
# 对于离开popup不返回任何值
browser.execute_script("window.onbeforeunload = null")


TAG:

 

评分:0

我来说两句

日历

« 2024-05-16  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 152641
  • 日志数: 185
  • 文件数: 6
  • 建立时间: 2007-08-06
  • 更新时间: 2015-01-06

RSS订阅

Open Toolbar