When there are multiple classes in appium+python, it is not necessary to initialize the driver solution every time

Keywords: Python Attribute Android Mobile

When appium+python writes automated test cases, it is very common to divide them into different classes for the sake of code maintainability,

But at runtime, each class needs to be initialized once, that is to say, it will restart the application every time. This is very troublesome__ new__ Multiple classes can be reused.

driver_configure.py
# coding:utf-8
__author__ = '**'
'''
De scription:driver to configure
'''
from appium import webdriver
from config.globalparameter import appPackage_name, deviceName

class Driver_configure(object):
def new(cls, args, kw):
"""
Use singleton mode to set the type to run with only one instance,
When using base classes in other python classes,
Multiple objects can be created to ensure that all objects are based on one browse
:param args:
:param kw:
:return:
The hasattr() function is used to detect whether an object contains an attribute named,
Return True if any, False if no
"""
if not hasattr(cls, '_instance'):
orig = super(Driver_configure, cls)
desired_caps = {}
desired_caps ['platformName'] = 'Android' -- platform
desired_caps ['platform version'] = '5.1.1' ා platform version
Wei self.desired_caps ['app'] = '1.apk' -- points to the. apk file. If appPackage and appActivity are set, this item will be ignored
desired_caps[‘appPackage’] = appPackage_ Name ා APK package name
# cls.desired_caps['appActivity'] = '.uitivity'
#Activity when the tested program starts. 'activity name, which can be found through inspection of appium'
desired_caps['appActivity'] = '.ui*ivity'
# self.desired_caps['appActivity'] = 'comding_Activity'
desired_caps ['unicode keyboard'] = 'true' ා whether unicode keyboard is supported. If you need to input Chinese, set to "true"
desired_caps ['resetKeyboard'] = 'true' ා whether to reset the keyboard as the default input method of the system after the test.
desired_caps ['newCommandTimeout'] = '120' -- the time when the appium server waits for the appium client to send a new message. Default to 60 seconds timeout
desired_caps['deviceName'] = deviceName
#Mobile ID (available for ADB devices)
desired_caps ['noReset'] = true ා true: do not reinstall app, false: reinstall app
#noReset means that each time you start the APP, you do not clear the previous status. For example, when you restart the session after login, you do not need to log in again and keep the previous login status
# cls.desired_caps['autoGrantPermissions'] = True
# self.desired_caps['noSign'] = True
#Remote control, can be set through appium; if it is a real machine, fill in directly http://localhost:4723/wd/hub or http://127.0.0.1:4723/wd/hub
cls._instance = orig.new(cls)
cls._instance.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
return cls._instance

class DriverClinet(Driver_configure):

</span><span style="color:#0000ff;">def</span><span style="color:#000000;"> get_driver(self):
    </span><span style="color:#0000ff;">return</span> self.driver</pre>

Call get_ in other.py files driver()

test_sysystem.py

from src.pages import login_page
from src.common import driver_configure, gesture_operator
import unittest
import time
import warnings
import os
import allure
from src.pages import module_page
from config.globalparameter import login_name, login_password

@ allure.MASTER_HELPER.feature("app automation test") (define function
class test_0_appium(unittest.TestCase):
@classmethod
def setUpClass(cls):
#Start appium
warnings.simplefilter("ignore", ResourceWarning)
os.system('start /b appium -a 127.0.0.1 -p 4723')
time.sleep(50) wait for appium to start
cls.driver = driver_configure.DriverClinet().get_driver()
cls.GM = gesture_ operator.Gesture_ Maincalculation() (gesture)

@classmethod
def tearDownClass(cls):
pass

<span style="color:#008000;">#</span><span style="color:#008000; "> login</span>
@allure.MASTER_HELPER.story(<span style="color:#800000;">'</span><span style="color:#800000;">Login test</span><span style="color:#800000;">'</span><span style="color:#000000;">)
@allure.MASTER_HELPER.severity(</span><span style="color:#800000;">'</span><span style="color:#800000;">blocker</span><span style="color:#800000;">'</span><span style="color:#000000;">)
</span><span style="color:#0000ff;">def</span><span style="color:#000000;"> test_login(self):
    </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">Sign in!</span><span style="color:#800000;">"</span><span style="color:#000000;">)
    time.sleep(</span>1<span style="color:#000000;">)
    self.login_page </span>=<span style="color:#000000;"> login_page.login_page(self.driver)
    self.login_page.input_user(login_name)
    self.login_page.input_Pws(login_password)
    self.login_page.click_btnLogin()
    time.sleep(</span>5<span style="color:#000000;">)

class test_1_appium(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = driver_configure.DriverClinet().get_driver()

@classmethod
</span><span style="color:#0000ff;">def</span><span style="color:#000000;"> tearDownClass(cls):
    driver_configure.DriverClinet().get_driver().quit()
    </span><span style="color:#008000;">#</span><span style="color:#008000;"> os.system("start /b stopAppiumServer.bat")</span>
    os.system(<span style="color:#800000;">"</span><span style="color:#800000;">start /b taskkill /F /IM node.exe</span><span style="color:#800000;">"</span><span style="color:#000000;">)

@allure.MASTER_HELPER.story(</span><span style="color:#800000;">'</span><span style="color:#800000;">Switching module test</span><span style="color:#800000;">'</span><span style="color:#000000;">)
@allure.MASTER_HELPER.severity(</span><span style="color:#800000;">'</span><span style="color:#800000;">Minor</span><span style="color:#800000;">'</span><span style="color:#000000;">)
</span><span style="color:#0000ff;">def</span><span style="color:#000000;"> test_module(self):
    </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">123!</span><span style="color:#800000;">"</span><span style="color:#000000;">)
    </span><span style="color:#008000;">#</span><span style="color:#008000;"> module_change.test_appium1()</span>
    self.module_page =<span style="color:#000000;"> module_page.Module_page(self.driver)
    self.module_page.click_btn_sy()
    self.module_page.click_side_goods()

if name == 'main':
unittest.main()

Run successfully:

 

Reproduced in: https://www.cnblogs.com/may18/p/10728844.html

Posted by WorldBizEduComputerShops on Sun, 07 Jun 2020 02:35:36 -0700