[英]Test a Snackbar with UiAutomator, is there a way?
我開始在Android上學習UIAutomator。 我創建了一個簡單的poc項目,里面有很少的元素:一個按鈕和一個editText。 行為很簡單:當我按下按鈕時,editText中寫入的消息出現在snackBar中。
現在我想做兩個簡單的測試:
對於第一點我用這種方式做了:
@Test
public void pressEmailButton() throws UiObjectNotFoundException {
mDevice.findObject( By.res(POC_PACKAGE,"fab") ).click();
// wait for the snackBar appear
UiObject snackBar2 = new UiObject (new UiSelector().text("Send"));
// Verify the snackBarIsShowed is displayed in the Ui
assertTrue("Timeout while snackbar", snackBar2.waitForExists(1000));
}
那就是我正在觀看小吃店檢查小吃店是否正確打開的動作。 有更好的方法嗎? 通過這種方式,如果有更多的元素以與snackbar的動作相同的方式命名,我將遇到問題
對於第二點,我找不到測試它的方法。 我只能使用uiAutomator而不是Espresso :)
謝謝大家 :)
我剛試了一個新的android項目:我在主布局中有一個Button,並按下按鈕點擊快餐欄,如下所示:
button = (Button) findViewById(R.id.button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentLayout = findViewById(R.id.root_layout);
Snackbar.make(parentLayout, "Snackbar Text", Snackbar.LENGTH_LONG)
.show();
}
});
在我的測試中,我然后測試它:
//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));
與使用UI Automator相同:
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject snackbarTextView = mDevice.findObject(new UiSelector().text("Snackbar Text"));
if(!snackbarTextView.getText().equals("Snackbar Text")) {
throw new RuntimeException("No snackbar!");
}
兩個測試都運行得很好! 選擇Snackbar文本的另一種方法是通過資源ID,如下所示:
//replace the package name in the next line with your package name
UiObject snackbarTextView = mDevice.findObject(new UiSelector().resourceId("com.example.testespressoapplication:id/snackbar_text"));
你做它的方式也有效但不推薦使用,所以我不會使用它,因為文檔說:
* @deprecated Use {@link UiDevice#findObject(UiSelector)} instead. This version hides
* UiObject's dependency on UiDevice and is prone to misuse.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.