Sample Selenium WebDriver Script
Look at the working of a Selenium script in the chrome browser.
Using Selenium Webdriver for Chrome
Letās run the sample script to visualize the test steps in the output tab of the widget below or click the link (below the āRunā button) to open the preview in a new browser tab.
require 'selenium-webdriver' caps = Selenium::WebDriver::Chrome::Options.new(args: ['--no-sandbox']) # Launching browser instance browser = Selenium::WebDriver.for :chrome, :capabilities => caps browser.navigate.to("http://travel.agileway.net") # Signing in to the site browser.find_element(:id, "username").send_keys("agileway") browser.find_element(:id, "password").send_keys("testwise") browser.find_element(:xpath,"//input[@value='Sign in']").click browser.find_elements(:name => "tripType").each { |elem| elem.click && break if elem.attribute("value") == "oneway" && elem.attribute("type") == "radio" } select_elem = browser.find_element(:name, "fromPort") options = select_elem.find_elements(:tag_name, "option") options.each { |opt| opt.click if opt.text == "New York"} Selenium::WebDriver::Support::Select.new(browser.find_element(:name, "toPort")).select_by(:text, "Sydney") Selenium::WebDriver::Support::Select.new(browser.find_element(:id, "departDay")).select_by(:text, "04") Selenium::WebDriver::Support::Select.new(browser.find_element(:id, "departMonth")).select_by(:text, "March 2021") browser.find_element(:xpath,"//input[@value='Continue']").click browser.find_element(:name, "passengerFirstName").send_keys("Wise") browser.find_element(:name, "passengerLastName").send_keys("Tester") browser.find_element(:xpath,"//input[@value='Next']").click browser.find_elements(:name => "card_type").each { |elem| elem.click && break if elem.attribute("value") == "visa" && elem.attribute("type") == "radio" } browser.find_element(:name, "card_number").send_keys("4000000000000000") Selenium::WebDriver::Support::Select.new(browser.find_element(:name, "expiry_year")).select_by(:text, "2013") browser.find_element(:xpath,"//input[@value='Pay now']").click browser.quit
Testing demo site: Agileway
Let's walk through the above Ruby script and make sure we understand its functionality.