ui自动化测试Android测试自动化测试

PageObject分离E2E测试

2016-11-24  本文已影响123人  Yuan_Jie

PageObject

It should allow a software client to do anything and see anything that a human can

将针对Page的所有操作进行统一封装,如: 输入框中输入内容、点击按钮等

下面我们看一个实践: 如何将页面进行PageObject抽离

分离PageObject

原始代码

public class ExampleInstrumentedTest {


    public static final String STRING_TO_BE_TYPED_EMAIL = "EspressoDemo@mail.com";
    public static final String STRING_TO_BE_TYPED_EMAIL_PASSWORD = "123456";

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
            LoginActivity.class);

    @Test
    public void testAttemptLogin() {
        // Type text and then press the button.
        onView(withId(demo.test.espressodemo.R.id.email))
                .perform(typeText(STRING_TO_BE_TYPED_EMAIL), closeSoftKeyboard());
        onView(withId(demo.test.espressodemo.R.id.email))
                .check(matches(withText(STRING_TO_BE_TYPED_EMAIL)));

        onView(withId(demo.test.espressodemo.R.id.password))
                .perform(typeText(STRING_TO_BE_TYPED_EMAIL_PASSWORD), closeSoftKeyboard());
        onView(withId(demo.test.espressodemo.R.id.password))
                .check(matches(withText(STRING_TO_BE_TYPED_EMAIL_PASSWORD)));

        onView(withId(demo.test.espressodemo.R.id.email_sign_in_button))
                .perform(click());

    }
}

testAttemptLogin中可以看出测试流程是这样

这是一个最常见的E2E测试的编写方式,这样的编写方式,明显就一步一步进入了我们上面提的 E2E的几个痛点

分离手术

public class LoginPageObject {

    public static void inputEmail(String email){
        onView(withId(demo.test.espressodemo.R.id.email))
                .perform(typeText(email), closeSoftKeyboard());
        onView(withId(demo.test.espressodemo.R.id.email))
                .check(matches(withText(email)));
    }

    public static void inputPassword(String password){
        onView(withId(demo.test.espressodemo.R.id.password))
                .perform(typeText(password), closeSoftKeyboard());
        onView(withId(demo.test.espressodemo.R.id.password))
                .check(matches(withText(password)));
    }

    public static void clickLogin(){
        onView(withId(demo.test.espressodemo.R.id.email_sign_in_button))
                .perform(click());
    }
}
public class ExampleInstrumentedTest {
    public static final String email = "EspressoDemo@mail.com";
    public static final String password = "123456";

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
            LoginActivity.class);

    @Test
    public void testAttemptLogin() {
        // Type text and then press the button.

        LoginPageObject.inputEmail(email);

        LoginPageObject.inputPassword(password);

        LoginPageObject.clickLogin();

    }
}

至此,PageObject已经分离完成,我们肯定不会到此就结束的,再把测试数据进行分离,便于整体数据的维护

public class UserInfo {
    public static final String email = "EspressoDemo@mail.com";
    public static final String password = "123456";

}
public class ExampleInstrumentedTest {

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
            LoginActivity.class);

    @Test
    public void testAttemptLogin() {
        // Type text and then press the button.

        LoginPageObject.inputEmail(UserInfo.email);

        LoginPageObject.inputPassword(UserInfo.password);

        LoginPageObject.clickLogin();

    }
}

走到这里,我们会发现,现在的测试很清晰,也很好维护

测试仅有步骤

有没有发现,这样的测试,更符合业务.

总结

参考

上一篇 下一篇

猜你喜欢

热点阅读