在机器人中是否有类似pagefactory的模式?

Fio*_*ona 1 android robotium

我正在尝试使用robotium为我们的Android应用程序构建自动化测试用例环境.虽然robotium现在可以运行,但我仍然对如何使测试用例更简洁或更有条理感到困惑.现在,测试用例看起来非常复杂和混乱.当我使用硒时,有一个pagefactory模式.

机器人中有类似的东西吗?

Jam*_*unn 5

首先,您需要区分两种模式,即Page ObjectPage Factory.

  • 页面对象图案是通过使表示网页(或在MOBLE应用的equivilent)类实现的.
  • 页面出厂设置模式是组合页面对象抽象工厂模式.Selenium确实带有实现PageObject Factory模式的类,但实现可能很棘手且容易出错,而且我的同事和我都没有发现它的使用.

因此,由于页面对象模式是您真正想要的,我将向您展示我的同事和我想出的一些在Robotium中实现此模式的内容.

在Robotium中,Selenium WebDriver的粗略等效是Solo类.它基本上是一堆其他对象的装饰器(你可以看到GitHub存储库中所有相关类的列表).

要使用Robotium Solo对象实现页面对象模式,首先要使用具有Solo字段的抽象页面对象(如在Selenium中,您将拥有WebDriver字段).

public abstract class AppPage {

    private Solo solo;

    public AppPage(Solo solo) {
        this.solo = solo;
    }

    public Solo getSolo() {
        return this.solo;
    }   
}
Run Code Online (Sandbox Code Playgroud)

然后,为每个页面扩展AppPage,如下所示:

public class MainPage extends AppPage {

    public MainPage(Solo solo) {
        super(solo);
    }

    // It is useful to be able to chain methods together.

    // For public methods that direct you to other pages,
    // return the Page Object for that page.
    public OptionsPage options() {
        getSolo().clickOnButton(getSolo().getString(R.string.options_button));
        return new OptionsPage(getSolo());
    }

    //For public methods that DON'T direct you to other pages, return this.
    public MainPage searchEntries(String searchWord) {
        EditText search = (EditText) solo.getView(R.id.search_field);
        solo.enterText(search, searchWord);
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

在Robotium中实现页面对象模式时,您可以做很多花哨的事情,但这将使您从正确的方向开始.