隐藏

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

发布:2023/10/22 21:45:21作者:管理员 来源:本站 浏览次数:201

实现效果:打开App进行自动化测试,只需打开APP一次,按先后顺序执行n个py文件中的相应操作,实现自动化测试。


示例:如截图示例,一个App,根据此APP内不同的模块,写成了不同的py文件,


预期结果:实现打开App,按顺序执行a、b、c 三个py文件进行自动化测试。如果不对driver进行封装,则每次执行一个py文件都对App打开一次,这样操作很麻烦,因此方法的封装见下文。




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


对driver方法的封装,py文件的名称为:appium_config.py  中的写法如下


# coding=UTF-8

'''

Created on 2017.1.13

@author: Lucky

'''

from appium import webdriver

from#本人自己封装的方法,你们写时可以不用调用,并且删除方法中调用的logging即可


class Singleton(object):  

   driver = None

   def*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= 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

'''

fromimport DriverClient   #导入appium_config.py,此处为我自己的路径,你们根据自己的路径写即可

from time import sleep


classdef __init__(self):

       driver =#对appium_config.py文件的调用def Setting_MyCertification2(self):

       sleep(3)

       self.device.find_element_by_name("iiii").click()

       self.device.back(1)