This post is from the Experitest blog and has not been updated since the original publish date.
How to use browser extensions with Selenium Testing
Learn how to run cross-platform Selenium testing on any extension, based on the Experitest cloud-service using the REST API application.
Modern browsers have great functionality for displaying content, tracking GPS coordinates, remembering passwords and much more. However, there is always a need for developers to come up with ways to extend browser functionality even more. This is why browser extensions are created. Such extensions can be tested in the manual testing process, but it will be better to see if we can do it automatically as part of a test automation process. If we can use it as part of our Selenium testing then it would be that much better.
So let's consider this case as an example. We need to perform a standard UI autotest using Selenium testing, during which we will use the REST API chrome extension to send requests for launch tests on various Experitest environments. Let's start doing this step by step with some brief explanations:
1) Create a Java Maven Project
The first thing we need to do is to create a Java Maven project in our IDE with default settings. IDEA is used as an IDE in all examples of this article. Create a class with a simple Selenium code that you can see here.
If you are using the IntelliJ IDEA, don't forget to enable auto-import, and confirm an import of all necessary dependencies (click Ctrl+Enter).
Make sure everything works correctly:
2) Launching Chrome Browser Instance
Please note that when we launch our Chrome Browser instance, it doesn't have any extensions.
However, since we plan to use one of them, let's talk about how to add and use it.
First, we need to find the necessary extension in the chrome web store:
In order to use the application manually, we can add it to your Chrome from the store, but for auto-testing we can't use the same method. Leave a bookmark on this page, we will return to it later.
b) Copy the URL, and go to chrome-extension-downloader.com web site to download the file of this extension to your work machine.
d) Run the test again and make sure everything worked out. It is important to pay attention to the fact that the first part of the test still works correctly, and our extension was successfully installed. You should see this as a result:
In manual testing, in order to start using the extension, just click on its icon and continue to use it (the way and actions depend on the specific extension).
e) At its core, each extension is just a set of files, which is a typical web application. This understanding allows us to use Selenium testing to manage extensions! We need to look at what this application consists of to find the HTML pages, and here's how:
a) Open the Chrome web app store again, find and install the "Chrome extension source viewer" app:
b) It's time to return to the REST API installation page in the web store (where I asked to bookmark earlier). Important point: In order to open any extension as a simple web page, go to the corresponding address of the form: chrome-extension:///.html. We need to copy from the URL:
Also, on this page, CRX will become active, and we will be able to open the source code for the extension that we are interested in:
c) Here we see what the application consists of. Use the search to find only HTML pages:
At this stage, we can analyze the pages found, and determine the main one of them (usually it's not difficult). So, in our case, this page will be "restlet_client.html". Let's copy the name of this page (sometimes it can be a path and not just the final name of the page) and paste it into our link, merging with ExtensionID that we got earlier.
d) Let's go to the received URL (chrome-extension://aejoelaoggembcahagimdiliamlcdmfm/restlet_client.html) in a new tab:
It works! This is a very good approach, since it allows you to not only use the extension as a tool for testing, but also to test the extension itself. But let's return to using it in the process of testing another web application.
This code will open REST API app in a new tab.
4) Creating a Test Suite with REST API
Now we must prepare the tests for the second part of our script. Go to the REST API application, and create a test suite. As a set of APIs, we will choose to perform the test via the Experitest cloud platform - this is a good solution because we can include cross-platform testing here. Start testing on different platforms using the Experitest API, by reading more here. For simplicity's sake, we will use the default Demo Test. We will run the tests one by one, waiting for the completion of testing on another platform. In the end, make sure everything went well. I got this test set:
Performing this test requires authorization on experitest.com, as well as the ability to download files without using the system api, let's add this to our Java code. The final version can be viewed at this link.
If you did everything correctly, you will see the following as a result:
Then, the Chrome browser will be closed, and you will see the test status in your IDE:
So let's summarize:
We have figured out how to run any extensions using ChromeDriver.
We have learned how to use extensions by Selenium testing code. This allows us to not only use the functionality of the extension for auto-testing but also opens up opportunities for testing the extensions themselves!
We have figured out how to run cross-platform testing based on the Experitest cloud-service using the REST API application.
One more Selenium testing tip:
Within the framework of a real project, scripts for testing APIs (or, as in our case, running tests with parameters using APIs) can be placed in a repository over which a team of engineers works. In this case, you can easily apply the method earlier described in the article "How to use Jenkins pipeline to launch Selenium tests" in order for your Jenkins to run the tests automatically when necessary and you can always make sure that the changes you have made do not damage the application.