3

我開始在Android上學習UIAutomator。 我創建了一個簡單的poc項目,裏面有很少的元素:一個按鈕和一個editText。 這個行爲很簡單: 當我推動botton時,在editText中寫的消息出現在一個snackBar中。用UiAutomator測試Snackbar,有沒有辦法?

現在我想提出兩個簡單的測試:

  1. 看是否小吃吧正確顯示
  2. 看到,如果editTest消息 正確的小吃吧報告

對於點一個我以這種方式完成:

@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)); 
} 

這就是我看小吃店的行爲,檢查小吃店是否正確打開。有沒有更好的方法來做到這一點?這樣,如果有更多元素以小吃棒行爲的相同方式命名,我將遇到問題

對於第二點,我沒有找到一種方法來測試它。 我只使用uiAutomator而不是ESPRESSO :)

感謝大家:)

+0

可以請你發佈你的活動圖像或佈局heirarchy?不是它出現'uiautomatorviewer'小吃店? – Rilwan

+0

是否必須是uiautomator或者您是否也可以使用espresso檢查? – stamanuel

回答

0

我只是想在一個新的Android項目:我在主要佈局一個按鈕和顯示按鈕點擊小吃吧像這樣:

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!"); 
} 

兩個測試都可以正常工作! 另一個選擇小吃吧文本的方式是通過資源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. 
相關問題