我有两个使用RemoteWebDriver
withChromeOptions
和的简单测试EdgeOptions
。这两个测试都使用通用代码来设置功能,包括browserstack.user
和browserstack.key
功能。
因为我使用的是DriverOptions
(而不是DesiredCapabilities
),所以我习惯AddAdditionalCapability(...)
将这些功能添加到驱动程序中。
Edge 测试正常,但 Chrome 测试在测试开始之前就失败了;
OpenQA.Selenium.WebDriverException:意外错误。需要授权
DesiredCapabalities
在我将 Selenium 驱动程序升级到 v3.14(DesiredCapabalities
已弃用)之前,这些测试以前可以使用。
更新
我已降级到 Selenium.WebDriver v3.4。
通过 (EdgeOptions) 和失败 (使用 ChromeOptions) 的代码示例:
[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);
}
}
我安装了以下软件包:
<packages>
<package id="Selenium.Firefox.WebDriver" version="0.21.0" targetFramework="net45" />
<package id="Selenium.Support" version="3.4.0" targetFramework="net45" />
<package id="Selenium.WebDriver" version="3.4.0" targetFramework="net45" />
<package id="Selenium.WebDriver.ChromeDriver" version="2.41.0" targetFramework="net45" />
<package id="Selenium.WebDriver.IEDriver" version="3.14.0" targetFramework="net45" />
</packages>