0

I am using rspec-watir to automate some tests, and recently came into a strange issue.
Background: I have a method called select_cards(cards) created that does a search, selects 16 (specifically 16) cards, then adds them to a section... recently I modified my configuration so that the screen window starts at 1920x1080 ( @browser.window.resize_to(1920, 1080) )

Since doing this, whenever I run select_cards(cards) it only selects 12 cards instead of 16. However, when I comment out the window.resize_to method, it successfully grabs all 16

Any idea why resizing the window would cause a behavior change? Any thoughts on how I could workaround this?

Code snippet for how select_cards(cards) works:

def select_cards(cards)
    @browser.button(:class, 'add-cards').click
    # this runs a generic search to return all cards
    search_modal = @browser.div(:class, 'quick-search')     
    search_modal.button(:class, 'test-quick-search').click

    results = @browser.div(:class, 'search-results')

    # This #take() passes in how many cards to click on--as stated; 16
    cards = results.divs(:class, 'card selectable')
    cards.take(cards).each do |assets|
        assets.click
    end
end
kmancusi
  • 591
  • 3
  • 20
  • 2
    When you don't resize the browser, is the window bigger or smaller than the 1920x1080? Does your application have any type of responsive design (ie layout changes based on window size)? – Justin Ko Aug 17 '16 at 21:14
  • @JustinKo, the normal browser window is _smaller_ than 1920x1080, and yes there is some responsive design involved--when the window is shrunk or expanded, the number of cards per row changes (i.e., normal browser window running has 4, 1920x1080 running => 6) – kmancusi Aug 17 '16 at 21:19
  • Interesting, I would have expected the smaller window to be the problem. Sharing how you are locating the elements would help. As well, checking the HTML at the two different browser sizes may provide useful information. – Justin Ko Aug 17 '16 at 21:23
  • Are you sure that's the right method? I don't see how `cards.take(cards)` would work (as I get an exception when I try similar). I assume you pass in 16 for the `cards` parameter. However, you then redefine `cards` as a `DivCollection`. As a result, I would expect `take` to throw an exception due to receiving a `DivCollection` instead of an `Integer`. – Justin Ko Aug 18 '16 at 03:29
  • @JustinKo, yup this is how my method works--I apologize, I should've clarified the parameter for `cards` comes from my `config.yml` file--I admit, I do not know the full ins/outs of YAML and Ruby so I don't know if these values are all read as strings – kmancusi Aug 18 '16 at 13:29

3 Answers3

0

That is just a guess. But it is possible that elements are not exist when they are not scrolled into view (or maybe have other DOMs)

Try to scroll the page (instead of resizing) like:

browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Antesser
  • 669
  • 4
  • 5
0

I've also faced similar issue whenever the elements are not in scroll view. In this situations I used "watir-nokogiri" gem to parse the html and then capture all the elements and perform the action if their is any unique identifier for the captured elements. In your situation I would do something like this:

def select_cards(cards)
  @browser.button(:class, 'add-cards').click
  search_modal = @browser.div(:class, 'quick-search')     
  search_modal.button(:class, 'test-quick-search').click 

  # Parsing HTML into a variable and searching for the results inside the variable
  doc = WatirNokogiri::Document.new(@browser.html)
  results = doc.div(:class, 'search-results') 

  cards = results.divs(:class, 'card selectable')

  cards.take(cards).each do |assets|
    # Assuming that id is the unique identifier for the assets
    @browser.element(:id => "#{assets.id}").click
  end
end

NOTE: this is a work around that I used to deal with these situations and it only works if I can interact with the browser using the captured HTML. If you don't have an identifier, you can try with index or some other way to click it. Using this library, you'll only be able to get all the elements even if they're not in scroll view.

yudi2312
  • 323
  • 1
  • 4
  • 21
0

Oddly enough, I found something that seems to be working for me:
After utilizing this Locating elements without a specific class name so I could make use of another method, I changed my loop to using assets.fire_event ("onclick")
I tested through the code a handful of times and am not having issues selecting all the elements

Community
  • 1
  • 1
kmancusi
  • 591
  • 3
  • 20