Saba Malik
Saba Malik

Reputation: 281

Selenium Webdriver - Need to enable chrome extension while launching the browser

I am using the below code to get a chrome extension(Browsec VPN) to open and automate specific site that must has to open on VPN - My code is able to launch the browser with extension but the extension is always in disable mode - I need to get it in enable mode. Please help! Thank you

package AutomationTesting;

import java.io.File;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Test1 {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
 //WebDriver driver = null; 
 String URL = "https://google.com/";

 System.setProperty("webdriver.chrome.driver", "C:\\New_Selenium\\chromedriver_win32\\chromedriver.exe");

 ChromeDriver driver = new ChromeDriver();

ChromeOptions options = new ChromeOptions();
 options.addExtensions(new File("C:\\New_Selenium\\Browsec-VPN-Free-and-Unlimited-VPN_v3.19.4.crx"));
 //DesiredCapabilities capabilities = new DesiredCapabilities();
 //capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(options);
 driver.get(URL);

 driver.manage().window().maximize();
 Thread.sleep(2000); 

 driver.quit();

    }

}

Upvotes: 4

Views: 15318

Answers (2)

JB-007
JB-007

Reputation: 2441

Caveat

Would recommend doing this only when testing your own extensions - i.e. your responsibility to ensure you do not infringe upon usage rights / T&Cs - in accordance with the provider's legal terms / expectations etc.

As such, response below is somewhat 'generic' / in context of testing your own extension - however, there is brief consideration / exploration of the element you were interested in, purely for academic / theoretical interest

Summary

  1. Pre-requisites: driver with extension installed
  2. Open 'chrome-extension://UNIQUE_ID/popup/popup.html' (this may vary from extension to extension, see ref in part B below)
  3. Inspect elements for testing as req.

Code / Desc

(skip to B-C for the bit you're after)

A) Basics (imports, driver, install extension)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from os import path
path_core = os.path.join(os.path.expanduser("~"), 'Projects', 'Testing')
path_exec = os.path.join(path_core, "chromedriver.exe")
path_ext = os.path.join(path_core, 'extension.CRX')

Notes: path_core, path_exec, and path_ext are respective locations for: your code, chromedriver.exe file, and extension (crx for your extension)

o = Options()
o.add_extension(path_ext)
d = webdriver.Chrome(executable_path = path_exec, options = o)

Webpage with extension should be installed.

B) Open popup as a webpage

d.get('chrome-extension://UNIQUE_ID/popup/popup.html')

Ref/based upon: E. Shani - where UNIQUE_ID is specific to the extension, and popup/popup.html may vary from extension to extension too...

You can then test your extension by interacting with it in the same way you would an ordinary webpage (assuming this functionality 'features')

C) Interact

Inspecting elements is generally straightforward - as an e.g., I had a brief look at the element you were interested in and managed to find it with JQuery / console as follows:

document.querySelector('page-switch').shadowRoot.querySelector('main-index').shadowRoot.querySelector('c-switch')

Appreciate this doesn't exactly answer your Q, but do hopes it serves as a useful guide when it comes to your own testing/extensions!

Upvotes: 0

abielita
abielita

Reputation: 13469

You may refer with this documentation on how to start the Chrome Browser with Extensions.

1.Use a Custom Chrome Profile

  • Add the needed settings to the Selenium code:

Here:

public class TestClass {

WebDriver driver;


@Before
public void setUp() {

System.setProperty(“webdriver.chrome.driver”, “C:\\Selenium\\BrowserDrivers\\chromedriver.exe”);

ChromeOptions options = new ChromeOptions();

options.addArguments(“user-data-dir=C:\\Selenium\\BrowserProfile”);

options.addArguments(“–start-maximized”);

driver = new ChromeDriver(options);

}


@After
public void tearDown() {

driver.quit();

}


@Test
public void testScript() {

Thread.sleep(10000);

}
}

2.Load a Chrome Extension

  • Add the needed settings to the code:

Here:

public class TestClass {

WebDriver driver;


@Before
public void setUp() {

String pathToExtension = “C:\\Users\\home\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions\\mbopgmdnpcbohhpnfglgohlbhfongabi\\2.3.1_0”;

ChromeOptions options = new ChromeOptions();

options.addArguments(“–load-extension=” + pathToExtension);

driver = new ChromeDriver(options);

}


@After
public void tearDown() {

driver.quit();

}


@Test
public void testScript() {

Thread.sleep(10000);

}
}

Additional references:

Upvotes: 2

Related Questions