Scripting - Watir/Selenium - Miscellaneous

Set path to chromedriver
Usually the location of chromedriver.exe is added to environmental variable PATH, but sometimes if a different version of chromedriver is needed, it can be specified as following:
(Watir)

chromedriver_path = File.join(File.absolute_path('.', File.dirname(__FILE__)),"chromedriver.exe") 
browser = Watir::Browser.new :chrome, driver_path: chromedriver_path
# Or with some options like following
browser = Watir::Browser.new :chrome, driver_path: chromedriver_path, :switches => %w[--disable-extensions]


Switch to Another Window Opened from Primary One
When a new window is opened from the primary one, the following can be used to switch to the new one:
browser1.window(:index => 1).some_method() 
Or
browser1.window(:title => title_secondary_window).use do
  browser1.some_method()
end

Use execute_script

Some tasks can be performed via
  browser.execute_script(javascript_text, *arguments)

Listed below are just (javascript_text, *arguments)

Scroll an element into view
("arguments[0].scrollIntoView(true);", element)

Scroll to the bottom of current page
("window.scrollTo(0, document.body.scrollHeight);")

Scroll vertically down/up by 600 pixels
("window.scrollBy(0,600);")
("window.scrollBy(0,-600);")

Click two elements
("arguments[0].click(); arguments[1].click();", element1, element2)

Get property of pseudo element
("return window.getComputedStyle(arguments[0], arguments[1]).getPropertyValue('background-color');", element, ':after')

Return page/element attributes
("return arguments[0].innerText;", element)
("return arguments[0].className;", element)
("return document.URL;")
("return document.title;")

Pop up an alert
("alert('This is an alert');")
("window.alert('This is an alert');")


More...

No comments:

Followers

About Xman

Basking Ridge, New Jersey, United States