我有两个简单的测试,分别将RemoteWebDriver
和ChromeOptions
一起使用。这两个测试都使用通用代码设置功能,包括EdgeOptions
和browserstack.user
功能。
因为我使用的是browserstack.key
(而不是DriverOptions
),所以我已经使用DesiredCapabilities
将这些功能添加到了驱动程序中。
Edge测试正在运行,但Chrome测试甚至在测试开始之前就失败了;
OpenQA.Selenium.WebDriverException:意外错误。需要授权
在我将Selenium驱动程序升级到v3.14(其中AddAdditionalCapability(...)
已被取消使用)之前,这些测试以前与DesiredCapabalities
一起使用。
更新
我已降级为Selenium.WebDriver v3.4。
传递(EdgeOptions)和失败(使用ChromeOptions)的代码示例:
DesiredCapabalities
我安装了以下软件包:
[TestClass]
public class Simple_GridTest_Chrome
{
private static IWebDriver driver;
private string _bsUsername = "<username>";
private string _bsAccessKey = "<myaccesskey>";
private string _bsProjectName = "TestProject";
private string _bsBuildName = "Build-0.0.1";
private void SetOptions(bool useEdge = false)
{
DriverOptions options;
if (useEdge)
{
options = new EdgeOptions(); // this works OK
} else
{
options = new ChromeOptions(); // this fails with OpenQA.Selenium.WebDriverException: Unexpected error. Authorization required
}
// the account that is running the test
options.AddAdditionalCapability("browserstack.user", _bsUsername);
options.AddAdditionalCapability("browserstack.key", _bsAccessKey);
options.AddAdditionalCapability("project", _bsProjectName);
options.AddAdditionalCapability("build", _bsBuildName);
// gather additional data during the test run (screen shots etc)
options.AddAdditionalCapability("browserstack.debug", "true");
driver = new RemoteWebDriver(
new Uri("https://hub-cloud.browserstack.com/wd/hub/"), options
);
//driver = new RemoteWebDriver(
// new Uri($"https://{_bsUsername}:{_bsAccessKey}@hub-cloud.browserstack.com/wd/hub/"), options
//);
}
[ClassInitialize()]
public static void MyClassInitialise(TestContext context)
{
}
[TestMethod]
[TestCategory("grid.BrowserStack.Google")]
public void NavigateToGoogle_Windows7_Chrome()
{
SetOptions(false); // use Chrome
GoogleTest(driver);
}
[TestMethod]
[TestCategory("grid.BrowserStack.Google")]
public void NavigateToGoogle_Windows10_Edge()
{
SetOptions(true); // use Edge
GoogleTest(driver);
}
private void GoogleTest(IWebDriver driver)
{
driver.Navigate().GoToUrl("https://www.google.com/?q=test");
Console.WriteLine(driver.Title);
driver.WaitForWebElement(By.XPath("//*[@name=\"btnK\"]")).Click();
Console.WriteLine(driver.Title);
}
}
答案 0 :(得分:5)
这似乎是特定于硒语言绑定如何生成有效负载以及浏览器堆栈如何在其末尾解析有效负载的问题。
根据您共享的错误消息,很有可能在解析请求有效负载时,浏览器堆栈无法找到您的用户名和访问密钥
您可以按照下面提到的步骤进行调试:
将行<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Exit" />
</MenuItem>
<MenuItem Header="_Test" />
</Menu>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Header="Interfaces" HorizontalAlignment="Stretch" Margin="5">
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="0" Header="Messages" HorizontalAlignment="Stretch" Margin="5">
</GroupBox>
<GroupBox Grid.Row="2" Grid.Column="0" Header="Responses" HorizontalAlignment="Stretch" Margin="5">
</GroupBox>
</Grid>
<Grid Grid.Column="1">
<RichTextBox Name="txtLog" Margin="5">
<FlowDocument>
<Paragraph FontSize="12">Hello, world!</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Grid>
</DockPanel>
更改为
driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub/"), options);
。您不需要在本地启动独立硒罐。
启动一个代理,该代理读取localhost:4444上的流量。 (如果需要,您可以将基于节点的实现用于相同的实现。这是一个这样的实现:https://gist.github.com/hanikhan/f817bd64b063129cb78dc7ed0b66fdb7)
观察由您正在使用的硒客户端绑定生成的请求有效负载(如前所述,v3.14)。例如,当仅需要通过浏览器时,我基于Java的硒客户端会生成此功能driver = new RemoteWebDriver(
new Uri("http://localhost:4444/wd/hub/"), options
);
现在将您的硒绑定降级(降级到可以工作的版本),并观察其生成的有效载荷。
检查客户端绑定是否使用严格的检查,由于这些检查,一些必需的功能最终会被丢弃。
如果这是真的,那么您将需要执行以下操作之一:
答案 1 :(得分:4)
您可以使用EdgeOptions和ChromeOptions为Edge和Chrome传递以下功能,以在BrowserStack上启动会话。这是用Java编写的。相应地将测试移植到其他语言。
对于Edge
EdgeOptions options = new EdgeOptions();
options.setCapability("browserstack.user","<userName>");
options.setCapability("browserstack.key","<accessKey>");
options.setCapability("os_version", "10"); //desired os_version
options.setCapability("browser", "chrome"); //desired browser
driver = new RemoteWebDriver(new URL("https://hub-cloud.browserstack.com/wd/hub"), options);
对于Chrome
ChromeOptions options = new ChromeOptions();
options.setCapability("browserstack.user","<userName>");
options.setCapability("browserstack.key","<accessKey>");
options.setCapability("os_version", "10");
options.setCapability("browser", "chrome");
driver = new RemoteWebDriver(new URL("https://hub-cloud.browserstack.com/wd/hub"), options);
答案 2 :(得分:3)
您是否尝试将选项添加为options.ToCapabilities()
?
driver = new RemoteWebDriver(
new Uri("https://hub-cloud.browserstack.com/wd/hub/"), options.ToCapabilities()
);
也尝试设置为全局功能:
options.AddAdditionalCapability("browserstack.user", _bsUsername, true);
答案 3 :(得分:3)
问题在于,AddAdditionalCapability(string capabilityName, object capabilityValue)
在ChromeOptions
,FirefoxOptions
和InternetExplorerOptions
上调用时未全局设置功能。而是将它们放在JSON中的特定浏览器选项内。有关更多信息,请参见https://github.com/SeleniumHQ/selenium/issues/6563。
您已经注意到,EdgeOption
确实对它们进行了全局设置,这就是为什么它对您有用(SafariOptions
本来可以使用相同的BTW)。
现在,您没有看到AddAdditionalCapability(string capabilityName, object capabilityValue, bool isGlobalCapability)
重载的原因是您的options
变量的类型为DriverOptions
,其中不包含此重载。解决方法是,您可以执行以下操作:
static void AddGlobalCapability(this DriverOptions options, string name, object value)
{
switch (options)
{
case ChromeOptions chromeOptions:
chromeOptions.AddAdditionalCapability(name, value, true);
break;
case FirefoxOptions firefoxOptions:
firefoxOptions.AddAdditionalCapability(name, value, true);
break;
case InternetExplorerOptions internetExplorerOptions:
internetExplorerOptions.AddAdditionalCapability(name, value, true);
break;
default:
options.AddAdditionalCapability(name, value);
break;
}
}
答案 4 :(得分:0)
我遇到了同样的问题,并通过将ChromeOptions的每个“ AddAdditionalCapability”方法的“ isGlobalCapability”设置为true(使用Selenium 3.14)解决了该问题。如果只有其中之一没有设置,则测试将失败。
chromeOptions.AddAdditionalCapability("browserstack.user", <user>, true);
chromeOptions.AddAdditionalCapability("browserstack.key", <key>, true);
chromeOptions.AddAdditionalCapability("browser", "chrome", true);
chromeOptions.AddAdditionalCapability("os", "Windows", true);
chromeOptions.AddAdditionalCapability("os_version", "10", true);
_Driver = new RemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), chromeOptions);
答案 5 :(得分:-1)
我们遇到了同样的问题。就像在Java项目中一样,我们试图在URL中使用凭据。
var browserstackUrl = string.Format(
"https://{0}:{1}@hub-cloud.browserstack.com/wd/hub",
browserstackUsername,
browserstackAccessKey
);
var webdriver = new RemoteWebDriver(new Uri(BrowserStackUrl), options);
通过将它们转移到功能上,我们可以解决此问题:
capabilities.SetCapability("browserstack.user", browserstackUsername);
capabilities.SetCapability("browserstack.key", browserstackAccessKey);
var browserstackUrl = "https://hub-cloud.browserstack.com/wd/hub";
var webdriver = new RemoteWebDriver(new Uri(BrowserStackUrl), options);