AttributeError: 'Context' 对象没有属性 'app'

时间:2020-12-29 16:31:01

标签: python appium bdd qa appium-android

您好,我没有找到类似问题的答案,所以我添加了新主题。 我对使用页面对象模型的 bdd + appium 有问题。当我运行我的脚本时,我遇到了问题:

Traceback (most recent call last):
  File "/home/mimy/.local/lib/python3.8/site-packages/behave/model.py", line 1329, in run
    match.run(runner.context)
  File "/home/mimy/.local/lib/python3.8/site-packages/behave/matchers.py", line 98, in run
    self.func(context, *args, **kwargs)
  File "features/steps/allow_to_app_steps.py", line 6, in tap_allow_when_using_app
    context.app.launch_page.tap_allow_button()
  File "/home/mimy/.local/lib/python3.8/site-packages/behave/runner.py", line 321, in __getattr__
    raise AttributeError(msg)
AttributeError: 'Context' object has no attribute 'app'

我的 environment.py 文件如下所示:

from appium import webdriver
from app.application import Application


def before_scenario(context, scenario):
    desired_capabilities = {
        "platformName": "Android",
        "platformVersion": "10",
        "deviceName": "Pixel 2 XL",
        "appPackage": "com.xxx.xxx",
        "appActivity": ".ui.MainActivity",
        "automationName": "UiAutomator2"

    }

    context.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities=desired_capabilities)
    context.driver.implicitly_wait(5)
    context.app = Application(context.driver)


def after_scenario(context, scenario):
    context.driver.quit()

我的步骤文件如下所示:

from behave import given, when, then, step


@given('I click on the "Allow only while using the app" button')
def tap_allow_when_using_app(context):
    context.app.launch_page.tap_allow_button()



@when('I click on the "Allow" button')
def tap_allow(context):
    context.app.launch_page.tap_allow()

我的页面对象模型的页面文件如下所示:

###LunchPage###

from selenium.webdriver.common.by import By
from pages.base_page import Page


class LaunchPage(Page):
    dialog_title = (By.XPATH, "//android.widget.TextView[contains(@text,'Allow QSpot to access this device')]")
    allow_only_while_using_the_app = (By.XPATH, "//android.widget.Button[@text='Allow only while using the app']")
    allow = (By.XPATH, "//android.widget.Button[@text='Allow']")

    def tap_allow_button(self):
        self.click(*self.allow_only_while_using_the_app)

    def tap_allow(self):
        self.click(*self.allow)

###BasePage###
class Page:

    def __init__(self, driver):
        self.driver = driver

    def find_element(self, *locator):
        return self.driver.find_element(*locator)

    def click(self, *locator):
        e = self.find_element(*locator)
        e.click()

和类应用

from pages.launch_page import LaunchPage


class Application:

    def __init__(self, driver):
        self.launch_page = LaunchPage(driver)

我现在这个问题可能与“驱动程序未启动”有关,但我无法修复它。 非常感谢您的帮助!

1 个答案:

答案 0 :(得分:-1)

就我而言,我错过了架构名称中的名称。 所以在从“环境”更改为“环境”(缺少N)之后。 它活过来了。

相关问题