We have discussed a lot about Desired capabilities, ChromeOptions and technique to set proxy in Selenium by using Chrome Options class. We knew that ChromeOptions is one of the upgraded class for Java to set the capabilities Since DesiredCapabilities have been deprecated in Java. Today we are going to discuss the technique to add chrome extension in the browser at runtime using Selenium WebDriver. We use Chrome Options here too to add the chrome extensions.
Let’s look forward.
How did I come across this scenario to add the chrome extension during test execution?
I got an assignment from one of the banking clients who is mainly based in North America. Their insurance portal is restricted in our country; hence, I was unable to access their website to capture the web elements to write the test cases. Therefore, I thought to use Virtual Proxy Network (VPN) to bypass the connection, hence, I downloaded one chrome extension and captured the web elements, thus, by doing so, I finished writing the test cases.
Now I had to execute the test cases to make sure that there are no breakpoints. We already know that WebDriver creates a new session each time it triggers the new browser. Hence, I didn’t find that Chrome extensions for VPN during test execution. After research, I found a document on official chromium site where the declaration was given. You can refer them for more info, here are their links available:
The scenario could be anything; You can use this implementation whenever required. I just explained How I took advantage of the availability of insertion of chrome extension in Selenium WebDriver.
How to add chrome extension at runtime using Selenium WebDriver?
Following piece of code helps us to add the chrome extensions at runtime using Selenium WebDriver.
ChromeOptions option = new ChromeOptions(); option.addExtensions(new File("Path to the crx file"));
Steps to implement insertion of chrome extension at runtime using Selenium WebDriver
Below mentioned steps elaborate the insertion of chrome extensions using Selenium.
Step# 1: Get the .crx file of the extension
First, you need to download the .crx file of the extension. Go to https://crxextractor.com/ then enter the link of the chrome extension in the download box then click on Get .CRX button. See image below:
Step# 2: Give the path of the crx file to the addExtensions() method
Following sample program adds an extension at runtime.
package Test; import java.io.File; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.CapabilityType; public class AddChromeExtension { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe"); ChromeOptions option = new ChromeOptions(); option.addExtensions(new File("C:\\Users\\Avinash\\Downloads\\extension_3_5_0_0.crx")); WebDriver driver = new ChromeDriver(option); driver.get("https://www.inviul.com/"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); System.out.println(driver.getTitle()); } }
See below image, chrome extension has been added.
This was all about the insertion of chrome extension at runtime. If you have any queries, suggestions or feedback do post in the comment form below and don’t miss to join our Facebook group for quick updates.
Suggested Readings
- Selenium Tutorials
- TestNG Tutorials
- Selenium Grid
- Cucumber Tutorials
- Selenium with Jenkins
- Reporting and Logging
You have made nice blog, nice to read your post.
Hi,
Your post helped me install chrome extension during run time using selenium java.
Would you please help me in how to click on the extension after installing it.
Your help is appreciated.
Thanks,
Hadeel
Hi Hadeel,
I think clicking on chrome extension should not be your scope of the testing, rather, you should test contents of your extension. You just have to get the URL of your extension and rest you can handle it. I am going to write a detailed tutorial on it.
Stay tuned.
Thanks,
Avinash
Hi Hadeel,
I am also searching like you question. Did you get any answer for that. If you get please let me know.
Thanks
Anusha