Python + Appium 【已解决】driver(session)在多个class之间复用,执行完一个类的用例,再次执行下个类的用例时不需要初始化

时间:2018-01-13 21:00:03   收藏:0   阅读:1910

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文件中需要则如上方法所示进行调用即可。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!