关于对自动化测试框架PO的认识详见之前我写的博客:http://www.cnblogs.com/hanxiaobei/p/6755329.html
本篇主要是说appium自动化测试如何有PO的设计思想来实现。
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 uit15 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的launcherActivity16 ‘appActivity‘: ‘com.smartisan.notes.NewNotesActivity‘,17 #如果存在activity之间的切换可以用这个18 # ‘appWaitActivity‘:‘.MainActivity‘,19 ‘unicodeKeyboard‘: True,20 #隐藏手机中的软键盘21 ‘resetKeyboard‘: True22 }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))
测试报告截图: