Search code examples
pythonselenium-webdriverappiumbrowserstackpython-behave

HOOK-ERROR in before_scenario: WebDriverException: Message: Authorization required


So, hello everybody I'm trying to run some appium tests in browserstack. (I'm using behave with python)

So I have the following issue :

Part of the structure of the project:
-data
--credentials.yaml --parameters.yaml

-helpers
--helpers.py

Content of credentials.yaml:

browserstack:
  userName: "superduperusername"
  accessKey: "superduperpaccesskey"

Content of parameters.yaml

browserstack_android:
  platformName: android
  framework: behave
  app: bs://roadtoeldorado
  platforms:
    - platformName: android
      deviceName: Samsung Galaxy S22 Ultra
      platformVersion: 12.0
  browserstackLocal: true
  buildName: bstack-demo
  projectName: BrowserStack Sample
  buildIdentifier: ${BUILD_NUMBER}
  debug: true
  networkLogs: true

Content of helpers.py

import pprint

from appium import webdriver
import yaml


def create_driver(driver_type):
    # Read desired capabilities from parameters.yaml based on driver_type
    with open("data/parameters.yaml", "r") as parameters_file:
        parameters = yaml.safe_load(parameters_file)

    with open("data/credentials.yaml", "r") as credentials_file:
        credentials = yaml.safe_load(credentials_file)

    if driver_type == 'local_android_emulator':
        capabilities = parameters.get('local_android_emulator', {})
        # Create and return the local driver instance
        return webdriver.Remote('http://localhost:4723/wd/hub', capabilities)

    elif driver_type == 'browserstack_android':
        capabilities = parameters.get('browserstack_android', {})
        browserstack_config = credentials.get('browserstack', {})
        capabilities.update({
            **capabilities,
            'browserstack.user': browserstack_config['userName'],
            'browserstack.key': browserstack_config['accessKey'],
        })
        # Create and return the BrowserStack driver instance
        pp = pprint.PrettyPrinter()
        print("\n")
        pp.pprint(capabilities)
        return webdriver.Remote(
            command_executor="https://hub-cloud.browserstack.com/wd/hub",
            desired_capabilities=capabilities
        )

    # Add more cases for other driver types if needed

    else:
        raise ValueError(f"Unsupported driver type: {driver_type}")

The result from the print:

{'app': 'bs://roadtoeldorado',
 'browserstack.key': 'superduperkey',
 'browserstack.user': 'superduperusername',
 'browserstackLocal': True,
 'buildIdentifier': '${BUILD_NUMBER}',
 'buildName': 'bstack-demo',
 'debug': True,
 'framework': 'behave',
 'networkLogs': True,
 'platformName': 'android',
 'platforms': [{'deviceName': 'Samsung Galaxy S22 Ultra',
                'platformName': 'android',
                'platformVersion': 12.0}],
 'projectName': 'BrowserStack Sample'}

The result from the operation :
HOOK-ERROR in before_scenario: WebDriverException: Message: Authorization required

Contents of the enviroment.py

import os

from helpers.helpers import create_driver


def before_scenario(context, scenario):
    # Check for the environment variable to set the execution mode
    execution_mode = os.environ.get("EXECUTION_MODE", "local_android_emulator")
    context.execution_mode = execution_mode
    # Set up the driver based on execution mode
    if context.execution_mode == "local_android_emulator":
        context.driver = create_driver('local_android_emulator')
    elif context.execution_mode == "browserstack_android":
        context.driver = create_driver('browserstack_android')
    else:
        raise ValueError(f"Unsupported execution mode: {execution_mode}")
    # Add more modes as needed


def after_scenario(context, scenario):
    # Quit the driver after all scenarios have finished
    if hasattr(context, "driver") and context.driver:
        context.driver.quit()

Pretty much tried everything. Changing to JSON format, calling the JSON, but still nothing (same error). I'm thinking that probably I'm not passing properly the capabilities, because if I try to put the credentials directly to the url for authentication, I'm getting "missing deviceName."  So what am I doing wrong? :D


Solution

  • In your code, could you please change the hub URL to the below Basic Auth format:

    "https://YOUR_BSTACK_USERNAME:[email protected]/wd/hub"