Block Slow Third-Party Requests
Pages load resources that are not relevant to the functionality we test (analytics, social widgets, ads). When they load slowly, they slow down our tests or make them fail. WebDriver BiDi lets the test block them.
Pages load resources that are not relevant to the functionality we test (analytics, social widgets, ads). When they load slowly, they slow down our tests or make them fail. WebDriver BiDi lets the test block them.
Selenium is built to mimic human action (e.g., clicking, typing, dragging, dropping, etc.). So how do you use it to test for broken images?
Working with Selenium straight in code has its advantages. However, if you run into a complicated scenario to automate, it becomes challenging to work through it to completion.
In order to test features in a previous version of Chrome, you would need to run a virtual machine (VM) on your computer with a legit version of Windows installed on the VM.
One of the most heated and subjective conversations in the Selenium community is which locator strategy is better, often circling around two -- XPath and CSS.
To get a real taste of what the performance difference is between CSS and XPath, we will need to dig deeper. We'll do that by running everything from a local machine while using a performance benchmarking utility.
In the last tip I provided benchmarks that showed XPath and CSS performance across various browsers. Since then I've received feedback on some things that my test was missing -- most notably more browser coverage and testing against a larger and deeper page.
There are times when you run across functionality you want to test repeatedly with various inputs to see how the system behaves. We're going to go over a way to do this that will take some of the burden out of the process.
On occasion you may have the need to check if an element on a page is disabled or enabled. Unfortunately, this is not a well-documented function of Selenium, so doing a trivial action like this can quickly become a pain.
In a previous tip we went through how to download files with Selenium by configuring the browser to download them locally and verifying their file size when done. While the previous method works, it requires a custom configuration that is inconsistent from browser to browser.
Previously, we've gone over how to test file downloads without a browser by leveraging Selenium Webdriver and an HTTP library in tandem. This approach is great for testing general file downloads, but what if the file you want to download is behind authentication?
Let's say you want to test some functionality of a web application but it loads things dynamically. You might think about using a hard-coded sleep (that you constantly tweak due to test failures) or you use a blanket timeout that you likely bump up to address test instabilities as well.
Signing up and logging in through the UI makes every test slower and more fragile. When the application has an API, the test can create its user with one HTTP call, log in with a session cookie, and keep the browser for what it needs to check.
Good test reports are a fundamental component of successful test automation, but running down a test failure by looking at a test report can be a real pain sometimes. Oftentimes, you're left to decipher debug statements, or go through things piece by piece all for the sake of trying to track down a transient issue.
It's likely that you'll run into odd test behavior that makes you question the locators you're using in a test. But how do you interrogate your locators to make sure they are doing what you expect?
You want reliable tests. But no matter how hard you try, you're going to run into intermittent test failures. These can be frustrating time wasters that distract you and your team and begin to erode the trust placed in the automated tests.
A page that logs an error to the console, or throws a JavaScript error, can look fine while it is broken. With WebDriver BiDi, the test gets the console messages and the errors of the page as they happen, and can check them.
Images without alt text, fields without labels, and buttons without names keep people who use screen readers out. The test can scan the page with axe and fail when it finds problems like these.
Comparing screenshots in the test code means keeping baselines for every browser and operating system, and reviewing the differences by hand. Sauce Labs Visual stores the baselines, compares the snapshots, and shows the changes for review.
A button that moves a few pixels, a wrong color, or a missing logo pass every functional check. A test that compares screenshots of an element, pixel by pixel, notices them.
Messages that hide after a minute, sessions that expire after 15 minutes, and countdowns make tests slow, since the test has to wait. The test can control the clock of the page and move it forward instead.
Most production applications have some kind of performance monitoring. But how do we catch a slow page before it reaches production? The browser measures every page load, and our Selenium tests can read and check those numbers.
Just like with uploading files we hit the same issue with downloading them a dialog box just out of reach of your automation script.
When the browser runs on another machine (a Selenium Grid, a cloud provider), the files it downloads stay on that machine. Here is how to get them back to your tests.
Pages that show content for where the user is, or format dates and numbers in the user's language, are hard to test from one machine. The test can tell the browser which location, time zone and locale to report.
IDs and CSS classes change when the page is redesigned, and many widgets don't have them. The role and the name of an element, as a screen reader announces them, change less often, and finding elements by them also checks that the page is accessible.
Web components keep their elements in a shadow root, apart from the rest of the page, so a normal locator doesn't find them. The test has to go through the shadow root, or use a tool whose locators look inside it.
It doesn't take long when using Selenium before you run into errors from Selenium that may seem inexplicable . They can be a bit of a shock if you're not sure what they are, how to handle them, or where to find documentation on how to address them.
A page that loads its data from an API is hard to test: the API can be slow, down, or return data that changes. The test can answer the API calls itself, with the data it chooses, and check how the page shows it.
Nearly everyone, at some point during their normal flow of work on the computer, has had to open a new window or tab.
If you want your Selenium tests to automatically run and report failures to you and your team, you'll want to wire them up to a Continuous Integration server.
On occasion you'll come across functionality that requires the use of keyboard key presses in your tests. Perhaps you'll need to tab to traverse from one portion of the page to another, back out of some kind of menu or overlay with the escape key, or even submit a form with Enter. But how do you do it and where do you start?
Invoices, tickets and reports are often printed or saved as PDF. The test can print the page to a PDF, without the print dialog, to check the printed version or to keep it as a record of the run.
Logging in through the form at the start of every test makes the suite slow, and every test fails when the login page has a problem. The test suite can log in once, keep the session cookie, and start each test already logged in.
You'll occasionally have something that you want to test across multiple browsers. It would be nice to run your test once and have it execute against all the browsers you care about. Unfortunately this functionality doesn't come out of the box with most testing frameworks.
In the previous tip I covered how to run a single test against multiple browsers. While this could come in handy, it is still a bit lacking.
Selenium needs a browser driver (ChromeDriver for Chrome) to drive a browser. Selenium Manager finds or downloads the driver and the browser for you, so a test only needs to ask for Chrome.
If you want to run your tests headless on a Continuous Integration (CI) server you'll quickly realize that you can't with an out-of-the-box setup since there is no display output for the browser to launch in.
A Continuous Integration (CI) server usually has no display for the browser to open a window in. Chrome can run headless (without a window), so the tests run there with no extra tools.
Some common use cases for selecting from a dropdown list might be selecting sizes or styles from a dropdown menu while online shopping, or selecting your method of payment.
Compared to Unit and Integration Tests, Selenium is slow. A single test can easily take minutes to run. And when you have a lot of them, it can quickly compound your suite to take hours to complete (just for a single browser) -- hampering your ability to get fast feedback.
Checkboxes are an often used element in web applications. This guide will show you how to work with them in your Selenium tests.
Testing e-mail can be a tricky process. Especially when dealing with things like forgot password. A lot of times this is something that gets checked manually. Or worse, it gets overlooked and ends up broken in production.
Exercising a page's drag-and-drop functionality should be simple with Selenium's Actions API, but HTML5 drag and drop doesn't work with it in every browser.
A chat, a shared document or a multiplayer game only works when one user sees what another user does. One test can start two browsers, one for each user, and check that a message sent by one user reaches the other.
Our Selenium tests already walk through the main journeys of our users. With WebDriver BiDi, a test can record the requests of a journey in a HAR file, and a load testing tool like k6 can replay them with many virtual users.
Uploading a file is a common piece of functionality found on the web but when trying to automate it, you get prompted with a dialog box that is just out of reach of your automation script.
When the browser runs on another machine (a Selenium Grid, a cloud provider), it cannot see the files on our machine. Here is how to send a file to it and upload it.
With Selenium Grid you can stand up a simple infrastructure of various browsers on different operating systems to not only distribute test load, but also give you a diversity of browsers to work with.
In this tip, you'll learn how to work with frames and write tests against them.
If you need to work with mouse hovers in your tests, it might not be obvious how to do this with Selenium. A quick search through the official Selenium documentation can also be confusing, and might leave you scouring online forums and search engines for the solution.
Built into Selenium is the ability to switch to an alert window and either accept or dismiss it. This way your tests can continue unencumbered by dialog boxes that may feel just out of reach.
This tip will explain how to work with multiple windows and switch between them.
Odds are at some point you've come across the use of tables in a web application to display data or information to a user, giving them the option to sort and manipulate it. Depending on your application it can be quite common and something you will want to write an automated test for.
In order to make our Selenium tests resilient, we need to make them wait for certain elements to load. Elements that we want to interact with. This is especially true with JavaScript heavy pages.
In a previous tip we covered how a REPL can help you determine what code to write in your scripts by testing locators and actions through an interactive prompt. This is great for simple tasks, but its starts to falter when debugging more complex scripts.
Tagging is a powerful tool for targeted test execution, as well as a lightweight form of documentation. But what happens when you want to see a summary of the tags you're using across all of your tests? Unfortunately, most solution don't offer this kind of functionality out of the box, leaving you to manually sift through your tests to get a sense of the different tags you're using.
In web design CSS (Cascading Style Sheets) is used to apply styles to the markup (HTML) on a page. CSS is able to do this by declaring which bits of the the markup it wants to interact with through the use of selectors.
Occasionally when running tests you may see unexpected behavior due to A/B testing of the application you're working with. In order to keep your tests running without issue we need a clean way to opt-out of these split tests.
There are times when you are testing a page (or numerous pages) and you want to verify that it responded correctly. A great way to handle this is by checking the HTTP Status Code that the browser received. However this functionality is not available in Selenium WebDriver.
We all write tests with the best intention of having them pass and, when they fail, to reveal legitimate issues in the application we're testing. But that's not always easy to accomplish. Especially when your application is dependent upon third-party service providers (e.g. payment providers, social networks, etc).
Sometimes you'll run into an app that has functionality hidden behind a right-click menu (a.k.a. a context menu). These menus tend to be system level menus that are untouchable by Selenium. So how do you test this functionality?
Safari comes with its own driver, safaridriver, on every Mac. It needs two settings turned on once, and then Selenium runs our tests on Safari like on any other browser.
As your test suite grows you'll likely employ some kind of folder structure to help make sense of everything -- grouping similar tests together. But what do you do when you want to run a set of tests that span across your organizational structure? Especially when your organizational structure isn't set in stone?
With browser tests it can often be challenging to track down the issue that caused a failure. By itself, a failure message along with a stack trace is hardly enough to go on, especially when you run the test again and it passes. You may or may not be able to recreate that initial error message, and might be left wondering what it was, and whether it will cause issues in the future.
With Selenium we can test our application on different browsers. But how does it behave on a slow connection? Chrome can limit its own network, so the tests can find out.
In a previous tip, we went through creating a simple Page Object to capture the behavior of a page we were interacting with. While this was a good start, it leaves a lot of room for improvement. One of the biggest issues is that there are common actions we will likely need across multiple Page Objects and, with our current approach, we would end up with duplicate code.
One of the biggest challenges with Selenium tests is that they can be brittle and challenging to maintain over time. This is largely due to the fact that things in the app you're testing change, breaking your tests.
Sometimes you'll work with applications that are secured behind Basic HTTP Authentication. In order to access them you'll need to pass credentials to the site when requesting a page otherwise you'll get a system level pop-up prompting you for a username and password rendering Selenium helpless.
There may come a time when you want to access Selenium logs in real time. Although there is a way to do this where you have to explicitly request the logs after each test action, what if we wanted information for every test action?