【问题标题】:AttributeError: 'Context' object has no attribute 'find_element_by_name'AttributeError:“上下文”对象没有属性“find_element_by_name”
【发布时间】:2019-01-03 08:55:29
【问题描述】:

我是使用 Appium 进行测试的新手,我正在使用页面对象模型结构使用 Behave 运行测试,遇到了一个我完全无法解决的问题。

在尝试运行我的行为脚本时,我遇到了以下错误:

$ behave --tags=@test1 features/login_page.feature 
Feature: Login Page # features/login_page.feature:1

  @test1
  Scenario: This will test that given a filled SUBJECT_ID_FIELD and PARTICIPATION_CODE field the LOGIN_BUTTON will be enabled.  # features/login_page.feature:5
    Given I navigate to the log in screen                                                                                       # features/steps/login_page.py:4 0.353s
      Traceback (most recent call last):
        File "/usr/local/lib/python2.7/site-packages/behave/model.py", line 1329, in run
          match.run(runner.context)
        File "/usr/local/lib/python2.7/site-packages/behave/matchers.py", line 98, in run
          self.func(context, *args, **kwargs)
        File "features/steps/login_page.py", line 6, in step_impl
          login_page = pages.Login(context)
        File "/Users/admin/Documents/iris-ios/features/pages/login.py", line 9, in __init__
          self.subject_id_field = self.driver.find_element_by_name(self.ELEMENT_NAME_SUBJECT_ID_FIELD)
        File "/usr/local/lib/python2.7/site-packages/behave/runner.py", line 321, in __getattr__
          raise AttributeError(msg)
      AttributeError: 'Context' object has no attribute 'find_element_by_name'

    When I enter valid details into the username field                                                                          # None
    And I enter valid details into the password field                                                                           # None
    Then I will verify the LOGIN_BUTTON is enabled                                                                              # None


Failing scenarios:
  features/login_page.feature:5  This will test that given a filled SUBJECT_ID_FIELD and PARTICIPATION_CODE field the LOGIN_BUTTON will be enabled.

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
0 steps passed, 1 failed, 0 skipped, 3 undefined
Took 0m0.353s

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

from appium import webdriver
import desired_capabilities


def before_scenario(context, scenario):
    desired_caps = desired_capabilities.get_desired_capabilities()
    context.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)

我的步骤文件如下所示:

import pages


@given(u'I navigate to the log in screen')
def step_impl(context):
    login_page = pages.Login(context.driver)
    login_page.enter_subject_id("HELLO WORLD")

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

class Login():
    ELEMENT_NAME_SUBJECT_ID_FIELD = "Subject_ID_field"
    ELEMENT_NAME_PARTICIPATION_CODE_FIELD =  "participation_code_field"
    ELEMENT_NAME_LOGIN_BUTTON = "login_button"

    def __init__(self, driver):
        self.driver = driver
        self.subject_id_field = self.driver.find_element_by_name(self.ELEMENT_NAME_SUBJECT_ID_FIELD)

    def enter_subject_id(self,text_to_enter):
        self.subject_id_field.send_keys(text_to_enter)

任何解决此问题的帮助将不胜感激。

谢谢。

【问题讨论】:

  • 您确定webdriver.Remote(...) 确实返回了WebDriver Remote 对象吗?我会检查您指定的 URL 地址——它看起来像是直接来自 docs

标签: python-2.7 appium pageobjects appium-ios python-behave


【解决方案1】:

AttributeError 指出方法self.driver.find_element_by_name(...) 不存在,因为self.driverContext 对象。您粘贴的代码看起来不错,但必须在某处设置行为的 Context 对象,而不是从 webdriver.Remote(...) 返回的实际 context.driver

为了调试这个,我建议实现类似行为的BEHAVE_DEBUG_ON_ERROR 标志以在失败时进入ipdb,然后在不同位置使用assert False 来查看context.driver 的值,看看它在哪里变成了Context 对象。

【讨论】:

    猜你喜欢
    • 2022-06-27
    • 2022-11-24
    • 2015-04-19
    • 2016-08-07
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多