简体   繁体   中英

How can I pass arguments to chrome extension through Selenium Script c#?

I am using a Chrome Extension called "Mod Header" to modify the Header of Each Request. To be Specific I am passing Dynatrace Header so that I would be able to capture the Performance metric of my Automated Selenium Tests. For this I want to pass the dynatrace header in the Request header. For doing this I'm using Chrome Extension. I was successfully able to launch the "Mod Header" Extension with the launched browser of Selenium. Post this I got stuck on How to pass the Argument to this Extesnion as I want to pass the Test case name in the header request. Following is the Header I want to pass :
dynatrace:VU=1;SN=Scriptname;TE=Testname;ID=1;NA=TestLoad

Can someone please help me with this issue ?

Chrome stores the settings of an extension in the localstorage. So one way to customize your extension is to first set the context on it and then edit the localstorage with a piece of Javascript.

Here is an example adding two headers (token1 and token2) to ModHeader:

// add the ModHeader extension
var options = new ChromeOptions();
options.AddExtension(@"C:\Downloads\ModHeader_v2.0.9.crx");

// launch the browser
var driver = new ChromeDriver(options);

// set the context on the extension so the localStorage can be accessed
driver.Navigate().GoToUrl("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");

// setup ModHeader with two headers (token1 and token2)
driver.ExecuteScript(@"
    localStorage.setItem('profiles', JSON.stringify([{
      title: 'Selenium', hideComment: true, appendMode: '',
      headers: [
        {enabled: true, name: 'token1', value: '01234', comment: ''},
        {enabled: true, name: 'token2', value: '56789', comment: ''}
      ],
      respHeaders: [],
      filters: []
    }]));");

// visit a page
driver.Navigate().GoToUrl("http://stackoverflow.com/");

One way is using search parameters in your extension url.

If you invoke your extensions by invoking some of its pages, you can also add parameters like

chrome-extension://efjkpoicghgloioskoepnjiniemnhcnl/index.html?param1=value1&param2=value2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM