我需要在 Chrome 中安装和配置一个扩展程序,以在 Selenium 测试执行期间修改所有请求 header 。我已经能够遵循这个 support article 中的一个例子在 Saucelas 中展示了如何在本地为 Firefox 执行此操作,但不确定如何为 Chrome 执行此操作。
extensions 的 ChromeDriver 文档只进入安装它们,而不是配置。
问题
计划是针对 SauceLabs 运行它。会尝试使用 ModHeader chrome 扩展来设置所需的 header 值。
编辑 1
尝试安装 MODHeader 扩展的 Chrome 版本,但遇到了类似的问题。能够在本地安装扩展,但在远程执行中看到错误。
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);
}
SauceLabs 日志中显示的错误是
[1.968][INFO]: RESPONSE InitSession unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: Arguments
最佳答案
由于您提到问题主要出在远程并且我注意到您正在使用 SauceLabs,您是否从他们那里查看了这篇文章?
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.
关于google-chrome - 如何通过 Selenium WebDriver 以编程方式配置 Chrome 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35027354/