Step By Step Process For Chrome Extension Automation Using Java-Selenium and TestNG
Step 1: Download Get CRX Extension
To automate chrome extension, first you need to add Get CRX extension. Visit Chrome Web Store and search Get CRX. From the search results, you should see the Get CRX extension listed. Click on the Get CRX extension to open its details page.
From details page, click [Add to Chrome], a confirmation dialog will appear, click [Add Extension]. Wait for installation, once the installation is complete, click on Extension button and you'll see Get CRX extension in extension section.
Step 2: Download Desires Extension And It's CRX File
Now you have to add the desired extension and it's crx file which you want to automate. As I'm showing automation for Metamask, so I'll show step by step process for Metamask. Visit Chrome Web Store and search Metamask. From the search results, you should see the Metamask extension listed. Click on the Metamask extension to open its details page. From details page, right click on and you'll see an option named Get CRS of this extension to get the crx file of this extension. Download the crx file of Metamask and save it to your automation code destination.
Now, add the Metamask extension in your chrome browser.
Step 3: Run Automation For Extension
Here’s the syntax for this code in Java language often used in scripts. By adding these code into your automation code you can easily automate chrome extension.
Recommended by LinkedIn
public class Testrunner {
public WebDriver driver;
@Test
public void automateExtension() throws InterruptedException {
String extensionPath="./src/test/resources/metamask.crx";
ChromeOptions options= new ChromeOptions();
options.addExtensions(new File(extensionPath));
driver= new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.get("https://www.google.com/");
}
}
Navigate to HTML Home Page Of Desired Extension
All the extension you can't navigate to html home page. Only few extensions have this option. As I'm showing process for Metamask, when you add Metamask extension. After installation you'll redirect to the Metamask home page. But if you want to visit home page manually, then visit Manage Extension page.
In extension page, you'll see all the installed extensions. Enable Developer mode, click on [Deatils] of Metamask extension.
It'll redirect to the Metamask details page. Scroll to [Inspect Views] and click [home.html].
It will also redirect you to the Metamask home page.
Now, if you want to navigate Metamask home page through automation then copy the home page url and paste it to your code.
The syntax to do this is below:
driver.get("chromeextension://nkbihfbeogaeaoehlefnkodbefgpgknn/home.html");
After doing this, you can interact with and test the extension as they would a normal HTML webpage.
Really useful
Md. Tanvir Hossain Mitul Very insightful. Thank you for sharing