Selenium 4 New Features by Examples in Ruby

Zhimin Zhan
4 min readMar 27, 2021

Selenium WebDriver 4 Beta is released and I updated my parallel testing lab to use Selenium 4. In the article, I will show what’s new in Selenium 4 with examples (in Ruby binding). I will focus on the core test framework (matters to test scripts), excluding optional components such as Selenium IDE and Selenium Grid.

There are many articles/blog posts on “Selenium 4 New Features” (such as the ones in DZone and SauceLabs Blog), but I haven’t seen the official one except for some talks by Simon Stewart, like this one.

I like learning by doing, here are some quick examples I came up with.

If you own the Selenium WebDriver Recipes in Ruby ebook, you can get a set of Selenium 4 recipe test scripts on the book site now. The content is yet to be added to the ebook.

1. Relative Locators

Relative Locators have been touted as the key Selenium 4 feature. However, there were very limited examples of relative locators. I searched for the Selenium repository (Ruby), with only 10 references in one integration test file.

I will write a separate article on using relative locators. For now, I will share one: “to the right of a fixed element”.

start_cell = driver.find_element(id: "test_products_only_flag")
elem_label = driver.find_element(relative: { tag_name: "span",
right: start_cell })
expect(elem_label.text).to eq("Test automation products only")

2. Save the screenshot of a specific web element

driver.save_screenshot to take a screenshot of the page and save to a file. Selenium 4 allows you to take a screenshot of a web element as well.

 elem = driver.find_element(:id, "get_coupon_btn")
elem.save_screenshot("/tmp/button.png")

3. Simper ways to open a new browser window or tab

We could open a new browser window and tab in Selenium 3, v4 offers a simpler syntax.

new_win = driver.manage.new_window(:window)
driver.switch_to.window(new_win)
driver.get("https://whenwise.com")
driver.close
driver.switch_to.window(driver.window_handles[0])
new_tab = driver.manage.new_window(:tab)
driver.switch_to.window(new_tab)…
Zhimin Zhan

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.