po模式
=======================================================================
一、po基本介绍
1.1
(1)PO是Page Object的缩写
(2)业务流程与页面元素操作分离的模式,可以简单理解为每个页面下面都有一个配置class, 配置class就用来维护页面元素或操作方法
(3)提高测试用例的可维护性、可读取性
对比:传统的设计测试用例存在的弊端:
1.易读性差 2.复用性差 3.可维护性差 4.扩展性差
==============================================================
自动化测试框架分层如下:
config:存放项目配置文件、如框架中每个目录的绝对路径
data:存放测试数据、一般以ini文件类型存放
public:
①pages:存放项目中共有的函数和方法
②utils:主要是用于读取data目录下ini文件并获取内容
repot:自动化测试报告
run:存放运行的用例
testcase:存放所有用例
1、config/config.py:内容如下
'''
此模块配置框架中所有目录绝对路径
'''
import os
#项目绝对路径
project_path = os.path.dirname(os.path.dirname(__file__))
# print(project_path)
#config绝对路径
config_path = os.path.join(project_path,'cofing')
# print(config_path)
#data绝对路径
data_path = os.path.join(project_path,'data')
# print(data_path)
#pages绝对路径
pages_path = os.path.join(project_path,'public','pages')
# print(pages_path)
#utils绝对路径
utils_path = os.path.join(project_path,'public','utils')
# print(utils_path)
#repot绝对路径
repot_path = os.path.join(project_path,'repot')
# print(repot_path)
#run绝对路径
run_path = os.path.join(project_path,'run')
# print(run_path)
#testcase绝对路径
testcase_path = os.path.join(project_path,'testcase')
# print(testcase_path)
#打印结果:
C:\python37\python.exe C:/Users/Administrator/PycharmProjects/untitled1/discuz/config/globalconfig.py
C:/Users/Administrator/PycharmProjects/untitled1/discuz
C:/Users/Administrator/PycharmProjects/untitled1/discuz
C:/Users/Administrator/PycharmProjects/untitled1/discuz\cofing
C:/Users/Administrator/PycharmProjects/untitled1/discuz\public\pages
C:/Users/Administrator/PycharmProjects/untitled1/discuz\public\utils
C:/Users/Administrator/PycharmProjects/untitled1/discuz\run
C:/Users/Administrator/PycharmProjects/untitled1/discuz\testcase
2、在data目录下创建一个file文件取名为:test.ini
[test_data]
input01 = mysql
input02 = python
url = https://www.baidu.com
3、在public/utils目录下创建一个python文件取名为:read_test_ini
3.1、dos命令下在线安装configparser模块:pip3 install configparser
3.2、from configparser import ConfigParser
from configparser import ConfigParser
from config.config import *
import os
class Read_test_ini(ConfigParser):
def get_value(self,action,option):
value = self.get(action,option)
return value
if __name__ == '__main__':
p = Read_test_ini()
path = os.path.join(data_path,'test.ini')
p.read(path)
url = p.get_value('test_data','url')
input01 = p.get_value('test_data','input01')
input02 = p.get_value('test_data', 'input02')
4、在public/pages目录下创建一个python文件取名为:basepage
import unittest
from selenium import webdriver
driver = webdriver.Chrome()
class BasePage(unittest.TestCase):
@classmethod
def set_driver(cls,driver): #单例模式:作用于所有用例只打开一个浏览器
cls.driver = driver
@classmethod
def get_driver(cls):
return cls.driver
'''封装元素定位'''
@classmethod
def find_element(cls,*element):
type = element[0]
value = element[1]
if type == 'id':
elem = cls.driver.find_element_by_id(value)
elif type == 'name':
elem = cls.driver.find_element_by_name(value)
elif type == 'class':
elem = cls.driver.find_element_by_class_name(value)
elif type == 'xpath':
elem = cls.driver.find_element_by_xpath(value)
else:
raise ValueError('请传入正确的定位方式')
return elem
'''封装一个点击方法'''
@classmethod
def click_element(cls,elem):
return elem.click()
@classmethod
def send_keys(cls,elem,value):
return elem.send_keys(value)
@classmethod
def max_window(cls):
return cls.driver.maximize_window()
@classmethod
def wait(cls,time):
return cls.driver.implicitly_wait(time)
if __name__ == '__main__':
BasePage.set_driver(driver)
driver = BasePage.get_driver()
driver.get('https://www.baidu.com')
BasePage.max_window()
BasePage.wait(10)
elem = BasePage.find_element('id','kw')
BasePage.send_keys(elem,'多测师')
elem1 = BasePage.find_element('id','su')
BasePage.click_element(elem)
5、testcase
from public.pages.basepage import BasePage
from selenium import webdriver
from public.utils.read_test_ini import *
import unittest
from public.pages.baidu_element import Baidu_Element as t
class Baidu_test(BasePage):
@classmethod
def setUpClass(cls) -> None:
driver = webdriver.Chrome()
BasePage.set_driver(driver)
@classmethod
def tearDownClass(cls) -> None:
pass
def test_baidu01(self):
driver = BasePage.get_driver()
driver.get(url)
BasePage.max_window()
elem = BasePage.find_element(t.text[0],t.text[1])
BasePage.send_keys(elem,input01)
elem1 = BasePage.find_element(t.click[0],t.click[1])
BasePage.click_element(elem1)
def test_baidu02(self):
driver = BasePage.get_driver()
driver.get(url)
BasePage.max_window()
elem = BasePage.find_element(t.text[0],t.text[1])
BasePage.send_keys(elem,input02)
elem1 = BasePage.find_element(t.click[0],t.click[1])
BasePage.click_element(elem1)
if __name__ == '__main__':
unittest.main()
6、run
from public.pages.basepage import BasePage
from selenium import webdriver
from public.utils.read_test_ini import *
import unittest
from public.pages.baidu_element import Baidu_Element as t
class Baidu_test(BasePage):
@classmethod
def setUpClass(cls) -> None:
driver = webdriver.Chrome()
BasePage.set_driver(driver)
@classmethod
def tearDownClass(cls) -> None:
pass
def test_baidu01(self):
driver = BasePage.get_driver()
driver.get(url)
BasePage.max_window()
elem = BasePage.find_element(t.text[0],t.text[1])
BasePage.send_keys(elem,input01)
elem1 = BasePage.find_element(t.click[0],t.click[1])
BasePage.click_element(elem1)
def test_baidu02(self):
driver = BasePage.get_driver()
driver.get(url)
BasePage.max_window()
elem = BasePage.find_element(t.text[0],t.text[1])
BasePage.send_keys(elem,input02)
elem1 = BasePage.find_element(t.click[0],t.click[1])
BasePage.click_element(elem1)
if __name__ == '__main__':
unittest.main()
7、utils
from configparser import ConfigParser
from config.config import *
import os
class Read_test_ini(ConfigParser):
def get_value(self,action,option):
value = self.get(action,option)
return value
p = Read_test_ini()
path = os.path.join(data_path,'test.ini')
p.read(path)
url = p.get_value('test_data','url')
input01 = p.get_value('test_data','input01')
input02 = p.get_value('test_data', 'input02')