讲解:ui自动化框架设计
第一步:新建一个项目
第二步:需要建包:
config:存放项目配置文件、如框架中每个目录的绝对路径 (放配置文件)
data:存放测试数据、一般以ini文件类型存放 (可以新建excel表格存数据))
public: (公共方法)
①pages:存放项目中共有的函数和方法 (封装页面的公共方法)
②utils:主要是用于读取data目录下ini文件并获取内容(封装所有的工具类和函数)
repot:自动化测试报告 (存放报告)
run:存放运行的用例 (运行测试用例)
testcase:存放所有用例 (存放所有的测试用例)=============================================================
config包
(1)右键新建一个ini文件 ,命名config.ini(为什么建ini文件?可以存放我们的项目路径)
在config中还要配置什么嗯?我们要配置所有包全局的绝对路径
(2)在新建一个globalconfig的.py;
在文件中配置包的所有绝对路径
2.1第一个路径:#当前项目的路径:project_path = os.path.dirname(os.path.dirname(__file__))
2.2第二个路径:
路径是在项目路径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)
以上路径,我们拼接下就可以;
案例展示如下图:
1、confing格式
2、config.ini中的内容
3、项目中所有包的绝对路径和项目路径
============================================================
data包
案例如下图:
=============================================================
public包
1、新建一个public包
2、在public目录下在新创建两个python包:pages包和utils包
3、在utils下取名为:readconfigini.py 文件
3.1、dos命令下在线安装configparser模块:pip3 install configparser
python当中有一个模块configparser是用来处理ini文件
============================================================
==========================================================
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')