Broken Images
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?
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.
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.
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.
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.
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.
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.
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.
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.
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 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?
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.
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?