Created
June 15, 2022 20:44
-
-
Save dragonskyside/0b5526d1fb10bc8d2293a5b529f57b13 to your computer and use it in GitHub Desktop.
Ruby code to illustrate main functionality of Watir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'watir' | |
require 'webdrivers' | |
#Start a new session | |
browser = Watir::Browser.new | |
browser.goto ("wikipedia.org") | |
#Count all the links on the page. | |
puts browser.links.count | |
#Find, verify and click specific links | |
english_link = browser.div(class: "lang1") | |
english_link.present? | |
english_link.click | |
puts browser.url | |
#Extract data from web page | |
puts browser.element(id:'articlecount').text.strip | |
# Locate a text field, wait, and set text | |
search_box = browser.text_field(class: "vector-search-box-input") | |
search_box.wait_until(&:present?).set("sailing yachts") | |
#Locate button associated with searchbox and execute a search. | |
button = browser.button(value: "Go") | |
button.click | |
puts browser.url | |
#execute some JS | |
browser.execute_script("alert('On the page for sailing yachts !')") | |
browser.alert.wait_until { |a| a.text == "On the page for sailing yachts !" } | |
sleep 3 | |
browser.alert.ok | |
#Take a screenshot and close browser | |
browser.screenshot.save("./my_watir_screenshot.png") | |
browser.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment