1

在我的团队中,我们正在使用 Appium 和 Appium Java-Client 进行跨平台 UI 测试。我们项目的当前结构类似于:

mobile
   pages
     SignInPage
   steps
     SignInSteps

使用黄瓜将这些步骤“粘合”在一起。SignInPage 看起来像这样:

public class SignInPage {

    public SignInPage(AppiumDriver driver) {
        PageFactory.initElements(new AppiumFieldDecorator(driver, 15, TimeUnit.SECONDS), this);
    }

    // region Identifiers
    final String IOS_USERNAME_FIELD = "SignInUsernameField";
    final String ANDROID_USERNAME_FIELD = "new UiSelector().resourceIdMatches(\".*id/username.*\")";
    final String IOS_PASSWORD_FIELD = "SignInPasswordField";
    final String ANDROID_PASSWORD_FIELD = "new UiSelector().resourceIdMatches(\".*id/password_editText.*\")";
    final String IOS_SIGN_IN_BUTTON = "SignInButton";
    final String ANDROID_SIGN_IN_BUTTON = "new UiSelector().resourceIdMatches(\".*id/signInButton.*\")";
    // endregion

    @iOSFindBy(accessibility = IOS_USERNAME_FIELD)
    @AndroidFindBy(uiAutomator = ANDROID_USERNAME_FIELD)
    private MobileElement usernameField;

    @iOSFindBy(accessibility = IOS_PASSWORD_FIELD)
    @AndroidFindBy(uiAutomator = ANDROID_PASSWORD_FIELD)
    private MobileElement passwordField;

    @iOSFindBy(accessibility = IOS_SIGN_IN_BUTTON)
    @AndroidFindBy(uiAutomator = ANDROID_SIGN_IN_BUTTON)
    private MobileElement signInButton;

    public MobileElement getUsernameField() {
        return usernameField;
    }

    public MobileElement getPasswordField() {
        return passwordField;
    }

    public MobileElement getSignInButton() {
        return signInButton;
    }

    public void tapUsernameField() {
        getUsernameField().click();
    }

    public void tapSignInButton() {
        getSignInButton().click();
    }

    public void clearUsernameEditText() {
        getUsernameField().clear();
    }
}

在性能和元素查找方面,我们不确定在哪里创建 SignInPage 的实例最好。目前,我们在 SignInSteps 中有一个 @Before 方法,该方法在每个 Gherkin 场景开始之前执行(这并不理想),但它有助于我们在 SignInSteps 类中拥有一个可被所有步骤重用的 SignInPage 属性。

public class SignInSteps {

    private SignInPage signInPage;
    AppiumDriver driver;

    @Before()
    public void setUp() throws MalformedURLException {
        driver = TestBase.getInstance().getDriver();
        signInPage = new SignInPage(driver);
    }

    @Given("I fill in the username and password")
    public void fill_username_and_password() throws Throwable {
        signInPage.tapUsernameField();
        signInPage.clearUsernameEditText();
        fillEditText(signInPage.getUsernameField(), PropertiesManager.getInstance().getValueForKey(Constants.SIGN_IN_USERNAME));
        fillEditText(signInPage.getPasswordField(), PropertiesManager.getInstance().getValueForKey(Constants.SIGN_IN_PASSWORD));
    }
  // Other sign in steps below
}

但是我觉得更简洁的方法是在 SignInSteps 的每个步骤方法中创建 SignInPage 作为局部变量。在每个步骤中创建您需要的页面是否会对性能产生影响?

另外,我不清楚,使用我们当前的方法(@Before 方法)为什么即使您为稍后将执行的某些步骤创建一个页面,它也能准确地工作(因此此时屏幕甚至不可见) .

所以也许更大的问题是如何查找元素?是不是在调用 PageFactory.initElements(new AppiumFieldDecorator(driver, 15, TimeUnit.SECONDS), this); 或者在实际访问带注释的属性时(这将是某种惰性初始化方法,据我所知 Java 没有,除非我对 Java 注释的理解是错误的)。

抱歉发了这么长的帖子,但这些是我想彻底理解的一些事情。因此,非常感谢任何帮助。

谢谢!

4

1 回答 1

1

我做了一些更多的研究(调试),我找到了答案:

当您PageFactory.initElements(new AppiumFieldDecorator(driver, 15, TimeUnit.SECONDS), this);从页面调用带注释的属性时,通过反射设置(装饰)(请参阅AppiumFieldDecorator),使用代理(ElementInterceptor)包装一个MobileElement. 每次调用带注释的属性的方法时,实际上都会调用查找元素并转发方法调用的代理。两者之间没有缓存(与之相反WidgetInterceptor,我还没有弄清楚它的使用位置)。

因此,在我的情况下,创建页面一次或在每个步骤中并没有真正的区别,因为每次与它交互时都会执行元素查找(我想这很好,但它也可能会对性能产生影响)。

我还在下面附上了一些截图:

Stacktrace 当你调用 PageFactory.initElements(new AppiumFieldDecorator(driver, 15, TimeUnit.SECONDS), this);

在此处输入图像描述

在此处输入图像描述

调用click元素时的堆栈跟踪

在此处输入图像描述

在此处输入图像描述

希望这有助于其他人了解该工具的工作原理。

于 2016-12-04T11:37:16.040 回答