AttributeError:“上下文”对象没有属性“ find_element_by_name”

时间:2018-07-26 11:15:21

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

我刚开始使用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)

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

谢谢。

1 个答案:

答案 0 :(得分:0)

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

要对此进行调试,我建议实现类似行为的BEHAVE_DEBUG_ON_ERROR标志,以在失败时闯入ipdb,然后在各个位置assert False窥视{{1}的值},看看它变成了context.driver对象的地方。