我正在尝试为我的第一次登录测试实现PageObject模式。运行它时,我收到以下错误:
>> py.test -v test_login.py
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
plugins: xdist
collected 0 items / 1 errors
==================================== ERRORS ====================================
____________________ ERROR collecting test_login_logout.py _____________________
test_login_logout.py:10: in <module>
> from ui.pages import LoginPage
../pages/__init__.py:1: in <module>
> from loginPage import LoginPage
../pages/loginPage.py:3: in <module>
> from base import BasePage
E ImportError: No module named base
这是pythonpath:
Pythonpath:PYTHONPATH =“$ {PYTHONPATH}:/ usr / lib / python2.7 /”
导出PYTHONPATH
就我的第一次测试而言,许多代码都是复制粘贴的,也许它有些问题但是我无法得到它。对这一点的任何建议都会非常满意。
下面是我所谓的PageObject实现的结构和内容:
ui / __ init __ .py :
__author__ = 'testuser'
ui / base / __ init __ .py :
from wrapper import SeleniumWrapper
from basePage import BasePage
selenium_driver = SeleniumWrapper()
UI /碱/ basePage.py :
class BasePage(object):
def __init__(self, driver):
self.driver = driver
def get_current_url(self):
return str(self.driver.current_url)
UI /碱/ configs.py :
import os
try:
os.environ["HOST"]
HOST = os.environ["HOST"]
except KeyError:
os.environ["HOST"] = 'http://www.website.com'
HOST = str(os.environ["HOST"])
PORT = ''
BASE_URL_US = '%s:%s/en/' % (HOST, PORT)
EMAIL = 'test.user@gmail.com'
PASSWORD = 'secret'
UI /碱/ wrapper.py :
from selenium import webdriver
import configs
class SeleniumWrapper:
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(SeleniumWrapper, cls).__new__(cls, *args, **kwargs)
return cls._instance
def connect(self, host=configs.BASE_URL_US):
self.driver = webdriver.Firefox()
self.base_url = host
return self.driver
ui / pages / __ init __ .py :
from loginPage import LoginPage
UI /页/ loginPage.py :
from base import BasePage
class LoginPage(object):
login_page_link = '.log-in>a'
email_field_locator = 'email'
password_field_locator = 'password'
login_button_locator = 'submit'
def __init__(self, driver, base_url):
self.driver = driver
self.driver.get(base_url)
def login_action(self, email, password):
login_page = self.driver.find_element_by_css_selector(self.login_page_link)
email_element = self.driver.find_element_by_id(self.email_field_locator)
password_element = self.driver.find_element_by_id(self.password_field_locator)
login_button = self.driver.find_element_by_id(self.login_button_locator)
login_page.click()
email_element.send_keys(email)
password_element.send_keys(password)
login_button.click()
ui / tests / __ init __ .py :
__author__ = 'testuser'
UI /测试/ test_login.py :
import sys
import os
import pytest
if __name__ == '__main__':
sys.path.append(os.path.dirname(__file__) + '/../')
from ui.base import selenium_driver
from ui.pages import LoginPage
from ui.base import configs
@pytest.mark.ui
class TestLoginLogout(object):
@classmethod
def setup_class(cls):
cls.verificationErrors = []
cls.driver = selenium_driver.connect()
cls.driver.implicitly_wait(10)
cls.base_url = selenium_driver.base_url
cls.email = configs.EMAIL
cls.password = configs.PASSWORD
@classmethod
def teardown_class(cls):
cls.driver.quit()
assert cls.verificationErrors == []
def test_login_positive(self):
welcome_page = LoginPage(self.driver, self.base_url)
login_page = welcome_page.login_action(self.email, self.password)
# assert 'property' in login_page.get_current_url()
if __name__ == '__main__':
pytest.main([__file__, "-s"])
答案 0 :(得分:4)
您的base
模块位于ui
模块中。因此行:
from base import BasePage
应该是:
from ui.base import BasePage
答案 1 :(得分:-1)
当这个错误触及我所做的事情时,我只是一个初学者 而不是:
from models.post import Post
我在安装post作为包后做了以下事情:
from post import Post
它对我有用
答案 2 :(得分:-2)
发生此错误时,我只是一个初学者。所以,我所做的是:
from models.post import Post