PageObject
- 老馬關(guān)于PageObject的定義
It should allow a software client to do anything and see anything that a human can
- PageObject到底是個(gè)啥
將針對(duì)Page的
所有操作
進(jìn)行統(tǒng)一
封裝熄阻,如: 輸入框中輸入內(nèi)容、點(diǎn)擊按鈕等
-
E2E的幾個(gè)
痛點(diǎn)
-
前端調(diào)整
引起元素定位不到,如
登錄頁(yè)面的
密碼
輸入框的id被程序員不小心
給刪除了袜茧,執(zhí)行E2E測(cè)試過(guò)程中就會(huì)無(wú)法定位到密碼
輸入框撵幽,即使有錯(cuò)誤提示梯捕,你就要
找到所有
的密碼
輸入框并修改密碼
輸入框元素定位-
相同的
頁(yè)面操作,要在不同的地方使用从诲,寫(xiě)N
次种蝶,代碼冗余
契耿,很不利于后期維護(hù)
用戶(hù)登錄,這樣的操作是最常見(jiàn)的螃征。但我們經(jīng)常會(huì)出現(xiàn)在每個(gè)需要
登錄
的地方搪桂,都
寫(xiě)一遍用戶(hù)
登錄 -
-
使用PageObject都可以解決
- 如果元素定位不到,只需要修改對(duì)應(yīng)的頁(yè)面的PageObject就好盯滚,
一處改動(dòng)踢械,關(guān)聯(lián)的地方都OK了
- 將相同的頁(yè)面操作,進(jìn)行
抽離
至PageObject中魄藕,大大減少代碼的冗余
- 如果元素定位不到,只需要修改對(duì)應(yīng)的頁(yè)面的PageObject就好盯滚,
下面我們看一個(gè)實(shí)踐: 如何將頁(yè)面進(jìn)行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
中可以看出測(cè)試流程是這樣
- 第一步:
R.id.email
輸入內(nèi)容STRING_TO_BE_TYPED_EMAIL
内列,再驗(yàn)證輸入內(nèi)容是否為STRING_TO_BE_TYPED_EMAIL
- 第二步:
R.id.password
輸入內(nèi)容STRING_TO_BE_TYPED_EMAIL_PASSWORD
,再驗(yàn)證輸入內(nèi)容是否為STRING_TO_BE_TYPED_EMAIL_PASSWORD
- 第三步: 點(diǎn)擊
R.id.email_sign_in_button
按鈕
這是一個(gè)最常見(jiàn)的E2E測(cè)試的編寫(xiě)方式背率,這樣的編寫(xiě)方式话瞧,明顯就
一步一步
進(jìn)入了我們上面提的 E2E的幾個(gè)痛點(diǎn)
中
分離
手術(shù)
- 提取
登錄PageObject
:LoginPageObject
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());
}
}
- 使用
登錄PageObject
來(lái)實(shí)現(xiàn)之前
的測(cè)試功能
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已經(jīng)分離完成寝姿,我們肯定不會(huì)到此就結(jié)束的交排,
再把
測(cè)試數(shù)據(jù)進(jìn)行分離
,便于整體數(shù)據(jù)的維護(hù)
- 提取
用戶(hù)數(shù)據(jù)
:UserInfo
public class UserInfo {
public static final String email = "EspressoDemo@mail.com";
public static final String password = "123456";
}
-
再次
調(diào)整測(cè)試代碼
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();
}
}
走到這里饵筑,我們會(huì)發(fā)現(xiàn)埃篓,現(xiàn)在的測(cè)試很
清晰
,也很好維護(hù)
測(cè)試僅有三
步驟
-
Login
輸入郵箱地址 -
Login
輸入密碼 -
Login
點(diǎn)擊登錄按鈕
有沒(méi)有發(fā)現(xiàn)根资,這樣的測(cè)試架专,更
符合
業(yè)務(wù).
總結(jié)
- 使用PageObject模型后,測(cè)試代碼更加便于
維護(hù)
嫂冻,大大增加了可讀性
,減少了冗余
代碼 - PageObject就是將頁(yè)面的操作行為胶征,進(jìn)行單獨(dú)
分離
和維護(hù)塞椎,統(tǒng)一管理
-
PageObject/測(cè)試數(shù)據(jù)/測(cè)試腳本的關(guān)系
關(guān)系圖 - 完整代碼: https://github.com/aimer1124/EspressoDemo