Android的测试
一个简单的Android界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sam.a.MainActivity"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="25dp"
android:id="@+id/textView"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name here"
android:id="@+id/editText"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SAY HELLO"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Java代码
package com.example.sam.a;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sayHello(View view){
TextView textView=(TextView)findViewById(R.id.textView);
EditText editText=(EditText)findViewById(R.id.editText);
extView.setText("Hello,"+editText.getText().toString()+"!");
}
}
测试代码
public class MainActivityInstrumentation {
private static final String STRING_TO_BE_TYPED="Peter";
@Rule
public ActivityTestRule<MainActivity>mActivityRule=new ActivityTestRule<>(MainActivity.class);
@Test
public void sayHello(){
onView(withId(![]()
R.id.editText)).perform(typeText(STRING_TO_BE_TYPED).closeSoftKeyboard());
onView(withId(![]()
onView(withId(
R.id.btn)).perform(click());
String expectedText="Hello,"+STRING_TO_BE_TYPED+"!";
onView(withId(![]()
R.id.textview)).check(matches(withText(expectedText)));
}
}