javascript - Watir 随机弹出窗口

标签 javascript html ruby cucumber watir-webdriver

最重要的是,我正在测试这个电子商务,我得到了这个随机弹出窗口(它是一个 div),它妨碍了我的脚本,鉴于它的随机外观,我无法真正预测它何时会显示,否则,每当我看到它时,我都可以轻松地与它交互,因为它是一个简单的 div。有没有一种方法可以让我捕捉到这个弹出窗口,并在它敢于显示时做我想做的事?提前致谢

<div class="fsrFloatingMid"><div class="fsrInvite">
<div class="fsrDialogs">
    <div style="margin-left: 0px;" class="fsrDialog ">
        <div class="fsrLogos">
            <img src="/_ui/desktop/common/foresee/sitelogo.gif" alt="" class="fsrSiteLogo">
            <img src="/_ui/desktop/common/foresee/fsrlogo.gif" alt="Foresee" class="fsrCorpLogo">
        </div>
        <h1 class="fsrHeading">We'd welcome your feedback!</h1>
        <p class="fsrBlurb">Some bullshit text</p>
        <p class="fsrSubBlurb">The survey is designed to measure your entire experience, please look for it at the <u>conclusion</u> of your visit.</p>
        <p class="fsrAttribution">This survey is conducted by an independent company, on behalf of the site you are visiting.</p>
        <div style="" class="fsrB">
            <div class="fsrAcceptButtonContainer">
                <a href="javascript:void(0)" class="fsrAcceptButton" tabindex="2">Yes, I'll give feedback</a><span class="hidden-accessible">&nbsp;(this will launch a new window)</span>
            </div>

            <div class="fsrDeclineButtonContainer"><a href="javascript:void(0)" class="fsrDeclineButton"    tabindex="1">No, thanks</a>
            </div>
        </div>
        <div class="fsrFooter">
            <a href="http://some-bullshit-url" tabindex="5" title="Validate TRUSTe privacy certification" target="_blank" class="fsrTE"><img src="/_ui/desktop/common/foresee/truste.png" alt="TRUSTe verified" class="fsrTruste"></a>
        </div>
    </div>
</div>
<a href="#" role="button" tabindex="6" class="fsrCloseBtn">×<span class="hidden-accessible">Click to close.</span></a>

最佳答案

如果此弹出窗口随机出现,那么我认为使用“保护代理”设计模式会很有帮助。它的目的是执行一段特定的代码,在我们的示例中是这样的:

if browser.div(class: 'fsrDialogs').exists?
      browser.a(class: 'fsrCloseBtn').click
end

在调用“subject”(“subject”是我们包装在 Proxy 类中的对象,在我们的例子中是浏览器)上的任何方法之前。代理设计模式在 Ruby 中实现起来非常简单,以下是我们在您的特定情况下如何实现的方法:

class WatirProxy
  attr_reader :subject
  def initialize(browser)
    @subject = browser
  end

  def method_missing(method, *args)
    puts "I am executing the code below before calling #{method} with args #{args}"
    if subject.div(class: 'fsrDialogs').exists?
      subject.a(class: 'fsrCloseBtn').click
    end

    subject.send(method, *args)
  end
end

您可以在生产中删除 method_missing 下面的 put,但是,如果您不是 100% 清楚下面的代码是如何工作的,我建议您暂时保留它。

让我们尝试一下:

browser = WatirProxy.new(Watir::Browser.new(:chrome)) # you can use ANY method on browser, although it's wrapped in a proxy
browser.goto 'https://google.com'
puts browser.text_field(name: 'q').exists?

Ruby 应输出:

I am executing the code below before calling goto with args ["https://google.com"]
I am executing the code below before calling text_field with args [{:name=>"q"}]
true # May change if google search box 'name' attribute changed to something other than 'q' in the future

在您的具体情况下,此弹出窗口会引发错误,因为浏览器没有预料到它,现在我们确保在调用浏览器上的任何方法之前进行检查。一本关于代理设计模式(以及 Ruby 中其他一些有用的模式)的好书是 Design Patterns in Ruby作者:拉斯·奥尔森。

关于javascript - Watir 随机弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25882186/

相关文章:

ruby - 无法使用 OS X Sierra 安装 capybara-webkit

javascript - 如何将 PHP 数据传递给外部 JavaScript

javascript - 如何将一个页面上的 block 复制到另一个保留 css 样式的页面?

javascript - 尝试发送 POST 请求并获取 json 数据,但收到 400

html - 删除 ajaxtoolkit Combobox 按钮的内联样式

ruby-on-rails - link_to、redirect_to 和 render 之间有什么区别?

javascript - typescript :空数组的长度不为零

javascript - 如何使用键盘滚动照片

javascript - Visual Studio、Windows 8 HTML5 Javascript 问题

ruby - 在 nokogiri 的两个元素之间抓取文本?