Behave ImportError: No module named features.steps.pages.home_page

Riaz Ladhani

I have a sample BDD scenario in Python Behave. When i run the feature I get the error:

ImportError: No module named features.steps.pages.home_page

I am not sure why it is complaining. home_page.py is in the pages folder, pages is in the steps folder and steps folder is in the features folder. In pages folder I have an init.py file.
Why is it complaining it cannot find home_page.py?

My code is: features\steps.py

from behave import *
#from features.steps.pages import home_page
from features.steps.pages.home_page import HomePage
#from features.steps.pages import search_page
from features.steps.pages.search_page import SearchPage
from features.steps.pages import home_page

@Given ('we are on the homepage')
def step(context):
   context.browser.get('http://www.test.com')

@When ('we enter "{product}" in the search field')
def step(context, product):
   #search_field = context.browser.find_element(By.XPATH, 'id("twotabsearchtextbox")')
   #search_field.send_keys(product)
   home_page = HomePage(context)
   home_page.enter_product_in_search_field(product, context)

@When ('And we click the search button')
def step(context):
   #search_button = context.browser.find_element(By.XPATH, './/*[@id="nav-search"]/form/div[2]/div/input')
   searchPage_results = home_page.click_search_button(context)
   #search_button.click()

@Then ('the list of products are displayed')
def step(context):
   context.searchPage_results.search_products_results(context)
   #wait = WebDriverWait(context.browser, 60)
   #divs =  wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div/a/h2')))
   #for i in divs:
         #div2 = divs + '/a/h2'
         #print divs.get_attribute('value')
         #print divs
         #print i.text
         #print "i"
   #      divs

features\steps\pages\home_page.py

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from search_page import SearchPage

class HomePage(object):

    def __init__(self, context):
        context = context

    def enter_product_in_search_field(self, product, context):
        search_field = context.browser.find_element(By.XPATH, 'id("twotabsearchtextbox")')
        search_field.send_keys(product)
        return self

    def click_search_button(self, context):
        search_button = context.find_element(By.XPATH, './/*[@id="nav-search"]/form/div[2]/div/input').click()
        return SearchPage(context)

features\test_feature.feature

Feature: testing product

Scenario Outline: visit test and search for product
    Given we are on the test homepage
    When we enter "<product>" in the search field
    And we click the search button
    Then the list of products are displayed

    Examples: By product
        | Forumla One |
        | PS4 |
        | Headphones |

My directory structure is:

E:features\test_feature.feature
E:features\init.py
E:features\pages\init.py
E:features\pages\home_page.py
E:features\pages\search_page.py

The full error is:

    E:\RL Fusion\projects\BDD\Python BDD\PythonBDD\Selenium Sample\features>behave test_feature.feature
Exception ImportError: No module named features.steps.pages.home_page
Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\scripts\behave.exe\__main__.py", line 9, in <module>
  File "C:\Python27\lib\site-packages\behave\__main__.py", line 109, in main
    failed = runner.run()
  File "C:\Python27\lib\site-packages\behave\runner.py", line 672, in run
    return self.run_with_paths()
  File "C:\Python27\lib\site-packages\behave\runner.py", line 678, in run_with_paths
    self.load_step_definitions()
  File "C:\Python27\lib\site-packages\behave\runner.py", line 658, in load_step_definitions
    exec_file(os.path.join(path, name), step_module_globals)
  File "C:\Python27\lib\site-packages\behave\runner.py", line 304, in exec_file
    exec(code, globals, locals)
  File "steps\amazon_steps.py", line 6, in <module>
    from features.steps.pages.home_page import HomePage
ImportError: No module named features.steps.pages.home_page

How can I resolve this issue?

Thanks, Riaz

accraze

It looks like you are not importing your modules correctly. To turn a directory in to a module, change all your init.py files to __init__.py.

Then when you are importing in features/steps.py you can use:

from pages.home_page import HomePage

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related