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

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

nic bject config com appium client lee session ted

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中實現如上方法的調用,則寫法如下:

# 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() self.device = Driver_Elements(driver) #對appium_config.py 文件的調用 def Setting_MyCertification2(self): sleep(3) self.device.find_element_by_name("iiii").click() self.device.back(1)

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

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