Selenium 4 New Features by Examples in Ruby
Selenium WebDriver 4 Beta has been 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 it 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)
driver.get("https://testwisely.com")
driver.close
driver.switch_to.window(driver.window_handles[0])
4. Save the page to PDF
Export a web page directly to a PDF file.
driver.save_print_page(“/tmp/web_page.pdf”)
Please note, this new feature only works in headless mode. Otherwise, “PrintToPDF is only supported in headless mode”.
the_chrome_options = Selenium::WebDriver::Chrome::Options.new
the_chrome_options.add_argument(“ — headless”)
the_browser_options = { :capabilities => the_chrome_options }
driver = Selenium::WebDriver.for(:chrome, the_browser_options)
5. Chrome DevTools
As I mentioned in another (the most popular of mine, based on Medium stats) article “Why Cypress Sucks for Real Test Automation?”, Selenium v4 will support Chrome DevTools Protocol (CDP), the one used in Puppeteer and Cypress.
Here is a sample I found:
driver.devtools.page.navigate(url: "http://google.com")
driver.devtools.console.clear_messages
driver.devtools.page.enable
driver.devtools.page.on(:load_event_fired) do |params|
puts("Page loaded in #{params["timestamp"]}")
end
However, it did not work with the error below.
cannot load such file -- selenium/devtools/v89
The reason: it depends on a separate gem selenium-devtools
, however, its release is often out of sync with the Chrome browser. For examples of using Chrome DevTools in Selenium, check out this article.
Personally, I don’t have much interest in CDP. Since 2011, I have been developing/debugging/maintaining raw Selenium WebDriver tests very efficiently with TestWise.
People who worked with me, often are impressed with my productivity on creating/maintaining automated test scripts (in raw Selenium WebDriver). My reply: “You can too if do things properly with the right tool”.
If you are aware of other Selenium v4 features that I missed and would like an example, please let me know.
See my other articles:
“WhenWise Regression Test Suite Reaches 500 Selenium tests and ~300K Test Executions”, achieving Level 4 for AgileWay Continuous Testing Grading
Step by Step Showing Why Selenium WebDriver is the Easiest-to-Learn Web Test Automation Framework
Selenium v4 recipe test scripts will be added to my book Selenium WebDriver Recipes in Ruby, some of the recipe tests are already available on the book site.
Also, the new v4 recipes will be added to my other “Selenium WebDriver Recipes” books: Java, JavaScript, and Python.