这应该通过 OOP 来完成。
您应该定义一个用于创建驱动程序实例的主类,并定义在其中创建一个驱动程序实例。然后从主类继承其他类。
假设我们称之为MainClass。然后我们应该自动化我们应用程序的两个部分,例如Register 和Login。它应该像这样实现:(Python)
MainClass.py
from appium import webdriver
class MainClass:
def __init__(self):
self.driver_instance = None
def open_application(self, udid=None):
caps = {
'platformName': 'Android', 'deviceName': Galaxy A7, 'automationName': 'UiAutomator2',
'skipServerInstallation': True, 'appActivity': 'YOUR_APP_START_ACTIVITY', 'noReset': no_reset,
'udid': udid, 'newCommandTimeout': 1200, 'autoGrantPermissions': True,
'appPackage': 'YOUR_APP_PACKAGE_NAME'}
driver = webdriver.Remote(str(appium_server), caps)
self.driver_instance = driver
return driver
MyApplication.py
from MainClass import MainClass
class MyApplication(MainClass)
def __init__(self):
super().__init__()
def open_my_application(udid=None):
self.open_application()
Authenticate.py
from MainClass import MainClass
class Authenticate(MainClass)
def __init__(self):
super().__init__()
def login(self, username, password):
self.driver_instance.find_element_by_xpath("//input[1]").set_text(username)
self.driver_instance.find_element_by_xpath("//input[2]").set_text(password)
self.driver_instance.find_element_by_xpath("//button[text='Submit']").click()
def register(self, username, password, name, phone):
self.driver_instance.find_element_by_xpath("//input[1]").set_text(username)
self.driver_instance.find_element_by_xpath("//input[2]").set_text(password)
self.driver_instance.find_element_by_xpath("//input[3]").set_text(name)
self.driver_instance.find_element_by_xpath("//input[4]").set_text(phone)
Test.py
import MyApplication
import Authenticate
MyApplication().open_my_application(udid="5c1425bd54c88")
Authenticate().login('user12', 'user12-password')
1- 如您在上面的示例中所见,首先我们创建MainClass 并包含创建驱动程序实例关键字,我们在这里称为open_application(),它将创建实例并将驱动程序实例变量存储为self.driver_instance 变量。
2- 然后我们创建另一个名为MyApplication 的类,它继承自MainClass。然后我们定义一个名为open_my_application() 的函数,它将从MainClass 调用open_application() 作为实例(类对象)。因此self.driver_instance 将存储为MainClass 的变量,您可以根据需要创建从MainClass 继承的任何类。
3- 我们创建一个名为Authenticate 的类来进行登录和注册。
4- Test.py 最后是一个示例测试文件。
希望这会有所帮助。