刷新浏览器后的Ruby全局变量

时间:2016-09-07 17:43:58

标签: javascript ruby watir acceptance-testing user-acceptance-testing

我对Ruby非常陌生并且使用Watir开发脚本以进行验收测试,而且我陷入了一种我甚至不确定是否有可能退出的问题。

我有一个运行的小脚本,基本上会查看下拉列表,将所有下拉项存储到一个全局变量中,然后迭代并运行其他一些函数。

当我尝试在为每个下拉项运行所有函数之后刷新浏览器然后尝试迭代到下一个下拉项并执行相同的函数直到所有下拉项都完成时,会出现问题。

$browser = Watir::Browser.new :chrome
$wait = Watir::Wait

def wait_while_loading(what)
    lightbox = $browser.div(:class , 'lightbox')

    begin
        # Capture load start
        start = Time.now

        $wait.until { lightbox.present? }
        $wait.until { !lightbox.present? }

        # Capture load end
        finish = Time.now
        load_time = (finish - start).to_s
    rescue => e
        print e.backtrace.join("\n")
        #log_error('Failed while loading ' + what + '.')
    end 
end

def cycle_all_views
    $views = []
    $current_view

    $view_dropdown = $browser.div(:id, 'views-dropdown') 

    $views = $browser.div(:class, 'dropdown-views').div(:class, 'dropdown-menu--footer').ul(:class, 'dropdown-menu').divs(:class, 'dropdown-menu-item')

    start_on_view = 10

    begin
        $views.drop(start_on_view).each_with_index do |view, i|     
            $view_dropdown.click

            $current_view = view.text
            index = i.to_s
            safe_name = sanitize($current_view)

            view.click
            wait_while_loading($current_view)

            $browser.refresh
            wait_while_loading('Refreshing Page...')
        end
    rescue => e
        #log_error('Failed to load view ' + $current_view)
    end
end

触发$browser.refresh后,在迭代到下一个$views_dropdown后点击view,我会收到一条错误消息,指出no implicit conversion of nil into String假设在尝试$current_view = view.textC:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele ments/element.rb:536:in `assert_element_found' C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele ments/element.rb:524:in `ensure_not_stale' C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele ments/element.rb:503:in `assert_exists' C:/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/ele ments/element.rb:81:in `text' main.rb:55:in `block in cycle_all_views' main.rb:52:in `each' main.rb:52:in `each_with_index' main.rb:52:in `cycle_all_views' main.rb:122:in `block in <main>' main.rb:112:in `each' main.rb:112:in `<main>'Failed to load view First View 1}}。页面刷新后是否会丢失对变量的所有赋值?如果是这样,有没有办法保留它们?

如果有人也能指出我调试Watir脚本的更好方法,我很想知道如何让我的生活更轻松。

堆栈跟踪返回:

Set-AWSCredentials -AccessKey AAAAAAAAAAAAAAA -SecretKey AAAAAAAAAAAAA -StoreAs default

2 个答案:

答案 0 :(得分:0)

答案几乎就是我在这里提出的:https://stackoverflow.com/a/39332788/4072371

使用索引而不是迭代集合

答案 1 :(得分:0)

这是一个展示这个问题的例子(在信用到期时给予信任 - 识别出titusfortner):

require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto('http://www.iana.org/domains/reserved')

first_link_from_divs = b.div(id: "sidebar_left").links.first
first_link_from_divs.text                                       #=> Overview
first_link_from_divs.exists?                                    #=> true

just_a_link = b.link(text: "Overview")
just_a_link.text                                                #=> Overview
just_a_link.exists?                                             #=> true

b.refresh
first_link_from_divs.exists?                                    #=> false
just_a_link.exists?                                             #=> true