ascendantofrain
ascendantofrain

Reputation: 302

Ruby Global Variables After Refreshing Browser

I am very new to Ruby and developing scripts with Watir for acceptance testing and I am stuck in a rut that I'm not even sure is possible to get out of.

I have a small script that runs that basically looks at a dropdown list, stores all of the dropdown items into a global variable which I then iterate over and run some other functions.

The issue occurs when I try to refresh the browser after running all of the functions for each dropdown item which then tries to iterate to the next dropdown item and execute the same functions until all dropdown items are complete.

$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

After triggering the $browser.refresh, clicking on the $views_dropdown after iterating to the next view, I am greeted with an error stating no implicit conversion of nil into String assuming when trying to do $current_view = view.text. Do I lose all assignments to variables after the page refresh? If so, is there a way to keep them?

If anyone could also point me to a better way of debugging my Watir scripts, I would love to know of any way to make my life easier.

Stack trace returns this:

C:/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

Upvotes: 1

Views: 199

Answers (2)

orde
orde

Reputation: 5283

Here's an example that demonstrates the issue (which--to give credit where credit is due--titusfortner identified):

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  

Upvotes: 0

titusfortner
titusfortner

Reputation: 4194

The answer is pretty much what I put here: https://stackoverflow.com/a/39332788/4072371

Use index instead of iterating over a collection

Upvotes: 1

Related Questions