1

我在 Behave Python Selenium 中编写了一个功能测试。当我运行测试时,它会抛出错误 ImportError No module named pages。我认为它在 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

我的代码如下:

特征\步骤\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)

功能\环境.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)

页面\searchpage.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

谢谢,里亚兹

4

1 回答 1

2

首选

在feature/steps下移动页面。添加初始化文件。然后导入就可以了

更难

更新 sys.path 以包含“页面”的位置。您可以从 environment.py 或 steps.py 本身执行此操作

繁琐

import imp
myPkg = imp.load_source('module.name', '/path/to/pages/file.py')

只是要知道

导出 PYTHONPATH=$PYTHONPATH:/abs/path/to/pages。不过不推荐。

插图

.
├── anotherpages
│   ├── __init__.py
│   └── third.py
├── features
│   ├── __init__.py
│   └── steps
│       ├── __init__.py
│       ├── pages
│       │   ├── first.py
│       │   └── __init__.py
│       └── steps.py
└── outpages
    ├── __init__.py
    └── second.py

$ cat features/steps/steps.py

#Preferred
from pages.first import inside_hello
inside_hello()

#Harder
import sys
import os
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
if not path in sys.path:
    sys.path.insert(1, path)
del path
from outpages.second import outside_hello
outside_hello()

#Fiddly
import imp
third = imp.load_source('third', '/path/to/anotherpages/third.py')
from third import another_hello
another_hello()

$ python 功能/步骤/steps.py

Hello from inside
Hello from outer world
Hello from another outer world

$ cd 功能/步骤/

~features/steps$ python steps.py
Hello from inside
Hello from outer world
Hello from another outer world
于 2015-10-11T23:27:57.167 回答