我正在尝试为我的第一个登录测试实现 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
这是python路径:
Pythonpath:PYTHONPATH="${PYTHONPATH}:/usr/lib/python2.7/"
导出 PYTHONPATH
就我的第一次测试而言,很多代码都是复制粘贴的,也许它有问题,但我无法得到它。对关于这一点的任何建议都会非常满意。
下面也是我所谓的 PageObject 实现的结构和内容:
- 用户界面
- 根据
- __ 初始化 __ .py
- 基页.py
- 配置文件
- 包装器.py
- 页面
- __ 初始化 __ .py
- 登录页面.py
- 测试
- __ 初始化 __ .py
- test_login.py
- __ 初始化 __ .py
- 根据
ui/__ 初始化 __ .py:
__author__ = 'testuser'
ui/base/__ 初始化 __ .py:
from wrapper import SeleniumWrapper
from basePage import BasePage
selenium_driver = SeleniumWrapper()
ui/base/basePage.py:
class BasePage(object):
def __init__(self, driver):
self.driver = driver
def get_current_url(self):
return str(self.driver.current_url)
ui/base/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/base/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/__ 初始化 __ .py:
from loginPage import LoginPage
ui/pages/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/__ 初始化 __ .py:
__author__ = 'testuser'
ui/tests/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"])