Python + Appium 【已解決】driver(session)在多個class之間復用,執行完一個類的用例,再次執行下個類的用例時不需要初始化


實現效果:打開App進行自動化測試,只需打開APP一次,按先后順序執行n個py文件中的相應操作,實現自動化測試。

示例:如截圖示例,一個App,根據此APP內不同的模塊,寫成了不同的py文件,

   預期結果:實現打開App,按順序執行a、b、c 三個py文件進行自動化測試。如果不對driver進行封裝,則每次執行一個py文件都對App打開一次,這樣操作很麻煩,因此方法的封裝見下文。

 

對driver方法的封裝,py文件的名稱為:appium_config.py  中的寫法如下

# coding=UTF-8
'''
Created on 2017.1.13
@author: Lucky
'''
from appium import webdriver
from Test.logs.logs import logging    #本人自己封裝的方法,你們寫時可以不用調用,並且刪除方法中調用的logging即可 class Singleton(object):   
    driver = None 
    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            orig = super(Singleton, cls)
            logging.info('-----------------------init driver----------------------')
            config = {
                'platformName':'Android',
                'platformVersion':'4.4',
                'deviceName':'11111',
                'newCommandTimeout':30,
                'automationName':'Appium',
                'appPackage':'com.ibroker.iBerHK', 
                'appActivity' :'.SplashActivity'
                #'autoLaunch':'false'   #appium是否要自動啟動或安裝APP,默認為ture
                #'newCommandTimeout':'60'  #設置未接受到新命令的超時時間,默認60s,說明:如果60s內沒有接收到新命令,appium會自動斷開,
          如果我需要很長時間做driver之外的操作,可設置延長接收新命令的超時時間 #'unicodeKeyboard':True, #'resetKeyboard':True #'noReset':'false'
#在會話前是否重置APP狀態,默認是false } cls._instance = orig.__new__(cls, *args, **kw) cls._instance.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',config) return cls._instance class DriverClient(Singleton): def getDriver(self): logging.info('get driver') return self.driver

例如在py文件名稱為:Test_Driver_Elements.py中實現driver方法的調用,則寫法如下:

# coding=UTF-8
'''
Created on 2018.1.13
@author: Lucky
'''
from Test.Common.appium_config import DriverClient   #導入appium_config.py,此處為我自己的路徑,你們根據自己的路徑寫即可 from time import sleep

class test_Common: def __init__(self): driver = DriverClient().getDriver()   #對appium_config.py文件的調用 def Setting_MyCertification2(self): sleep(3) self.device.find_element_by_name("iiii").click() self.device.back(1)

若在其他的py文件中需要則如上方法所示進行調用即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM