macaca 测试web(3)
上回书说到 macaca 测试web(2) 使用ddt做参数驱动化, 有些人会说,你好low,我说怎么low呢,他说你看看你的脚本就放在一个文件里,对于我们小白来说,这看起来很乱啊,能不能给我拆分,拆分,于是乎,你有需求,我有方案,你有你的需求,我有我的方案,我的方案会满足你的需求, 你是产品经理,我是测试,我来给你拆分,拆分,
那么我们先看看上节课的脚本,
import unittest from macaca import WebDriver import time import BSTestRunner import ddt login_data=[ {'username':'北漂的','password':'li1213456','id':'tip_btn','assert':'该用户不存在'}, {'username':'','password':'li123456','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'','password':'','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'','password':'li123456','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'北漂的雷子','password':'li123456','id':'tip_btn','assert':'用户名或密码错误'}, {'username':'北漂的雷子','password':'','id':'tip_input2','assert':'请输入密码'}, {'username':'北漂的雷子','password':'####','id':'feed_recent','assert':'最新动态'},] die_arp={ 'platformName':'Desktop', 'browserName':'electron' } server_url={ 'hostname':'localhost', 'post':3456 } @ddt.ddt class BokeyuanTest(unittest.TestCase): def setUp(self): self.deriver=WebDriver(die_arp,server_url) self.deriver.init() self.deriver.get('https://passport.cnblogs.com/user/signin') def tearDown(self): self.deriver.quit() @ddt.data(*login_data) def test_login(self,login_data): self.deriver.element_by_id('input1').send_keys(login_data['username']) self.deriver.element_by_id('input2').send_keys(login_data['password']) self.deriver.element_by_id('signin').click() time.sleep(1) self.assertTrue(self.deriver.element_by_id(login_data['id']).text,login_data['assert']) if __name__ == '__main__': suite = unittest.TestSuite() now = time.strftime('%Y-%m%d', time.localtime(time.time())) report_dir = r'%s.html' % now suite.addTests(unittest.TestLoader().loadTestsFromTestCase(BokeyuanTest)) re_open = open(report_dir, 'wb') runner = BSTestRunner.BSTestRunner(stream=re_open, title='demo by macaca', description='测试结果') runner.run(suite)
哪里能拆,哪里能拆,雷子你又骗我,我没有,来来,我给你分析分析,die_arp,server_url,login_data这个是不是参数的,那么我们可不可以对这个进行拆分呢,答案是肯定的,是可以的,那么来来,我们来看看,看看我的拆分思路,
创建一个新文件,我给他起名config.py
来来上config.py的代码
die_arp={ 'platformName':'Desktop', 'browserName':'electron' } server_url={ 'hostname':'localhost', 'post':3456 } login_data=[ {'username':'北漂的','password':'li1213456','id':'tip_btn','assert':'该用户不存在'}, {'username':'','password':'li123456','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'','password':'','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'','password':'li123456','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'北漂的雷子','password':'li123456','id':'tip_btn','assert':'用户名或密码错误'}, {'username':'北漂的雷子','password':'','id':'tip_input2','assert':'请输入密码'}, {'username':'北漂的雷子','password':'####','id':'feed_recent','assert':'最新动态'},]
哦哦,这样啊,那你来给我看看你的脚本
来来,脚本来了
from macaca import WebDriver import unittest import HTMLTestRunner,time import ddt from config import die_arp,server_url,login_data @ddt.ddt class BokeyuanTest(unittest.TestCase): def setUp(self): self.deriver=WebDriver(die_arp,server_url) self.deriver.init() self.deriver.get('https://passport.cnblogs.com/user/signin') def tearDown(self): self.deriver.quit() @ddt.data(*login_data) def test_login(self,login_data): self.deriver.element_by_id('input1').send_keys(login_data['username']) self.deriver.element_by_id('input2').send_keys(login_data['password']) self.deriver.element_by_id('signin').click() time.sleep(1) self.assertTrue(self.deriver.element_by_id(login_data['id']).text,login_data['assert']) if __name__ == '__main__': suite = unittest.TestSuite() now = time.strftime('%Y-%m%d', time.localtime(time.time())) report_dir = r'%s.html' % now suite.addTests(unittest.TestLoader().loadTestsFromTestCase(BokeyuanTest)) re_open = open(report_dir, 'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=re_open, title='demo by macaca', description='测试结果') runner.run(suite)
小伙伴书,我不信,你这么运行可以成功,来来,代码来了,小伙子,看好了,光说你不信,来来,run一下
oh,这成功了吗,不信,你给我看看你的测试报告
看到了吗,还信不信,满足不满足你的需求,不满足,不满足,那你要干啥,那不是还有可以提出来的吗,哪里,哪里告诉我,我看到了,我看到了,定位的,定位的。
oo 那么我们再来建一个新的文件,我们叫他page.py,上代码,这么简单
page_data={ 'username':'input1', 'password':'input2', 'login':'signin', }
那你来让我看看你的代码,来来,代码来了
from macaca import WebDriver import unittest import HTMLTestRunner,time import ddt from config import die_arp,server_url,login_data from page import page_data @ddt.ddt class BokeyuanTest(unittest.TestCase): def setUp(self): self.deriver=WebDriver(die_arp,server_url) self.deriver.init() self.deriver.get('https://passport.cnblogs.com/user/signin') def tearDown(self): self.deriver.quit() @ddt.data(*login_data) def test_login(self,login_data): self.deriver.element_by_id(page_data['username']).send_keys(login_data['username']) self.deriver.element_by_id(page_data['password']).send_keys(login_data['password']) self.deriver.element_by_id(page_data['login']).click() time.sleep(1) self.assertTrue(self.deriver.element_by_id(login_data['id']).text,login_data['assert']) if __name__ == '__main__': suite = unittest.TestSuite() now = time.strftime('%Y-%m%d', time.localtime(time.time())) report_dir = r'%s.html' % now suite.addTests(unittest.TestLoader().loadTestsFromTestCase(BokeyuanTest)) re_open = open(report_dir, 'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=re_open, title='demo by macaca', description='测试结果') runner.run(suite)
config.py保持不变
来来,小伙运行下:
我要看测试报告
小伙你弄的不错了,小伙,今天的需求到这里,到这里,你可以下班休息了。
oo,太让我开心了。、