1

Python Behave にサンプルの BDD シナリオがあります。この機能を実行すると、次のエラーが表示されます。

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

なぜそれが不平を言っているのかわかりません。home_page.py は pages フォルダーにあり、pages は steps フォルダーにあり、steps フォルダーは features フォルダーにあります。pages フォルダーに init.py ファイルがあります。
home_page.py が見つからないと不平を言っているのはなぜですか?

私のコードは次のとおりです: 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 |

私のディレクトリ構造は次のとおりです。

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

完全なエラーは次のとおりです。

    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

この問題を解決するにはどうすればよいですか?

ありがとう、リアズ

4

1 に答える 1