Case Study: Simulate Two Users with Selenium Automation Scripts
An impressive demonstration of a typical enterprise business process scenario with automation scripts.
This article is included in my “How to in Selenium WebDriver” series.
This article will show the execution of a Selenium WebFriver script to test/showcase a typical business web app. I will explain some automation techniques along the way.
Typically, a business app has two types of users: client and business user (owner and staff). There are business interaction processes between these two users. For example, in my WhenWise booking app, after a client makes a booking on the app, a business user can view the booking details.
Hence, to develop a true end-to-end automated test, a test automation engineer needs to drive two browser windows (which is not possible with Cypress) to simulate two users. With the help of Selenium WebDriver, it is an easy job.
“You cannot use Cypress to drive two browsers at the same time.”
— the offical Cypress Doc
Let’s see the action first (execution time: 32 seconds, below is an animated GIF. A video version can be found towards the end of the article).
Framework/Tools
Framework: raw Selenium WebDriver in Ruby binding, ver: 4.0.0.beta3
Tool: TestWise 6 is used here. However, there is no dependency on TestWise; the User may edit the test in NotePad and run it from the command line.
Syntax framework: RSpec, “BDD for Ruby.”
(The whole setup/test script work on Windows, macOS, and Linux)
Test Flow
Business user logs in, navigates to the Calendar page (tomorrow)
A client logs in (on mobile), finds the business and makes a booking (of a driving lesson) for the next day
The business user refreshes the calendar and finds the new booking.
Task 1. Start two browsers with different sizes
The business user (using a desktop computer).
driver = Selenium::WebDriver.for(browser_type, browser_options)
driver.manage().window().resize_to(800, 640)
The client (using iPhone 8 Plus).
driver2 = Selenium::WebDriver.for(browser_type, browser_options)
driver2.manage().window().resize_to(414, 640)
After that, use driver
and driver2
for business user operations and client operations respectively.
Task 2. Align two browser windows side by side
Move the browser windows to the specified position.
driver.manage().window().move_to(410, 10) # biz user
# ...
driver2.manage().window().move_to(10, 10) # client
Challenge 1. The browser window (client) is actually larger than the specified iPhone size.
The reason: the minimum address bar width is wider than the iPhone Plus.
Solution: Disable the address via ChromeOptions.
chrome_options.add_argument("--app=
http://www.google.com
");
This should also hide this info bar.
If not, you may do this explicitly.
the_chrome_options =
Selenium::WebDriver::Chrome::Options.new(
options: {"excludeSwitches" => ["enable-automation"]}
)
Challenge 2. Hide the “Save Password” Popup
To disable it, add the following two preferences in ChromeOptions.
chrome_options.add_preference(:credentials_enable_service, false)
chrome_options.add_preference(:password_manager_enabled, false)
Challenge 3. Hide “Notifications” Popup
It can also be disabled by passing an argument to ChromeOptions.
chrome_options.add_argument("--disable-notifications")
Q & A
How to use page object patterns with multiple browser windows?
Passing driver
as an argument, like below
login_page = LoginPage.new(driver) # business user# ...login_page = LoginPage.new(driver2) # client
2. Is this for demo purposes only?
No. The current version (shown below) is extracted from a real test automation script for a showcase. Some assertion steps have been removed.
For more information, please read this article: “Use UI Automation to Assist Agile Showcases.”
3. How many automated tests (like this one) are there for WhenWise?
As of today (2021–05–09), 502. Check out this article: “WhenWise Regression Test Suite Reaches 500 Selenium tests and ~300K Test Executions.”
And yes, we ran the regression suite if there were any changes to the app. This Continuous Testing process enables the app development to release to production multiple times of day if necessary.
4. How do you handle application changes?
Design your tests well, see this article: Maintainable Automated Test Design.
Full Test Script
The top-level test script is shown below. The test_helper.rb
and page classes are similar to the samples I put on GitHub.
load File.dirname(__FILE__) + "/../test_helper.rb"
describe "Multi Window" do
include TestHelper before(:all) do
# browser_options, site_url are defined in test_helper.rb
@driver = Selenium::WebDriver.for(:chrome, browser_options)
driver.manage().window().move_to(10, 10)
driver.manage().window().resize_to(800, 640)
driver.get(site_url + "/reset")
end
after(:all) do
driver.quit unless debugging?
end
it "Driving 2 Windows as two different users" do
visit("/sign-in")
login_page = LoginPage.new(driver)
login_page.enter_email("driving@biz.com")
login_page.enter_password("test01")
login_page.click_login
visit("/calendar")
driver.find_element(:class, "fc-next-button").click
driver.manage().window().move_to(410, 10) driver2 = Selenium::WebDriver.for(browser_type, browser_options)
driver2.manage().window().resize_to(414, 640)
driver2.manage().window().move_to(10, 10)
driver2.get(site_url + "/sign-in")
login_page = LoginPage.new(driver2)
login_page.enter_email("james@client.com")
login_page.enter_password("test01")
login_page.click_login
driver2.get(site_url + "/biz/wise-driving")
book_resource_calendar_page = BookResourceCalendarPage.new(driver2)
# default today
book_resource_calendar_page.click_on_tomorrow
try_for(2) { book_resource_calendar_page.click_book(7, "900-1000") }
sleep 0.5
confirm_booking_modal_page = ConfirmBookingModalPage.new(driver2)
confirm_booking_modal_page.enter_pick_up_address("22 Hilda Street, Alderley")
confirm_booking_modal_page.click_confirm
sleep 1
elem = driver.find_element(:class, "fc-reload-button")
highlight_control(elem)
elem.click
sleep 1
end
end
Video (youtube):