肖sir_高级讲师_多测师讲解po(版本1)

po模式

===================================================================

一、po基本介绍

1.1

(1)PO是Page Object的缩写

(2)业务流程与页面元素操作分离的模式,可以简单理解为每个页面下面都有一个配置class, 配置class就用来维护页面元素或操作方法

(3)提高测试用例的可维护性、可读取性

==============================================================

自动化测试框架分层如下:
config:存放项目配置文件、如框架中每个目录的绝对路径
data:存放测试数据、一般以ini文件类型存放
utils:主要是用于读取data目录下ini文件并获取内容
repot:自动化测试报告
run:存放运行的用例
testcase:存放所有用例

 ======================================================

案例基本格式:

 

 ========================================================

config包

配置文件的格式一般:.ini ,xlsx,conf  ,.csv.

新建两个文件:config.ini包   和 globalconfig.py文件

读取所有文件的绝对

新建一个ini文件,

 

 confing.ini文件

在跑自动化的过程中有一些参数:url,username,pwd,
[path] #这个是section
project_path=C:UsersAdministratorPycharmProjectshzpo #这个是option
[env]
url=http://discuz.e70w.com/
username=xlh
pwd=123456


-----------------------------------------------------------
创建globalconfig.py 文件

#定义所有项目包配置的路径
import os
#定义当前项目的路径
project_path = os.path.dirname(os.path.dirname(__file__))
# print (project_path)
#定义config的绝对路径
config = os.path.join(project_path,"config")
# print (config)
#定义data的绝对路径
data = os.path.join(project_path,'data')
#定义public的绝对路径
public = os.path.join(project_path,'public')
#定义report的绝对路径
report = os.path.join(project_path,'report')
#定义testcase的绝对路径
testcase = os.path.join(project_path,'testcase')
========================================================
创建ReadConfigIni.py文件

在文件中我们要导入configparse类,

from configparser import ConfigParser  #导入ConfigParser类
from config.globalconfig import config
import os

class ReadConfigIni: # 定义一个类

def __init__(self,filename): # 写一个构造函数
self.cf = ConfigParser() #创建ConfigParser类的对象
#cf是config的缩写模式模式创建confgigporser类的对象
        self.cf.read(filename)   #通过对象调用read()

def get_ini_data(self,section, option):
'''获取ini后缀结尾的文件内容'''
value = self.cf.get(section, option) #get中有个section 和option
return value

read_conf = ReadConfigIni(os.path.join(config,'config.ini')) #os是导入readcinfig.ini文件
print (read_conf.get_ini_data('path','project_path')) #调用第一个参数
print (read_conf.get_ini_data('env','url'))

==========================================

data :

新建一个data.ini文件,

或导入表格方式

(1)创建data.ini格式文件

 (2)创建excel表格

2.1 创建data包

2.2 新建data文件

 

 2.3 excel文件中内容

 ======================================================

元素包:baidu_driver

 =====================================================

reoot 包   

===================================================

run包

 =================================================

testcase包

==================================================

utis包

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:python37python.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/discuzcofing
C:/Users/Administrator/PycharmProjects/untitled1/discuzpublicpages
C:/Users/Administrator/PycharmProjects/untitled1/discuzpublicutils
C:/Users/Administrator/PycharmProjects/untitled1/discuz un
C:/Users/Administrator/PycharmProjects/untitled1/discuz estcase


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')

=====================================================

原文地址:https://www.cnblogs.com/xiaolehua/p/14162828.html