参考:https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-Actual.html
目录
架构结构
config
|-config
|-|-init.py
|-|-conf.py
|-|-config.ini
conf.py
放的是一些配置信息,全局变量,比如根目录在哪儿
使用方法:
config.ini
放的是一些xpath的位置
- INI文件的格式很简单,最基本的三个要素是:parameters,sections和comments。
- INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value
- 所有的parameters都是以sections为单位结合在一起的
- 在INI文件中注释语句是以分号“;”开始的
使用方法:
# 用户名输入框
username = do_conf.get_locators_or_account('LoginPageElements', 'username')
# 密码输入框
password = do_conf.get_locators_or_account('LoginPageElements', 'password')
data
|-data
|-|-init.py
|-|-attachment
|-|-contact_data.py
|-|-login_data.py
|-|-send_mail_data.py
|-|-tcData.xlsx
attachmen
这是发信用的附件
login_data.py
这里存放的是测试数据,比如登录需要什么数据就写在这里
写法上用类的方法
调用方法:
login_data = LoginData
@pytest.mark.parametrize('username, password, expect', login_data.login_success_data)
class LoginData(object):
"""用户登录测试数据"""
login_success_data = [
(
"daido1",
"daido001",
"ログアウト"
)
]
login_fail_data = [
(
"daido1",
"daido11111111",
" 登录失败"
),
(
"daido111111111",
"daido001",
" 登录失败"
),
(
"linux",
"xiaochao",
" 登录失败"
)
]
![](https://img2020.cnblogs.com/blog/1951341/202009/1951341-20200914142450249-486594020.png)
if __name__ == '__main__':
pass
tcData.xlsx
Page
BasePage.py
存放一些共通方法
class BasePage(object):
"""结合显示等待封装一些selenium内置方法"""
def __init__(self, driver, timeout=30):
self.byDic = {
'id': By.ID,
'name': By.NAME,
'class_name': By.CLASS_NAME,
'xpath': By.XPATH,
'link_text': By.LINK_TEXT
}
self.driver = driver
self.outTime = timeout
def find_element(self, by, locator):
"""
find alone element
:param by: eg: id, name, xpath, css.....
:param locator: id, name, xpath for str
:return: element object
"""
def find_elements(self, by, locator):
"""
find group elements
:param by: eg: id, name, xpath, css.....
:param locator: eg: id, name, xpath for str
:return: elements object
"""
def is_element_exist(self, by, locator):
"""
assert element if exist
:param by: eg: id, name, xpath, css.....
:param locator: eg: id, name, xpath for str
:return: if element return True else return false
"""
def is_alert(self):
"""
assert alert if exsit
:return: alert obj
"""
def switch_to_frame(self, by, locator):
"""判断frame是否存在,存在就跳到frame"""
def switch_to_default_frame(self):
"""返回默认的frame"""
def get_alert_text(self):
"""获取alert的提示信息"""
def get_element_text(self, by, locator, name=None):
"""获取某一个元素的text信息"""
def load_url(self, url):
"""加载url"""
def get_source(self):
"""获取页面源码"""
def send_keys(self, by, locator, value=''):
"""写数据"""
def clear(self, by, locator):
"""清理数据"""
def click(self, by, locator):
"""点击某个元素"""
def sleep(num=0):
"""强制等待"""
def ctrl_v(self, value):
"""ctrl + V 粘贴"""
def enter_key():
"""enter 回车键"""
def wait_element_to_be_located(self, by, locator):
"""显示等待某个元素出现,且可见"""
def get_page_source(self):
return self.get_source()
LoginPage.py
存放页面的方法
class LoginPage(BasePage):
# 配置文件读取元素
do_conf = ParseConFile()
# 选择密码登录的按钮
password_login_btn = do_conf.get_locators_or_account('LoginPageElements', 'password_login_btn')
# 登录框外的iframe
frame = do_conf.get_locators_or_account('LoginPageElements', 'frame')
def login(self, username, password):
"""登录流程"""
self.open_url()
self.click_password_login_btn()
# self.switch_login_frame()
self.input_username(username)
self.input_password(password)
self.click_login_btn()
def open_url(self):
return self.load_url('http://daido.sitetest1.com/')
#return self.load_url('https://mail.126.com')
def click_password_login_btn(self):
return self.click(*LoginPage.password_login_btn)
def switch_login_frame(self):
return self.switch_to_frame(*LoginPage.frame)
def clear_username(self):
return self.clear(*LoginPage.username)
def input_username(self, username):
self.clear_username()
return self.send_keys(*LoginPage.username, username)
def clear_password(self):
return self.clear(*LoginPage.password)
def input_password(self, password):
self.clear_password()
return self.send_keys(*LoginPage.password, password)
def click_login_btn(self):
return self.click(*LoginPage.loginBtn)
def switch_default_frame(self):
return self.switch_to_default_frame()
def get_error_text(self):
return self.get_element_text(*LoginPage.error_head)
def get_login_success_account(self):
return self.get_element_text(*LoginPage.account)
if __name__ == "__main__":
pass
util
这里存放的是一些封装好的方法
TestCases
@pytest.mark.loginTest
class TestLogin(object):
"""登录"""
login_data = LoginData
@pytest.mark.parametrize('username, password, expect', login_data.login_success_data)
def test_login(self, open_url, username, password, expect):
login_page = open_url
login_page.login(username, password)
time.sleep(3)
actual = login_page.get_login_success_account()
assert expect in actual, "登录成功, 断言失败"
login_page.switch_default_frame()
@pytest.mark.parametrize('username, password, expect', login_data.login_fail_data)
def test_fail(self, open_url, username, password, expect):
login_page = open_url
login_page.login(username, password)
actual = login_page.get_error_text()
assert actual == expect, "登录失败, 断言失败"
if __name__ == "__main__":
pytest.main(['-v', 'test_loginCase.py'])