3

我在处理以下情况时遇到问题。

我正在编写测试脚本,它将使用谷歌浏览器登录系统。

我正在从excel中读取数据。

第一组数据的用户名/密码组合不正确。

场景是输入错误密码时,弹出窗口显示用户名/密码不正确的消息。

在这种情况下,我应该能够关闭弹出窗口,并且 watir 应该从 excel 中读取下一组数据,直到满足正确的用户名/密码组合。


问题是我不确定如何检测弹出对话框。它有以下代码:

<div id="alertMsg" style="width: auto; min-height: 76.69999980926514px; height: auto;" class="ui-dialog-content ui-widget-content"><div class="dialogBoxTitle">&nbsp;</div>
<div class="alertMessage pointSearch">    
    <form name="saveComfortModelForm" class="pointSearchFrm">
        <table cellpadding="0" cellspacing="0" width="100%" height="100%" class="pointSearchTable">
            <tbody><tr>
                <td>Login failed due to invalid password or username</td>
            </tr>
        </tbody></table>    
    </form>
</div></div>

还有点击链接会关闭弹出窗口。代码如下:

<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
<span class="ui-dialog-title" id="ui-dialog-title-alertMsg">&nbsp;</span>
<a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button">
<span class="ui-icon ui-icon-closethick">close</span>
</a>
</div>

显示此消息时,我应该能够关闭窗口。

4

1 回答 1

0

您可以通过检查其中一个元素的可见性来检测是否显示了弹出对话框。这可以通过visible?orpresent?方法来完成。尝试:

browser.div(:id => "alertMsg").present?
#=> Returns true if popup is displayed, false if not displayed

您可以通过单击关闭链接关闭弹出窗口:

browser.link(:class => "ui-dialog-titlebar-close").click

逻辑的 wir 部分可能是这样的:

begin
  #Execute your logic to sign in with the next user/password

  #Check if the sign in was successful
  sign_in_successful = not(browser.div(:id => "alertMsg").present?)
  unless sign_in_successful
    browser.link(:class => "ui-dialog-titlebar-close").click
  end
end until sign_in_successful

更新:似乎有些人不喜欢 begin-while/until而应该使用循环:

loop do
  #Execute your logic to sign in with the next user/password

  if browser.div(:id => "alertMsg").present?
    browser.link(:class => "ui-dialog-titlebar-close").click
  else
    break
  end
end

结尾

于 2012-11-14T05:23:21.763 回答