HOOK-ERROR在before_scenario中: WebDriverException: 消息: 需要授权

huangapple go评论134阅读模式
英文:

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

问题

你好大家,我正在尝试在BrowserStack上运行一些Appium测试。(我使用Python的Behave框架)

我遇到了以下问题:

项目结构的一部分:

  • data
    -- credentials.yaml
    -- parameters.yaml

  • helpers
    -- helpers.py

credentials.yaml内容:

browserstack:
  userName: "superduperusername"
  accessKey: "superduperaccesskey"

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

helpers.py内容:

import pprint
from appium import webdriver
import yaml

def create_driver(driver_type):
    # 从parameters.yaml中读取所需的能力
    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', {})
        # 创建并返回本地驱动程序实例
        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'],
        })
        # 创建并返回BrowserStack驱动程序实例
        pp = pprint.PrettyPrinter()
        print("\n")
        pp.pprint(capabilities)
        return webdriver.Remote(
            command_executor="https://hub-cloud.browserstack.com/wd/hub",
            desired_capabilities=capabilities
        )

    else:
        raise ValueError(f"不支持的驱动程序类型:{driver_type}")

打印的结果:

{'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'}

操作的结果:
在before_scenario钩子中出现错误:WebDriverException: Message: 需要授权

enviroment.py的内容:

import os
from helpers.helpers import create_driver

def before_scenario(context, scenario):
    # 检查环境变量以设置执行模式
    execution_mode = os.environ.get("EXECUTION_MODE", "local_android_emulator")
    context.execution_mode = 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"不支持的执行模式:{execution_mode}")
    # 根据需要添加更多模式

def after_scenario(context, scenario):
    # 在所有场景完成后退出驱动程序
    if hasattr(context, "driver") and context.driver:
        context.driver.quit()

我几乎尝试了所有方法。尝试切换到JSON格式,调用JSON,但仍然没有解决问题(相同的错误)。我认为可能是没有正确传递能力,因为如果我尝试将凭据直接放在身份验证URL中,我会得到"missing deviceName"的错误。那么我做错了什么? 😄

英文:

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? HOOK-ERROR在before_scenario中: WebDriverException: 消息: 需要授权

答案1

得分: 0

请将您的代码中的hub URL更改为以下Basic Auth格式:

"https://YOUR_BSTACK_USERNAME:BSTACK_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"

英文:

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

"https://YOUR_BSTACK_USERNAME:BSTACK_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"

huangapple
  • 本文由 发表于 2023年8月9日 18:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定