如何通过Selenium Script c#将参数传递给Chrome扩展? [英] How can I pass arguments to chrome extension through Selenium Script c#?

查看:1107
本文介绍了如何通过Selenium Script c#将参数传递给Chrome扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用名为Mod Header的Chrome扩展程序来修改每个请求的标题。为了具体我传递Dynatrace标题,以便我能够捕获我的Automated Selenium Tests的性能指标。为此,我想在请求标头中传递dynatrace标头。为此,我使用Chrome扩展程序。我成功地通过推出的Selenium浏览器启动了Mod HeaderExtension。发布这个我卡住了如何将参数传递给这个Extesnion,因为我想在头部请求中传递测试用例名称。
以下是我想传递的标题:

dynatrace:VU = 1; SN = Scriptname; TE = Testname; ID = 1; NA = TestLoad

有人可以帮我解决这个问题吗?

解决方案

Chrome将扩展的设置存储在本地存储。
因此,定制扩展的一种方法是首先在其上设置上下文,然后用一段Javascript编辑本地存储。



以下是添加示例两个标头(token1和token2)给ModHeader:

  //添加ModHeader扩展
var options = new ChromeOptions );
options.AddExtension(@C:\Downloads\ModHeader_v2.0.9.crx);

//启动浏览器
var driver = new ChromeDriver(options);

//在扩展中设置上下文,以便可以访问localStorage
driver.Navigate()。GoToUrl(chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png);
$ b $ //设置带有两个标头(token1和token2)的ModHeader
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:[]
}])););

//访问页面
driver.Navigate()。GoToUrl(http://stackoverflow.com/);


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/");

这篇关于如何通过Selenium Script c#将参数传递给Chrome扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆