android自動化測試 ESPRESSO的使用詳解
ESPRESSO
studio3.5版本
新的改變
大家可以去各大招聘網站上看看,大部分公司都要求開發人員會編寫測試用例或使用框架或工具進行測試,並且大公司要求更甚.
這下還有什麼好說的嗎?單元測試是一個硬要求,即便你不喜歡單元測試,但是如果你想進入一個理想的公司,這是必備的一個技能,所以無論如何你都要去學習的,反正技多不壓身,所以我們更得去使用它
//這是Activity主代碼
butt = (Button) findViewById(R.id.butt);
tv = (TextView) findViewById(R.id.tv);
et = (EditText) findViewById(R.id.et);
login = (Button) findViewById(R.id.login);
butt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setText(“gg”);
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = et.getText().toString();
String pass=“yan”;
if (!TextUtils.isEmpty(s)){
String s1 = tv.getText().toString();
if (s.equals(pass)&&s1.equals(“gg”)){
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}else {
Toast.makeText(MainActivity.this,“失敗”,Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(MainActivity.this,“爲空”,Toast.LENGTH_SHORT).show();
}
}
});
//測試用例
@LargeTest
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule rule =
new ActivityTestRule<>(MainActivity.class);
private ViewInteraction butt;
@Test
public void testApp() {
/* 判斷登錄按鈕是否顯示*/
onView(withId(R.id.login)).check(matches(isDisplayed()));
/*直接點擊登錄按鈕*/
//onView(withId(R.id.login)).perform(click());
onView(withId(R.id.butt)).perform(click());
onView(withId(R.id.et)).perform(typeText("yan"));
onView(withId(R.id.login)).perform(click());
onView(withText("ok")).check(matches(isDisplayed()));
onView(withId(R.id.name)).perform(typeText("sahngsan"));
onView(withText("ok")).perform(click());
}
}