Behave Python Selenium で機能テストを作成しました。テストを実行すると、エラー ImportError No module named pages がスローされます。ページディレクトリにクラスが見つからないと思います。ホームページ.py、検索ページ.py
私のインポートは間違っていますか? この問題を解決するにはどうすればよいですか?
完全なエラーは次のとおりです。
> log
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_pat
self.load_step_definitions()
File "C:\Python27\lib\site-packages\behave\runner.py", line 658, in load_step_de
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\steps.py", line 5, in <module>
from pages import homepage
ImportError: No module named pages
私のコードは次のとおりです。
features\steps\steps.py
from behave import given, when, then
from pages import homepage
from pages import searchpage
@given ('we are on the homepage')
def step(context):
context.browser.get('http://localhost:8080/test')
@when ('we enter "{product}" in the search field')
def step(context, product):
home_page = homepage.HomePage(context)
home_page.enter_product_in_search_field(product, context)
@when ('And we click the search button')
def step(context, home_page):
searchPage_results = home_page.click_search_button(context)
@then ('the list of products are displayed')
def step(context, searchPage):
searchPage.search_products_results(context)
features\environment.py
import logging
from selenium import webdriver
def before_all(context):
selenium_logger = logging.getLogger(
'selenium.webdriver.remote.remote_connection')
selenium_logger.setLevel(logging.WARN)
context.browser = webdriver.Firefox()
context.browser.get('http://localhost:8080/test')
context.browser.implicitly_wait(3)
def after_all(context):
context.browser.quit()
ページ\ホームページ.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 searchpage 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.ID, 'search_field')
search_field.send_keys(product)
return self
def click_search_button(self, context):
search_button = context.find_element(By.ID, 'search_button_home_page').click()
return SearchPage(context)
ページ\検索ページ.py
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class SearchPage():
def __init__(self, context):
context = context
def search_products_results(self, context):
wait = WebDriverWait(context.browser, 60)
divs = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div/a/h2')))
for i in divs:
print i.text
return self
私のディレクトリ構造は次のとおりです。
E:\selenium_test\features\steps\steps.py
E:\selenium_test\features\environment.py
E:\selenium_test\features\search.feature
E:\selenium_test\features\__init__.py
E:\selenium_test\pages\homepage.py
E:\selenium_test\pages\searchpage.py
E:\selenium_test\pages\__init__.py
ありがとう、リアズ