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

时间:2017-01-25 08:15:42

标签: selenium automated-tests

我目前正在尝试行为驱动开发。我正在使用带有selenium的behave_django。我得到以下输出

Creating test database for alias 'default'...
Feature: Open website and print title # features/first_selenium.feature:1

Scenario: Open website                   # features/first_selenium.feature:2
Given I open seleniumframework website # features/steps/first_selenium.py:2 0.001s
  Traceback (most recent call last):
    File "/home/vagrant/newproject3/newproject3/venv/local/lib/python2.7/site-packages/behave/model.py", line 1456, in run
      match.run(runner.context)
    File "/home/vagrant/newproject3/newproject3/venv/local/lib/python2.7/site-packages/behave/model.py", line 1903, in run
      self.func(context, *args, **kwargs)
    File "features/steps/first_selenium.py", line 4, in step_impl
      context.browser.get("http://www.seleniumframework.com")
    File "/home/vagrant/newproject3/newproject3/venv/local/lib/python2.7/site-packages/behave/runner.py", line 214, in __getattr__
      raise AttributeError(msg)
  AttributeError: 'Context' object has no attribute 'browser'

Then I print the title                 # None


Failing scenarios:
features/first_selenium.feature:2  Open website

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
0 steps passed, 1 failed, 1 skipped, 0 undefined
Took 0m0.001s
Destroying test database for alias 'default'...

以下是代码:

first_selenium.feature

Feature: Open website and print title
    Scenario: Open website
      Given I open seleniumframework website
      Then I print the title

first_selenium.py

from behave import *
@given('I open seleniumframework website')
def step_impl(context):
   context.browser.get("http://www.seleniumframework.com")
@then('I print the title')
def step_impl(context):
   title = context.browser.title
   assert "Selenium" in title

manage.py

#!/home/vagrant/newproject3/newproject3/venv/bin/python

import os
import sys
sys.path.append("/home/vagrant/newproject3/newproject3/site/v2/features")

import dotenv


if __name__ == "__main__":
    path = os.path.realpath(os.path.dirname(__file__))
    dotenv.load_dotenv(os.path.join(path, '.env'))

    from configurations.management import execute_from_command_line
    #from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

我不确定这个错误意味着什么

2 个答案:

答案 0 :(得分:0)

我知道这是一个迟到的答案,但也许有人会从中获利: 你需要在使用它之前声明context.browser(在before_all / before_scenario / before_feature钩子定义或测试方法定义中),例如:

context.browser = webdriver.Chrome()

请注意,必须在单独的environment.py模块中定义挂钩

答案 1 :(得分:0)

在我的情况下,未安装浏览器。情况也可能如此。另外,如果您使用的是Firefox,请确保暴露出geckodriver的路径。