appium - 将 Appium 驱动程序实例从一个类传递到另一个类
问题描述
我正在自动化一个android应用程序,并在设置类文件中有功能方法,并从该方法返回驱动程序实例。
我的一个页面有一个类文件,我在其中调用该功能方法并使用驱动程序实例来定位元素,尽管我无法在其他类文件中使用该驱动程序实例。这样做的最佳方法是什么?
解决方案
这应该使用 OOP 来完成。您应该定义一个用于创建驱动程序实例的主类,并定义在其中创建一个驱动程序实例。然后从主类继承其他类。假设我们调用它MainClass
。然后我们应该自动化我们应用程序的两个部分,例如Register
和Login
。它应该像这样实现:(Python)
主类.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
我的应用程序.py
from MainClass import MainClass
class MyApplication(MainClass)
def __init__(self):
super().__init__()
def open_my_application(udid=None):
self.open_application()
验证.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)
测试.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()
,它将调用open_application()
fromMainClass
作为实例(类对象)。因此,self.driver_instance
将存储为MainClass
's 变量,您可以根据需要创建任何继承自MainClass
的类。
3-我们创建一个名为Authenticate
登录和注册的类。
4- Test.py 最后是一个示例测试文件。
希望这可以帮助。
推荐阅读
- ansible - 是否有机会在 Ansible 剧本中的 Serial: 1 位置使用 Group_vars 变量(在“1”位置)
- javascript - 混合内容和不安全的网站图标
- android - 在 xamarin.forms (android) 上更改谷歌地图中的集群图标
- php - 为什么使用 exec 函数在 php 中从 avi 到 mp4 的视频转换不起作用?
- python - 我想将(非圆形)浮点数限制为 PYTHON 中的某个数字,
- database - 第一范式识别主键的具体步骤是什么
- hyperledger-fabric - 了解 Hyperledger Fabric 背书策略逻辑和对等节点类型
- c# - 为什么我的代码在项目中找不到路径?
- mysql - 仅将表数据从 MSSQL 迁移到 MySQL
- python - 如何使用终端运行 py 文件并写入文件