Home > Software design >  WATIR: Is there a reason why browser scrolling won't work?
WATIR: Is there a reason why browser scrolling won't work?

Time:10-20

I'm trying to use browser.scroll.to :bottom but nothing happens. I know that it works because I've tried it on public facing sites such as the BBC and Wikipedia, but for some reason, these scroll commands don't work on our in-house browser based app.

Does anyone know of any reasons or settings that could be preventing this from happening? Things such as browser.refresh and browser.window.maximize work fine but the scrolling literally refuses to budge.

Here's what my code looks like:

require 'watir'
require_relative 'regression_config_bob01.rb'
require 'date'
require 'faker'
require 'slack-notifier'
require 'watir-scroll'

user_name = "blah"
password = "blah"

test_env = "the Site"


browser = Watir::Browser.new 
driver = browser.wd           # The new line

browser.goto(test_env)

# Login
browser.text_field(:name => 'P101_USERNAME').set user_name
browser.text_field(:tabindex=> '2').set password
browser.link(:text => "Log in").click

sleep 20

browser.scroll.to :bottom

print "done"

sleep 30

CodePudding user response:

The current scroll implementation is using javascript to move the page around. For some reason your app is not responding to the JS commands in the normal way.

Selenium has recently released native scrolling via the drivers that Watir has not yet been updated. Take a look at the Selenium documentation on it: https://www.selenium.dev/documentation/webdriver/actions_api/wheel/?language=ruby

To use this in watir, you just need to add the line to the top: driver = browser.wd

CodePudding user response:

like Titus said, Watir is a wrapper for Selenium and lets you write shorter and prettier code, like

browser.scroll.to :bottom
browser.scroll.to :top
browser.element(id: "asd").scroll.to

but when (for any reason) it doesn't work you can use Selenium syntax directly by sending commands to browser.wd instead browser like this:

browser.wd.action.scroll_by(0, 1000).perform #this scrolls bottom
browser.wd.action.scroll_by(0, -1000).perform #this scrolls top  
browser.wd.action.scroll_to(browser.wd.find_element(id: "asd")).perform

but instead of writing browser.wd every time you need to talk to driver directly, you can type driver = browser.wd so your code is more clear:

driver = browser.wd
driver.action.scroll_by(0, 1000).perform #this scrolls bottom
driver.action.scroll_by(0, -1000).perform #this scrolls top  
some_element = driver.find_element(id: "asd")
driver.action.scroll_to(some_element).perform

There are countless examples where using Watir is more efficient than talking to driver directly, you just happened to encounter an edge case.

Also try avoiding sleep when possible, instead, wait for something to show, like:

browser.element(id: 'asd').wait_until(&:present?)
browser.element(id: 'asd').wait_until({ |el| el.text == "Success" })
some_element = browser.element(id: 'asd')
browser.wait_until(30){some_element.present?}

Instead of .element you can use correct subclass like .div, .button, .span etc

  • Related