Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BrowserStack: Unexpected error. Authorization required

I have two simple tests that are using RemoteWebDriver with ChromeOptions and EdgeOptions. Both these tests are using common code to set capabilities, including the browserstack.user and browserstack.key capabilities.

Because I am using DriverOptions (instead of DesiredCapabilities) I have used AddAdditionalCapability(...) to add these capabilities to the Driver.

The Edge test is working but the Chrome test is failing before the test even starts with;

OpenQA.Selenium.WebDriverException: Unexpected error. Authorization required

These tests were previously working with DesiredCapabalities before I upgraded my Selenium Driver to v3.14 (where DesiredCapabalities have been depracated).


Update

I have downgraded to Selenium.WebDriver v3.4.

An example of the code that is passing (EdgeOptions) and failing (with 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);
    }
}

I have the following packages installed:

<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>
like image 335
Mark Cooper Avatar asked Sep 06 '18 15:09

Mark Cooper


2 Answers

This seems an issue specific to how the selenium language bindings generate payload and how browserstack parses it at their end.

Based on the error message you shared, it is quite likely that while parsing the request payload, browserstack is not able to find your username and access key

You may follow the steps mentioned below to debug this:

  • Change the line driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub/"), options); to driver = new RemoteWebDriver( new Uri("http://localhost:4444/wd/hub/"), options );. You are not required to start selenium-standalone jar locally.

  • Start a proxy that reads traffic on localhost:4444. (You may use a node based implementation for the same if needed. Here is one such implementation: https://gist.github.com/hanikhan/f817bd64b063129cb78dc7ed0b66fdb7)

  • Observe the request payload generated by the selenium client bindings you are using(v3.14 as you mentioned). For example, my java based selenium client generates this when only browser is passed is desiredcapabitlies {"desiredCapabilities":{"browserName":"Chrome"},"capabilities":{"firstMatch":[{"browserName":"Chrome"}]}}

  • Now downgrade your selenium bindings(to a version where it was working) and observe the payload it generates.

Check if the client bindings use strict checks due to which some required capabilities are getting discarded at your end.

If this is true then you will be required to do one of the following:

  • Raise an issue with selenium C# bindings to remove strict checks for your case
  • Contact Browserstack and ask them to provide a capability that passes the strict check
like image 88
BountyHunter Avatar answered Oct 15 '22 08:10

BountyHunter


You can pass the capabilities as below for both Edge and Chrome using EdgeOptions and ChromeOptions to initiate session on BrowserStack. This is in Java. Port your test accordingly for other languages.

For 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);

For 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);
like image 4
Kireeti Annamaraj Avatar answered Oct 15 '22 08:10

Kireeti Annamaraj