_ruby
TL;DR - Show Me The Code
require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers
def setup
@driver = Selenium::WebDriver.for(:firefox)
end
def teardown
@driver.quit
end
def run
setup
yield
teardown
end
run do
@driver.get 'https://the-internet.herokuapp.com/windows'
@driver.find_element(css: '.example a').click
@driver.switch_to.window(@driver.window_handles.first)
expect(@driver.title).to eql 'The Internet'
@driver.switch_to.window(@driver.window_handles.last)
# Waiting for the new window to have a title, then we consider it is loaded
wait = Selenium::WebDriver::Wait.new(timeout: 2)
wait.until { @driver.title != "" }
expect(@driver.title).to eql 'New Window'
end
run do
@driver.get 'https://the-internet.herokuapp.com/windows'
first_window = @driver.window_handle
@driver.find_element(css: '.example a').click
all_windows = @driver.window_handles
new_window = all_windows.find { |window| window != first_window }
@driver.switch_to.window(first_window)
expect(@driver.title).to eql 'The Internet'
@driver.switch_to.window(new_window)
# Waiting for the new window to have a title, then we consider it is loaded
wait = Selenium::WebDriver::Wait.new(timeout: 2)
wait.until { @driver.title != "" }
expect(@driver.title).to eql 'New Window'
end
Code Walkthrough
Importing Libraries, Setup and Teardown
We'll first need to pull in our requisite libraries (selenium-webdriver
to drive the browser, and
rspec/expecations
& RSpec::Matchers
to perform our assertions) and wire up some simple setup
, teardown
,
and run
methods.
Example 1
Line 19 shows a test that exercises new window functionality from an application. In this case, we'll be using the new window example from the-internet.
After loading the page we click the link which spawns a new window. We then grab the window handles (a.k.a. unique identifier strings which represent each open browser window) and switch between them based on their order (assuming that the first one is the originating window, and that the second one is the new window). We round this test out by performing a simple check against the title of the page to make sure Selenium is focused on the correct window.
While this may seem like a good approach it can present problems later because the order of the window handles is not consistent across all browsers. Some return it in the order opened, others alphabetically.
Here's a more resilient approach. One that will work across all browsers.
Example 2
Line 31 shows that after loading the page we store the window handles in a variable (e.g., first_window
)
and then proceed with clicking the new window link.
Now that we have two windows open we grab all the window handles and search through them to find the new window
handle (e.g., the handle that doesn't match the first one we've already stored). We store the result in another
variable (e.g., second_window
) and then switch between the windows. Each time checking the page title to make sure
the correct window is in focus.
For more information about switching windows (and tabs) visit the official Selenium documentation page.
Executing the Test
Before executing the test, we need to make sure the required dependencies are declared on the Gemfile
file.
Toggle to see the Gemfile
file.
source 'https://rubygems.org'
gem 'rspec', '~> 3.10'
gem 'selenium-webdriver', '~> 4.24.0'
Finally, we can run the test by executing ruby windows.rb
from the command-line.