Appium+python自動化(四十)-Appium自動化測試框架綜合實踐 - 代碼實現(超詳解)

1.簡介

  今天咱們緊接着上一篇繼續分享Appium自動化測試框架綜合實踐 - 代碼實現。因爲時間的關係,宏哥這裏用代碼給小夥伴演示兩個模塊:註冊和登陸。web

2.業務模塊封裝

     由於如今各類APP的層出不羣,各式各樣的。可是其大多數都有註冊、登陸。爲了避免要使小夥伴卡在入門處,因此宏哥在這裏僅僅是給你打一個樣,俗話說:師傅領進門修行靠我的。剩下的就靠童鞋們本身實現和學習了app

2.1登陸頁面業務邏輯模塊

思路:框架

一、首先定義封裝一個登陸類的視圖dom

二、而後繼承公共類的定位方法,定位帳號和密碼的輸入框學習

三、在類中,定義登陸APP的方法測試

四、在類中,定義登陸APP檢查帳戶的方法編碼

五、在類中,定義檢查登陸APP登陸狀態的方法spa

2.1.1loginView.py

 2.1.2參考代碼

# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,通常放在第一行

# 2.註釋:包括記錄建立時間,建立人,項目名稱。
'''
Created on 2019-11-18
@author: 北京-宏哥   QQ交流羣:707699217
Project:Appium自動化測試框架綜合實踐 - 代碼實現
'''
# 3.導入模塊
import logging
from kyb_testProject.common.common_fun import Common,NoSuchElementException
from kyb_testProject.common.desired_caps import appium_desired
from selenium.webdriver.common.by import By

class LoginView(Common):
    username_type=(By.ID,'com.tal.kaoyan:id/login_email_edittext')
    password_type=(By.ID,'com.tal.kaoyan:id/login_password_edittext')
    loginBtn=(By.ID,'com.tal.kaoyan:id/login_login_btn')

    tip_commit=(By.ID,'com.tal.kaoyan:id/tip_commit')

    button_mysefl=(By.ID,'com.tal.kaoyan:id/mainactivity_button_mysefl')
    username=(By.ID,'com.tal.kaoyan:id/activity_usercenter_username')

    RightButton=(By.ID,'com.tal.kaoyan:id/myapptitle_RightButton_textview')
    logoutBtn=(By.ID,'com.tal.kaoyan:id/setting_logout_text')




    def login_action(self,username,password):
        self.check_cancelBtn()
        self.check_skipBtn()

        logging.info('============login_action==============')
        logging.info('username is:%s' %username)
        self.driver.find_element(*self.username_type).send_keys(username)

        logging.info('password is:%s'%password)
        self.driver.find_element(*self.password_type).send_keys(password)

        logging.info('click loginBtn')
        self.driver.find_element(*self.loginBtn).click()
        logging.info('login finished!')

    def check_account_alert(self):
        logging.info('=====check_account_alert====')
        try:
            element=self.driver.find_element(*self.tip_commit)
        except NoSuchElementException:
            pass
        else:
            logging.info('close tip_commit')
            element.click()

    def check_loginStatus(self):
        logging.info('====check_loginStatus======')
        self.check_market_ad()
        self.check_account_alert()

        try:

            self.driver.find_element(*self.button_mysefl).click()
            self.driver.find_element(*self.username)
        except NoSuchElementException:
            logging.error('login Fail!')
            self.getScreenShot('login fail')
            return False
        else:
            logging.info('login success!')
            self.logout_action()
            return True

    def logout_action(self):
        logging.info('=====logout_action======')
        self.driver.find_element(*self.RightButton).click()
        self.driver.find_element(*self.logoutBtn).click()
        self.driver.find_element(*self.tip_commit).click()



if __name__ == '__main__':
    driver=appium_desired()
    l=LoginView(driver)
    l.login_action('bjhg2019','bjhg2019')
    # l.login_action('bjhg2018','34454')
    l.check_loginStatus()

2.2註冊頁面業務邏輯封裝

思路:3d

一、首先定義封裝一個註冊類的視圖code

二、而後繼承公共類的定位方法,定位帳號和密碼等其餘的輸入框

三、在類中,定義註冊APP的註冊狀態的方法

四、在類中,定義添加註冊APP信息的方法

2.2.1registerView.py

 2.2.2參考代碼

# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,通常放在第一行

# 2.註釋:包括記錄建立時間,建立人,項目名稱。
'''
Created on 2019-11-18
@author: 北京-宏哥   QQ交流羣:707699217
Project:Appium自動化測試框架綜合實踐 - 代碼實現
'''
# 3.導入模塊
import logging,random
from kyb_testProject.common.desired_caps import appium_desired
from  kyb_testProject.common.common_fun import Common,By,NoSuchElementException


class RegisterView(Common):

    register_text=(By.ID,'com.tal.kaoyan:id/login_register_text')

    #頭像設置相關元素
    userheader=(By.ID,'com.tal.kaoyan:id/activity_register_userheader')
    item_image=(By.ID,'com.tal.kaoyan:id/item_image')
    save=(By.ID,'com.tal.kaoyan:id/save')

    #用戶名密碼郵箱相關元素
    register_username= (By.ID, 'com.tal.kaoyan:id/activity_register_username_edittext')
    register_password= (By.ID, 'com.tal.kaoyan:id/activity_register_password_edittext')
    register_email= (By.ID, 'com.tal.kaoyan:id/activity_register_email_edittext')
    register_btn= (By.ID, 'com.tal.kaoyan:id/activity_register_register_btn')

    #完善資料界面元素
    perfectinfomation_school = (By.ID, 'com.tal.kaoyan:id/perfectinfomation_edit_school_name')
    perfectinfomation_major = (By.ID, 'com.tal.kaoyan:id/activity_perfectinfomation_major')
    perfectinfomation_goBtn = (By.ID, 'com.tal.kaoyan:id/activity_perfectinfomation_goBtn')

    #院校相關元素
    forum_title = (By.ID, 'com.tal.kaoyan:id/more_forum_title')
    university = (By.ID, 'com.tal.kaoyan:id/university_search_item_name')

    #專業相關元素
    major_subject_title = (By.ID, 'com.tal.kaoyan:id/major_subject_title')
    major_group_title = (By.ID, 'com.tal.kaoyan:id/major_group_title')
    major_search_item_name = (By.ID, 'com.tal.kaoyan:id/major_search_item_name')

    #用戶中心相關元素
    button_mysefl = (By.ID, 'com.tal.kaoyan:id/mainactivity_button_mysefl')
    username = (By.ID, 'com.tal.kaoyan:id/activity_usercenter_username')


    def register_action(self,register_username,register_password,register_email):
        self.check_cancelBtn()
        self.check_skipBtn()

        logging.info('======register_action======')
        self.driver.find_element(*self.register_text).click()

        logging.info('set userhead')
        self.driver.find_element(*self.userheader).click()
        self.driver.find_elements(*self.item_image)[10].click()
        self.driver.find_element(*self.save).click()

        logging.info('username is %s'%register_username)
        self.driver.find_element(*self.register_username).send_keys(register_username)

        logging.info('password is %s' % register_password)
        self.driver.find_element(*self.register_password).send_keys(register_password)

        logging.info('email is %s' % register_email)
        self.driver.find_element(*self.register_email).send_keys(register_email)

        self.driver.find_element(*self.register_btn).click()

        try:
            self.driver.find_element(*self.perfectinfomation_school)
        except NoSuchElementException:
            logging.error('register fail !')
            self.getScreenShot('register fail')
            return False
        else:
            self.add_register_info()
            if self.check_register_status():
                return True
            else:
                return False

    def add_register_info(self):
        logging.info('======add_register_info=====')

        logging.info('select school...')
        self.driver.find_element(*self.perfectinfomation_school).click()
        self.find_elements(*self.forum_title)[1].click()
        self.find_elements(*self.university)[1].click()

        logging.info('select major...')
        self.driver.find_element(*self.perfectinfomation_major).click()
        self.driver.find_elements(*self.major_subject_title)[1].click()
        self.driver.find_elements(*self.major_group_title)[2].click()
        self.driver.find_elements(*self.major_search_item_name)[1].click()

        self.driver.find_element(*self.perfectinfomation_goBtn).click()

    def check_register_status(self):
        logging.info('=====check_register_status=====')
        self.check_market_ad()
        try:
            self.driver.find_element(*self.button_mysefl).click()
            self.driver.find_element(*self.username)
        except NoSuchElementException:
            logging.error('register fail!')
            self.getScreenShot('register fail')
            return False
        else:
            logging.info('register success!')
            return True


if __name__ == '__main__':
    driver=appium_desired()
    register=RegisterView(driver)

    username = 'bjhg2019' + 'fly' + str(random.randint(1000, 9000))
    password = 'bjhg2019' + str(random.randint(1000, 9000))
    email = 'bjhg' + str(random.randint(1000, 9000)) + '@163.com'

    register.register_action(username,password,email)

3.小結

    好了,今天因爲時間的關係就分享就到這裏,宏哥這裏着重實現了兩個業務模塊,其餘業務模塊你能夠照貓畫虎的就能夠輕易地實現了。感謝您耐心的閱讀!

相關文章
相關標籤/搜索