数据驱动(1)
一、数据驱动:
安装yaml文件,打开cmd—>输入pip3 install pyyaml命令,回车
1、在base包下创建一个bases.py存放基础代码。代码如下:
from selenium.webdriver.common.by import By from selenium.webdriver.support.expected_conditions import NoSuchFrameException from selenium.webdriver.support.wait import WebDriverWait from selenium import webdriver import time as t class WebUI: def __str__(self): return 'driver' '''不管是单元素定位还是单元素定位参数都是*loc''' '''单个元素定位的方法''' def findElement(self,*loc): return self.driver.find_element(*loc) '''多个元素定位的方法''' def findsElements(self,*loc): return self.driver.find_elements(*loc) def findFrame(self, frameID): return self.driver.switch_to.frame(frameID) @property def getTitle(self): return self.driver.Title @property def getUrl(self): t.sleep(2) return self.driver.current_url
2、在data包下创建一个(1)sina.json或者(2)sina.yaml(二选一),代码如下:
(1)sina.json(在.json中只能使用双引号)
{ "login": { "notEmail": "请输入邮箱名", "formatEmail": "您输入的邮箱名格式不正确", "errorEmail": "登录名或密码错误", "username": "wuya1303@sina.com","password": "admin123", "textone": "test", "url": "https://m0.mail.sina.com.cn/classic/index.php#title=%25E9%2582%25AE%25E7%25AE%25B1%25E9%25A6%2596%25E9%25A1%25B5&action=mailinfo" } }
(2)sina.yaml
login: notEmail: 请输入邮箱名 formatEmail: 您输入的邮箱名格式不正确 errorEmail: 登录名或密码错误 username: wuya1303@sina.com password: admin123
3、在config包中创建一个config.yaml(将网页链接地址分离出来),代码如下:
url: qa: https://mail.sina.com.cn/
4、在page包中创建一个init.py,代码如下:
import unittest from selenium import webdriver class InitSina(unittest.TestCase): def setUp(self) -> None: #1、前置条件 self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.get(getUrl) self.driver.implicitly_wait(30)
def tesrDown(self) -> None:
self.driver.quit()
5、在utils包下创建 patUtils.py(获取当前的工程路径)和(1) jsonUtils.py或者(2)yamlUtils.py代码如下:
pathUtils.py
import os '''获取当前的工程路径''' def base_dir(): #os.path.dirname()去掉脚本的文件名,返回目录 return os.path.dirname(os.path.dirname(__file__)) def filePath(directory='data',fileName=None): '''找到具体的文件路径''' return os.path.join(base_dir(),directory,fileName)
(1) jsonUtils.py
import json from utils.pathUtils import base_dir,filePath import os def readJson(): return json.load(open(filePath(directory='data',fileName='sina.json'),encoding='utf-8')) print(readJson())
(2)yamlUtils.py
import yaml from utils.pathUtils import filePath def readYaml(): '''读取yaml文件里面的内容''' return yaml.load(open(filePath(fileName='sina.yaml'),encoding='utf-8')) def getUrl(): return yaml.load(open(filePath(directory='config',fileName='config.yaml'),encoding='utf-8'))['url']['qa'] print(getUrl())
6、最后在test包下创建一个(1)test_sina_login_json.py或者(2)test_sina_login_yaml.py
(1)test_sina_login_json.py
import unittest from page.init import InitSina from page.sina import Sina from selenium import webdriver from utils.jsonUtils import readJson import time as t class SinaTest(InitSina,Sina): def test_file_sina_001(self): '''登录验证:用户名密码为空的信息''' self.login(username='',password='') #获取错误提示信息 self.assertEqual(self.getpromptText,readJson()['login']['notEmail']) def test_file_sina_002(self): '''登录验证:邮箱名不规范''' self.login(username='@#%^',password='1245') # 获取错误提示信息 self.assertEqual(self.getpromptText,readJson()['login']['formatEmail']) def test_file_sina_003(self): '''登录验证:用户和密码都错误''' self.login(username='1563784603@sina.com',password='14526') # 获取错误提示信息 self.assertEqual(self.getpromptText,readJson()['login']['errorEmail']) def test_sina_004(self): '''登录验证:登录成功''' self.login( username=readJson()['login']['username'], password=readJson()['login']['password']) #断言 # self.assertEqual(self.getNick,readJson()['login']['username']) #断言1 # self.assertEqual(self.getNickOne,readJSon()['login']['textone']) # #断言2 self.assertEqual(self.getUrl,readJson()['login']['url']) if __name__ == '__main__': unittest.main(verbosity=2)
(2)test_sina_login_yaml.py
import unittest from page.init import InitSina from page.sina import Sina from selenium import webdriver from utils.jsonUtils import readJson from utils.yamlUtils import readYaml import time as t class SinaTest(InitSina,Sina): def test_file_sina_001(self): '''登录验证:用户名密码为空的信息''' self.login(username='',password='') #获取错误提示信息 self.assertEqual(self.getpromptText,readYaml()['login']['notEmail']) def test_file_sina_002(self): '''登录验证:邮箱名不规范''' self.login(username='@#%^',password='1245') # 获取错误提示信息 self.assertEqual(self.getpromptText,readYaml()['login']['formatEmail']) def test_file_sina_003(self): '''登录验证:用户和密码都错误''' self.login(username='1563784603@sina.com',password='14526') # 获取错误提示信息 self.assertEqual(self.getpromptText,readYaml()['login']['errorEmail']) def test_sina_004(self): '''登录验证:登录成功''' self.login( username=readJson()['login']['username'], password=readJson()['login']['password']) #断言 # self.assertEqual(self.getNick,readYaml()['login']['username']) #断言1 # self.assertEqual(self.getNickOne,readYaml()['login']['textone']) # #断言2 self.assertEqual(self.getUrl,readJson()['login']['url']) if __name__ == '__main__': unittest.main(verbosity=2)
处理错误情况:
错误提示:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 92: illegal multibyte sequence
解决办法:1、设置IDE的编码为UTF-8(在file->setting中搜索enc)
2、读取文件的时候,设置编码(在utils包下面的pathUtils中的open(encodings='utf-8'
注意事项:
在data包中的新建file,文件后缀名.json,在该文件中只能用双引号