Appium基於PO模型的自動化測試(Python)

基於python單元測試框架unittest完成appium自動化測試,生成基於html可視化測試報告html

代碼示例:python

 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()

生成測試報告:android

Appium自動化測試PO模型:web

其中,main.py爲框架的主入口,test_creat.py調用creat_page.py,creat_page.py調用base_page.py。app

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.pyui

 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))

測試報告截圖:

相關文章
相關標籤/搜索