【问题标题】:Pass Appium driver instance from one class to another将 Appium 驱动程序实例从一个类传递到另一个类
【发布时间】:2021-12-23 01:09:03
【问题描述】:

我正在自动化一个 android 应用程序,并在设置类文件中有功能方法并从该方法返回驱动程序实例。

我的一个页面有一个类文件,我在其中调用该功能方法并使用驱动程序实例来定位元素,尽管我无法在其他类文件中使用该驱动程序实例。最好的方法是什么?

【问题讨论】:

  • 我已经使用 Mohammad 提供的解决方案实现了它,这个问题现在可以结束了。

标签: appium appium-android


【解决方案1】:

这应该通过 OOP 来完成。 您应该定义一个用于创建驱动程序实例的主类,并定义在其中创建一个驱动程序实例。然后从主类继承其他类。 假设我们称之为MainClass。然后我们应该自动化我们应用程序的两个部分,例如RegisterLogin。它应该像这样实现:(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 最后是一个示例测试文件。

希望这会有所帮助。

【讨论】:

  • 感谢 Mohammad,它会有所帮助,我会尝试以这种方式实现它。虽然stackoverflow不允许我支持这个答案。
【解决方案2】:

为对象处理创建一个合适的测试框架,更新起来非常简单。完整的框架请参考这个视频-https://youtu.be/cHOs-CmY-M8

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    相关资源
    最近更新 更多