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

时间:2013-06-24 09:21:53

标签: android robotium

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

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

3 个答案:

答案 0 :(得分:5)

首先,您需要区分两种模式, 页面对象 页面工厂

  • 页面对象 模式是通过创建代表网页的类(或moble app中的等效项)来实现的。
  • 页面工厂 模式是 页面对象 和<的组合i> Abstract Factory 模式。 Selenium确实附带了实现 PageObject Factory 模式的类,但是实现可能很棘手且容易出错,而且我的同事和我都没有发现使用它。

    < / LI>

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

在Robotium中,Selenium WebDriver的粗略等效是Solo类。对于一堆其他对象,它基本上是decorator(您可以在GitHub Repository中看到所有相关类的列表。)

要使用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;
    }   
}

然后,为每个页面扩展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;
    }
}

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

答案 1 :(得分:1)

您可以查看Robotium-Sandwich项目。 Robotium-Sandwich使为Robotium创建页面对象变得非常容易。

答案 2 :(得分:-1)

您可以使用https://code.google.com/p/selenium/wiki/PageObjects

中的页面对象