Watir Web-Driver 的Alert 应用

上一篇 / 下一篇  2013-01-17 15:56:14 / 个人分类:测试

Watir Web-Driver已经不再依赖AutoIt就可以对Alert窗口进行操作。

Alert的方法主要有:

  • close    关闭当前的alert/prompt弹窗。此方法相当于点击Cancel,Close等按键。
  • exists? 是否存在alert/prompt弹窗 (present?也可以用)。
  • ok       点击alert/prompt弹窗的OK button
  • set(value) 在prompt弹窗中输入内容
  • text    返回alert/prompt弹窗的内容值
  • wait_until_present 在弹窗出现前,最多等待多少秒 (超时,弹窗没出现,报错)
  • wait_while_present 在弹窗消失前,最多等待多少秒 (超时,弹窗还在,报错)*官方文档的注释是错误的
  • when_present       等待多少秒,直到弹窗出现(等同于wait_until_present)
后三者的用法是:
browser.alert.wait_until_present
browser.alert.wait_while_present(10)
browser.when_present

我们看一个具体的实例:

首先,我们创建一个alerttest.html文件,放到D盘根目录下,内容为:

<html>
<head>
<SCRIPT. LANGUAGE="JAVASCRIPT">
function button1onclick(){
    confirm("Confirm Mode")
}
function button2onclick(){
    alert("Alert Mode")
}
function button3onclick(){
    var name=prompt("Your name is:");
     document.write(name);
}
</SCRIPT>
</head>
<form>
<input type="button" value="confirm mode" id ="button1" nclick="button1onclick()">
<input type="button" value="alert mode"   id ="button2" nclick="button2onclick()">
<input type="button" value="prompt mode"  id ="button3" nclick="button3onclick()">
</form>
</html>

然后我们创建一个测试脚本test.rb

#encoding:utf-8
require 'watir-webdriver'
require 'test/unit'   
 
class MyTest < Test::Unit::TestCase

 def test

$browser = Watir::Browser.new :chrome
$browser.goto("d:\\alerttest.html")
# confirm

$browser.button(:id,'button1').click
sleep(1)
if $browser.alert.exists? then
$browser.alert.ok # $browser.alert.close
sleep(1)
end
# alert

$browser.button(:id,'button2').click
sleep(1)
if $browser.alert.wait_until_present(3) then
$browser.alert.ok # $browser.alert.close
$browser.alert.wait_while_present(5)
end

# alert

$browser.button(:id,'button3').click
sleep(1)
if $browser.alert.when_present then
sleep(1)
$browser.alert.set("test name")
sleep(1)
$browser.alert.ok # $browser.alert.close
sleep(2)
end

end 
end
 


TAG: alert Watir watir Alert

 

评分:0

我来说两句

日历

« 2024-04-22  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

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

RSS订阅

Open Toolbar