|
基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告
代码示例:
复制代码
- 1 #利用unittest并生成测试报告
- 2 class Appium_test(unittest.TestCase):
- 3 """appium测试类"""
- 4 def setUp(self):
- 5 desired_caps = {
- 6 'platformName': 'Android',
- 7 'deviceName': 'Android Emulator',#可有可无,这里是指我的模拟器
- 8 'platformVersion': '5.0',
- 9 # apk包名
- 10 'appPackage': 'com.smartisan.notes',
- 11 # apk的launcherActivity
- 12 'appActivity': 'com.smartisan.notes.NewNotesActivity',
- 13 #如果存在activity之间的切换可以用这个
- 14 # 'appWaitActivity':'.MainActivity',
- 15 'unicodeKeyboard': True,
- 16 #隐藏手机中的软键盘
- 17 'resetKeyboard': True
- 18 }
- 19 self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
- 20 time.sleep(5)
- 21 self.verificationErrors = "今天天气不错在家学习!" #设置的断言
- 22
- 23 def tearDown(self):
- 24 time.sleep(10)
- 25 assertt = self.driver.find_element_by_id("com.smartisan.notes:id/list_rtf_view").text
- 26 # print(assertt) #调试用
- 27 self.assertEqual(assertt,self.verificationErrors,msg="验证失败!")
- 28 #断言:实际结果,预期结果,错误信息
- 29 self.driver.quit()
- 30
- 31 def test_creat(self):
- 32 """记事本中新增一条记录"""
- 33 self.driver.find_element_by_id("com.smartisan.notes:id/add_button").click()
- 34 time.sleep(3)
- 35 self.driver.find_element_by_class_name("android.widget.EditText").send_keys("今天天气不错在家学习!")
- 36 self.driver.find_element_by_id("com.smartisan.notes:id/send_finish_button").click()
- 37
- 38 suite = unittest.TestSuite()
- 39 suite.addTest(Appium_test('test_creat'))
- 40
- 41 report_file = ".\\appium_report.html"
- 42 fp = open(report_file,'wb')
- 43 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="appium测试报告",description='新增一条笔记并保存')
- 44 runner.run(suite)
- 45 fp.close()
复制代码
复制代码
生成测试报告:
Appium自动化测试PO模型:
其中,main.py为框架的主入口,test_creat.py调用creat_page.py,creat_page.py调用base_page.py。
PO代码示例:
main.py
复制代码
- 1 import unittest
- 2 import HTMLTestRunner
- 3
- 4 #相对路径
- 5 testcase_path = ".\\testcase"
- 6 report_path = ".\\report\\appium_report.html"
- 7 def creat_suite():
- 8 uit = unittest.TestSuite()
- 9 discover = unittest.defaultTestLoader.discover(testcase_path,pattern="test_*.py")
- 10 for test_suite in discover:
- 11 # print(test_suite)
- 12 for test_case in test_suite:
- 13 uit.addTest(test_case)
- 14 return uit
- 15
- 16 suite = creat_suite()
- 17 fp = open(report_path,"wb")
- 18 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="测试结果",description="appium新建笔记测试结果")
- 19 runner.run(suite)
- 20 fp.close()
复制代码
复制代码
test_creat.py
复制代码
- 1 from appium import webdriver
- 2 import unittest
- 3 from appiumframework.PO.creat_page import CreatPage
- 4 import time
- 5
- 6 class Test(unittest.TestCase):
- 7 """自动化"""
- 8 def setUp(self):
- 9 desired_caps = {
- 10 'platformName': 'Android',
- 11 'deviceName': 'Android Emulator',#可有可无
- 12 'platformVersion': '5.0',
- 13 # apk包名
- 14 'appPackage': 'com.smartisan.notes',
- 15 # apk的launcherActivity
- 16 'appActivity': 'com.smartisan.notes.NewNotesActivity',
- 17 #如果存在activity之间的切换可以用这个
- 18 # 'appWaitActivity':'.MainActivity',
- 19 'unicodeKeyboard': True,
- 20 #隐藏手机中的软键盘
- 21 'resetKeyboard': True
- 22 }
- 23 self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
- 24 time.sleep(5)
- 25 self.verificationErrors = "今天天气不错在家学习!"
- 26
- 27 def tearDown(self):
- 28 time.sleep(10)
- 29 self.driver.quit()
- 30
- 31 def test_saveedittext(self):
- 32 """保存编辑的文本"""
- 33 sp = CreatPage(self.driver)
- 34 sp.add_button_link()
- 35 sp.run_case("今天天气不错在家学习!")
- 36 #断言:实际结果,预期结果,错误信息
- 37 self.assertEqual(sp.get_finish_button_text(),self.verificationErrors,msg="验证失败!")
复制代码
复制代码
creat_page.py
复制代码
- 1 from appiumframework.PO import base_page
- 2 import time
- 3
- 4 class CreatPage(base_page.Action):
- 5 add_button_loc = ("com.smartisan.notes:id/add_button")
- 6 edittext_loc = ("com.smartisan.notes:id/list_rtf_view")
- 7 finish_button_loc = ("com.smartisan.notes:id/send_finish_button")
- 8
- 9 def add_button_link(self):
- 10 self.find_element(self.add_button_loc).click()
- 11 time.sleep(3) #等待3秒,等待登录弹窗加载完成
- 12
- 13 def run_case(self,value):
- 14 self.find_element(self.edittext_loc).send_keys(value)
- 15 time.sleep(5)
- 16 self.find_element(self.finish_button_loc).click()
- 17 time.sleep(2)
- 18
- 19 def get_finish_button_text(self):
- 20 return self.find_element(self.edittext_loc).text
复制代码
复制代码
base_page.py
复制代码
- 1 class Action(object):
- 2 #初始化
- 3 def __init__(self,se_driver):
- 4 self.driver = se_driver
- 5
- 6 #重写元素定位的方法
- 7 def find_element(self,loc):
- 8 try:
- 9 return self.driver.find_element_by_id(loc)
- 10 except Exception as e:
- 11 print("未找到%s"%(self,loc))
复制代码
复制代码
测试报告截图:
|
|