Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically configure Chrome extension through Selenium WebDriver

I need to install and configure an extension in Chrome to modify all request headers during Selenium test execution. I've been able to follow an example from this support article in Saucelabs showing how to do this for Firefox locally, but not sure how to do so for Chrome.

The ChromeDriver documentation for extensions only goes into installing them, not configuring.

Questions

  • Can someone point me to some docs which explain how this can be accomplished or post an example here?
  • How would settings be updated?
  • How to find out what settings properties are available for any given extension?
  • Are there any differences between local and remote execution since that's one of the issues I've encountered with the Firefox method?

Plan is to run this against SauceLabs. Would try to use the ModHeader chrome extension to set the header values needed.

EDIT 1

Tried installing the Chrome version of the MODHeader extension, but running into similar problems. Able to get the extension installed locally, but in remote executions see an error.

private static IWebDriver GetRemoteDriver(string browser)
{

    ChromeOptions options = new ChromeOptions();
    options.AddExtensions("Tools/Chrome_ModHeader_2_0_6.crx");

    DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
    capabilities.SetCapability(ChromeOptions.Capability, options);


    capabilities.SetCapability("name", buildContext);
    capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");
    capabilities.SetCapability(CapabilityType.Version, "");
    capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
    capabilities.SetCapability("screen-resolution", "1280x1024");
    capabilities.SetCapability("username", "SaucelabsUserName");
    capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
    capabilities.SetCapability("build", "BuildNumber");
    capabilities.SetCapability("seleniumVersion", "2.50.1");


    return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}

Error displayed in SauceLabs logs is

[1.968][INFO]: RESPONSE InitSession unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: Arguments
like image 946
Jerry Avatar asked Jan 27 '16 01:01

Jerry


People also ask

Can we automate Chrome extensions?

But in order to automate actions on a browser extension, testers have to identify where the extension's pages are located. Then, they would have to switch their scope in the web UI to interact with the extension pages as DOM elements.

How do you code an extension in Chrome?

How to Create a Chrome Extension. First of all, we need to create an empty folder where we will add our HTML, CSS, and JavaScript files. Now, let's add a link to the Bootstrap CDN in the head tag. We will be using the Bootstrap framework here so that we don't have to write some extra CSS in this example.

How can we set the system property for ChromeDriver?

chrome. driver","path to chromedriver.exe"); // This basically sets the system property to value named webdriver. chrome. driver and the path is mentioned to get the chrome driver Webdriver driver = new ChromeDriver(); // This is basically used to create an instance of the chrome driver.


2 Answers

Since you mention that the problem is mainly on remote and I notice you are using SauceLabs, have you check this article from them?

https://support.saucelabs.com/customer/en/portal/articles/2200902-creating-custom-firefox-profiles-and-chrome-instances-for-your-automated-testing

Installing an Firefox Extension such as Modify Headers(You would need download the .xpi file on your machine first):

DesiredCapabilities caps = new DesiredCapabilities();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File("path\of\Modify Headers xpi file"));
profile.setPreference("general.useragent.override", "UA-STRING");
profile.setPreference("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
profile.setPreference("modifyheaders.headers.count", 1);
profile.setPreference("modifyheaders.headers.action0", "Add");
profile.setPreference("modifyheaders.headers.name0", "X-Forwarded-For");
profile.setPreference("modifyheaders.headers.value0", "161.76.79.1");
profile.setPreference("modifyheaders.headers.enabled0", true);
profile.setPreference("modifyheaders.config.active", true);
profile.setPreference("modifyheaders.config.alwaysOn", true);
profile.setPreference("modifyheaders.config.start", true);
caps.setCapability(FirefoxDriver.PROFILE, profile);

NOTE: If you trying to do the same using C#, you would need to use the ToBase64String() method.
like image 162
Rain9333 Avatar answered Oct 13 '22 08:10

Rain9333


    public void AddHeaderChrome()
    {
    ChromeOptions  options = new ChromeOptions();
    options.addExtensions(new File("C:\\Downloads\\ModHeader_v2.0.9.crx"));
     DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

    capabilities.setCapability(CapabilityType.options);
    // launch the browser
    WebDriver driver = new ChromeDriver(options);
    String HeadersName[]=new String[10];
    String HeadersValue[]=new String[10];;
    int length;
    if(ConfigDetails.HeadersName.contains(","))
    {
    HeadersName=ConfigDetails.HeadersName.split(",");
    HeadersValue=ConfigDetails.HeadersValue.split(",");
    length=HeadersName.length;
    }
    else
    {
       HeadersName[0]=ConfigDetails.HeadersName; 
       HeadersValue[0]=ConfigDetails.HeadersValue;
       length=1;
    }   
    int field_no=1;
    for(int i=0;i<length;i++)
    {
    driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/popup.html");
    driver.findElement(By.xpath("//input[@id='fl-input-"+field_no+"']")).sendKeys(HeadersName[i]);
    driver.findElement(By.xpath("//input[@id='fl-input-"+(field_no+1)+"']")).sendKeys(HeadersValue[i]);
    field_no+=2
    }
like image 44
Rohit Avatar answered Oct 13 '22 07:10

Rohit