一、先來看一下整體appium po的架構圖設計
二、我們先看PO文件中base_page類的實現:
#coding=utf-8 ''' po設計模式:page object 頁面對象 所有用到的頁面都定義成一個類,繼承自基礎的Page類 把頁面中用到的元素定義成方法 把頁面上一些操作定義成方法 ''' # 基礎類 用於所有頁面的繼承 class Action(object): #初始化 def __init__(self,driver): self.driver = driver #重新 def by_id(self,loc): try: return self.driver.find_element_by_id(loc) except Exception as e: print('未找到 {0}'.format(e)) def by_name(self, loc): try: return self.driver.find_element_by_name(loc) except Exception as e: print('未找到 {0}'.format(e)) def by_xpath(self, loc): try: return self.driver.find_element_by_xpath(loc) except Exception as e: print('未找到 {0}'.format(e))
我們對selenium的元素定位進行二次封裝設計,對id/xpath定位進行改寫。
三、我們對登錄頁面進行頁面對象設計
#coding=utf-8 ''' airen登陸頁面對象設計 ''' from Appium.PO import base_page import time class Airen_Login_Page(base_page.Action): add_button_my=("com.juyang.mall:id/rb_Mine") #點擊我的 clear_content=("com.juyang.mall:id/edit_Tel") #清空 input_username=("com.juyang.mall:id/edit_Tel") #輸入賬戶 input_password=("com.juyang.mall:id/edit_Pwd") #輸入密碼 click_login_button=("com.juyang.mall:id/tv_Login") #點擊登陸按鈕 login_result_page_text=(u"商城") #驗證返回商城首頁 def click_my_button(self): '''封裝點擊我的方法''' self.by_id(self.add_button_my).click() time.sleep(5) def clear_login_content(self): '''封裝情空方法''' self.by_id(self.clear_content).clear() def input_text_username(self,username): '''封裝輸入用戶名方法''' self.by_id(self.input_username).send_keys(username) time.sleep(3) def input_text_password(self,password): '''封裝輸入密碼方法''' self.by_id(self.input_password).send_keys(password) def click_loginbtn(self): '''封裝點擊登陸方法''' self.by_id(self.click_login_button).click() time.sleep(5) def get_finish_button_text(self): '''登陸成功后獲取首頁商城信息''' return self.by_name(self.login_result_page_text).text def login_airen_appproject(self,user,passwd): '''登陸流程封裝''' self.click_my_button() self.clear_login_content() self.input_text_username(user) self.input_text_password(passwd) self.click_loginbtn() # def add_button_link(self): # self.find_element(self.add_button_loc).click() # time.sleep(3) #等待3秒,等待登錄彈窗加載完成 # # # def run_case(self,value): # self.find_element(self.edittext_loc).send_keys(value) # time.sleep(5) # self.find_element(self.finish_button_loc).click() # time.sleep(2) # # # def get_finish_button_text(self): # return self.find_element(self.edittext_loc).text # # driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) #鏈接app # sleep(4) # driver.find_element(By.ID,'com.juyang.mall:id/rb_Mine').click() #點擊我的 # sleep(4) # driver.find_element(By.ID,'com.juyang.mall:id/edit_Tel').clear() # driver.find_element(By.ID,'com.juyang.mall:id/edit_Tel').send_keys("18665100958") # driver.find_element(By.ID,'com.juyang.mall:id/edit_Pwd').send_keys("123456") # driver.find_element(By.ID,"com.juyang.mall:id/tv_Login").click() # sleep(8)
1.把所有用到的元素都定義成一個方法。
2.每一個操作步驟都封裝為一個方法。
四、testCase文件中測試用例的實現如下:
#coding=utf-8 from Appium.pages.loginpage import Airen_Login_Page from Appium.PO.base_page import Action import unittest import time from appium import webdriver class TestAirenLogin(unittest.TestCase): def setUp(self): desired_caps = {'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '3.8.3.1', 'appPackage': 'com.juyang.mall', 'appActivity': 'com.shanjian.juyang.activity.home.Activity_Home' } desired_caps["unicodeKeyboard"] = "True" #使用unicode編碼方式發送字符串 desired_caps["resetKeyboard"] = "True" #將鍵盤隱藏起來 self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # self.driver.wait_activity('.activity.other.Activity_In', 10) # 等待app首頁出現 self.verificationErrors = u'商城' def tearDown(self): time.sleep(4) self.driver.close_app() def test_aiRenLogin(self): '''PO設計登陸功能實戰''' aiRen = Airen_Login_Page(self.driver) aiRen.login_airen_appproject('18665100958','123456') #斷言:實際結果 預期結果 錯誤信息 self.assertEqual(self.verificationErrors,aiRen.get_finish_button_text(),msg=u'驗證失敗!') if __name__ == '__main__': unittest.main()
對整個登錄模塊進行測試用例編寫。
五、總執行文件去調用測試用例,並輸出測試報告
#coding=utf-8 import unitTests,os,time from Public import HTMLTestRunner cur_path = os.path.dirname(os.path.realpath(__file__)) case_path = os.path.join(cur_path,'test_Cases') report_path = os.path.join(cur_path,'Result') if __name__ == '__main__': testlist = unitTests.defaultTestLoader.discover(case_path, pattern='test*.py') now_time = time.strftime('%Y_%m_%d_%H_%M_%S') report_name = report_path + '\\' + now_time + 'result.html' fp = HTMLTestRunner.HTMLTestRunner(stream=open(report_name,'wb'), title='appium-po設計模式自動化測試報告', description='window7 夜神模擬器艾人針灸') fp.run(testlist)