Appium基於Python APP自動化測試架構 -- PO

來源:互聯網
上載者:User

標籤:載入   runner   color   link   oid   rdo   登入   思想   orm   

關於對自動化測試架構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))

測試報告:

Appium基於Python APP自動化測試架構 -- PO

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.