Vitamin
Vitamin

Reputation: 25

AttributeError: 'Context' object has no attribute 'browser'

I am currently experimenting with Behavioral Driven Development. I am using behave_django with selenium. I get the following output

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'...

Here is the code:

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)

I'm not sure what this error means

Upvotes: 2

Views: 14995

Answers (2)

Suprith
Suprith

Reputation: 61

In my case the browser wasn't installed. That can be a case too. Also ensure path to geckodriver is exposed if you are working with Firefox.

Upvotes: 0

fanny
fanny

Reputation: 1441

I know it is a late answer but maybe somebody is going to profit from it: you need to declare the context.browser (in a before_all/before_scenario/before_feature hook definition or just test method definition) before you use it, e.g.:

context.browser = webdriver.Chrome()

Please note that the hooks must be defined in a separate environment.py module

Upvotes: 4

Related Questions