如何初始化驱动程序对象,以便所有类都可以使用它

如何初始化驱动程序以便它可以被所有类使用

大家好,

我正在使用 Appium、Selenium 和 Cucumber 在 JAVA 中编写测试自动化框架。

我首先在我的一个测试步骤文件中声明一个 Appium 驱动程序,然后根据被测应用程序将其转换为 Android 驱动程序或 iOS 驱动程序。

我需要一些帮助 - 我需要我的所有类文件才能访问此驱动程序实例,但我不确定如何执行此操作。测试是从功能文件驱动的,并且一些测试步骤位于不同的类文件中,那么它们如何都可以访问该驱动程序实例?

谢谢马特


慕少森
浏览 123回答 3
3回答

鸿蒙传说

您可以将AppiumDriver定义为静态public class AppiumHelper(){&nbsp; &nbsp;public static AppiumDriver<MobileElement> driver;&nbsp; &nbsp;public void setupDriver(){&nbsp; &nbsp; &nbsp; &nbsp;//define your DesiredCapabilities&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//initialize your driver&nbsp; }然后你可以在你的测试方法中使用你的驱动程序,比如public void test1(){&nbsp; &nbsp; &nbsp; &nbsp;MobileElement element= AppiumHelper.driver.findElementById("elements id");}

qq_遁去的一_1

您可以在可以完成所有其他配置设置的类中创建一个初始化方法,然后您可以创建该类的一个实例来调用 getDriver 方法。例如:public class initialiseDriver{private static AppiumDriver<MobileElement> driver;public AppiumDriver<MobileElement> getDriver() throws IOException {if (PLATFORM_NAME.equals("Android")) {&nbsp; &nbsp; // setup the android driver} else if (PLATFORM_NAME.equals("iOS")) {&nbsp; &nbsp; // setup the ios driver}return driver;&nbsp; }}您可以在要使用驱动程序的地方调用此方法。理想情况下,您应该通过在@BeforeSuite/@BeforeClass 方法中调用此方法来初始化驱动程序,这样您就不需要每次启动脚本时都调用此方法,因为它会被@BeforeSuite/@BeforeClass 隐式调用。

慕娘9325324

Serenity&nbsp;PageObject类提供了一个内置的getDriver()方法,您可以在任何想要初始化驱动程序的地方调用该方法(最好在测试类中)。避免尝试在任何步骤定义/步骤库中初始化驱动程序(使用 @Managed 注释进行管理),否则它将抛出:空指针异常。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java