Experitest Blog

How to use browser extensions with Selenium Testing

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:

package com.test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium {
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”,”F:\\Jenkins\\workspace\\chromedriver.exe”); // <– Change this path
WebDriver driver = null;
driver = new ChromeDriver();
String baseUrl = “https://experitest.com/”;
WebDriverWait wait = new WebDriverWait(driver, 20);
String expectedTitle = “experitest.com”;
String actualTitle = “”;
driver.get(baseUrl);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“/html/body/header/div/div/div[3]/ul/li[2]/a”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”root\”]/div/div/div[2]/div[2]/div/p/a”))).click();
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)){
System.out.println(“TEST PASSED!”);
} else {
System.out.println(“TEST FAILED”);
}
driver.close();
}
}

 

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:

selenium testing - passed

 

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.

 

a) Installing:

First, we need to find the necessary extension in the chrome web store:

selenium testing - restlet

 

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.

 

c) We modify our code so that when we launch an instance of our browser, our extension is pre-installed into it:

package com.test;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.util.ArrayList;

public class Selenium {

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”,”F:\\Jenkins\\workspace\\chromedriver.exe”); // <– Change this path
WebDriver driver = null;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File(“F:\\Experitest\\ChromeExtensions\\ChromeExtensions\\extensions\\Restlet-Client-REST-API-Testing_v2.19.1.crx”)); // <– Change this path
DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptions);
driver = new ChromeDriver(desiredCapabilities);
WebDriverWait wait = new WebDriverWait(driver, 20);
String baseUrl = “https://experitest.com/”;
String expectedTitle = “experitest.com”;
String actualTitle = “”;
driver.get(baseUrl);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“/html/body/header/div/div/div[3]/ul/li[2]/a”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”root\”]/div/div/div[2]/div[2]/div/p/a”))).click();
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)){
System.out.println(“1st PART IS PASSED!”);
} else {
System.out.println(“TEST FAILED”);
}
}
}

 

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:

selenium testing - source viewer

 

          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:

selenium testing - chrome web store

 

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:

selenium testing - restlet

 

          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:

selenium testing - requests

 

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.

3) Add a new part to our Selenium code:

(JavascriptExecutor)driver).executeScript(“window.open()”);

ArrayList tabs = new ArrayList(driver.getWindowHandles());

driver.switchTo().window(tabs.get(1));

driver.get(“chrome-extension://aejoelaoggembcahagimdiliamlcdmfm/restlet_client.html”);

 

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 performing the test via 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:

selenium testing - requests

 

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 will be:

package com.test;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.util.ArrayList;

public class Selenium {

private static void uploadFile(WebElement fileupload) {
try {
File file = new File(“F:\\Experitest\\ChromeExtensions\\ChromeExtensions\\scenarions_for_REST_API\\REST_API_Experitest.json”); // <– Don’t forget to change this string
fileupload.sendKeys(file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”,”F:\\Jenkins\\workspace\\chromedriver.exe”); // <– Don’t forget to change this string
WebDriver driver = null;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File(“F:\\Experitest\\ChromeExtensions\\ChromeExtensions\\extensions\\Restlet-Client-REST-API-Testing_v2.19.1.crx”)); // <– Don’t forget to change this string
DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptions);
driver = new ChromeDriver(desiredCapabilities);
WebDriverWait wait = new WebDriverWait(driver, 20);
String baseUrl = “https://experitest.com/”;
String expectedTitle = “experitest.com”;
String actualTitle = “”;
driver.get(baseUrl);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“/html/body/header/div/div/div[3]/ul/li[2]/a”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”root\”]/div/div/div[2]/div[2]/div/p/a”))).click();
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)){
System.out.println(“1st PART IS PASSED!”);
} else {
System.out.println(“TEST FAILED”);
}
String Email = “****************”; // <– Don’t forget to change this string
String Password = “**************”; // <– Don’t forget to change this string wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”email\”]”))).sendKeys(Email);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”password\”]”))).sendKeys(Password);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”root\”]/div/div/div[2]/div[3]/div/form/div[3]/button”))).click();
((JavascriptExecutor)driver).executeScript(“window.open()”);
ArrayList tabs = new ArrayList(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get(“chrome-extension://aejoelaoggembcahagimdiliamlcdmfm/restlet_client.html”);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//div[3]/div[2]/div/div/p[2]/button”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//div[2]/section/div[3]/div[1]/a[2]”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//div[2]/section/div[3]/div[1]/ul/li[5]/a”))).click();
WebElement element = driver.findElement(By.xpath(“//*[@id=’gwt-uid-607′]”));
uploadFile(element);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”gwt-uid-608\”]”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”console\”]/div[7]/div[3]/button[2]”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”left-side\”]/div[2]/section/div[2]/div[1]/ul/li/div/i”))).click(); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”left-side\”]/div[2]/section/div[2]/div[1]/ul/li/ul/li/div/i”))).click(); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”left-side\”]/div[2]/section/div[2]/div[1]/ul/li/ul/li/div/span”))).click(); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”console\”]/div[4]/div[4]/div/div[1]/div/div/div[2]/div[3]/button[2]”))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”console\”]/div[4]/div[4]/div[1]/div/div[2]/div[1]/div[1]”))).click();
String testStatus = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”console\”]/div[4]/div[4]/div[1]/div/div[3]/div/dl[2]/div[2]/dd”))).getText();

while (testStatus.equals(“In progress”)) {
testStatus = driver.findElement(By.xpath(“//*[@id=\”console\”]/div[4]/div[4]/div[1]/div/div[3]/div/dl[2]/div[2]/dd”)).getText();
}
if (testStatus.equals(“Success”)){ wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//*[@id=\”console\”]/div[4]/div[4]/div[1]/div/div[2]/div[1]/div[2]”)));
//Since, no exception, so element is present
System.out.println(“2nd PART IS PASSED!”);
driver.close();
driver.switchTo().window(tabs.get(0));
driver.close();
System.out.println(“————-“);
System.out.println(“TEST PASSED!”);
}
else{
//Element is not present
System.out.println(“TEST FAILED!”);
System.out.println(testStatus);
}
}
}

 

If you did everything correctly, you will see the following as a result:

selenium testing - scenarios

 

Then, the Chrome browser will be closed, and you will see the test status in your IDE:

selenium testing - passed

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.

 

Maxim Babilo – Performance Engineer ISSArt QA team

Comments are closed.