- Espresso 测试框架教程
- Espresso测试 - 主页
- 介绍
- 设置说明
- 在 Android Studio 中运行测试
- JUnit 概述
- 建筑学
- 查看匹配器
- 自定义视图匹配器
- 查看断言
- 查看操作
- 测试AdapterView
- 测试网页视图
- 测试异步操作
- 测试意图
- 测试多个应用程序的 UI
- 测试记录仪
- 测试用户界面性能
- 测试可访问性
- Espresso测试资源
- Espresso测试 - 快速指南
- Espresso 测试 - 有用资源
- Espresso测试 - 讨论
Espresso 测试框架 - 意图
Android Intent 用于打开新活动,无论是内部活动(从产品列表屏幕打开产品详细信息屏幕)还是外部活动(例如打开拨号器进行呼叫)。内部意图活动由 espresso 测试框架透明地处理,不需要用户方进行任何特定工作。然而,调用外部活动确实是一个挑战,因为它超出了我们的范围,即被测试的应用程序。一旦用户调用外部应用程序并退出正在测试的应用程序,那么用户以预定义的操作顺序返回应用程序的机会就会相当少。因此,我们需要在测试应用程序之前假设用户操作。Espresso 提供了两种选项来处理这种情况。它们如下:
故意的
这允许用户确保从被测应用程序打开正确的意图。
打算
这允许用户模拟外部活动,例如从相机拍照、拨打联系人列表中的号码等,并使用预定义的值集返回到应用程序(例如来自相机的预定义图像而不是实际图像) 。
设置
Espresso 通过插件库支持 Intent 选项,并且该库需要在应用程序的 gradle 文件中进行配置。配置选项如下,
dependencies {
// ...
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
}
故意的()
Espresso 意图插件提供了特殊的匹配器来检查调用的意图是否是预期的意图。提供的匹配器和匹配器的用途如下,
有动作
这接受意图操作并返回一个与指定意图匹配的匹配器。
有数据
这接受数据并返回一个匹配器,该匹配器在调用意图时匹配提供给意图的数据。
到包
这接受意图包名称并返回一个匹配器,该匹配器与调用的意图的包名称相匹配。
现在,让我们创建一个新应用程序并使用预期()测试该应用程序的外部活动以理解这个概念。
启动 Android 工作室。
如前所述创建一个新项目并将其命名为 IntentSampleApp。
使用Refactor → Migrate to AndroidX选项菜单将应用程序迁移到 AndroidX 框架。
通过更改Activity_main.xml来创建一个文本框、一个用于打开联系人列表的按钮和另一个用于拨打电话的按钮,如下所示,
<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity">
<EditText
android:id = "@+id/edit_text_phone_number"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:text = ""
android:autofillHints = "@string/phone_number"/>
<Button
android:id = "@+id/call_contact_button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:layout_below = "@id/edit_text_phone_number"
android:text = "@string/call_contact"/>
<Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:layout_below = "@id/call_contact_button"
android:text = "@string/call"/>
</RelativeLayout>
另外,在strings.xml资源文件中添加以下项目,
<string name = "phone_number">Phone number</string> <string name = "call">Call</string> <string name = "call_contact">Select from contact list</string>
现在,在主活动 ( MainActivity.java )的onCreate方法下添加以下代码。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// ... code
// Find call from contact button
Button contactButton = (Button) findViewById(R.id.call_contact_button);
contactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Uri uri = Uri.parse("content://contacts");
Intent contactIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
contactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(contactIntent, REQUEST_CODE);
}
});
// Find edit view
final EditText phoneNumberEditView = (EditText)
findViewById(R.id.edit_text_phone_number);
// Find call button
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(phoneNumberEditView.getText() != null) {
Uri number = Uri.parse("tel:" + phoneNumberEditView.getText());
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
}
}
});
}
// ... code
}
在这里,我们编写了带有 id、 call_contact_button 的按钮来打开联系人列表,以及带有 id、button 的按钮来拨打电话。
在MainActivity类中添加一个静态变量REQUEST_CODE,如下所示,
public class MainActivity extends AppCompatActivity {
// ...
private static final int REQUEST_CODE = 1;
// ...
}
现在,在MainActivity类中添加onActivityResult方法,如下所示,
public class MainActivity extends AppCompatActivity {
// ...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Bundle extras = data.getExtras();
// String phoneNumber = extras.get("data").toString();
Uri uri = data.getData();
Log.e("ACT_RES", uri.toString());
String[] projection = {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int numberColumnIndex =
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
int nameColumnIndex = cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameColumnIndex);
Log.d("MAIN_ACTIVITY", "Selected number : " + number +" , name : "+name);
// Find edit view
final EditText phoneNumberEditView = (EditText)
findViewById(R.id.edit_text_phone_number);
phoneNumberEditView.setText(number);
}
}
};
// ...
}
此处,当用户使用call_contact_button按钮打开联系人列表并选择联系人后返回到应用程序时,将调用onActivityResult 。调用onActivityResult方法后,它会获取用户选择的联系人,找到联系人号码并将其设置到文本框中。
运行应用程序并确保一切正常。Intent示例应用程序的最终外观如下所示,
现在,在应用程序的 gradle 文件中配置 espresso 意图,如下所示,
dependencies {
// ...
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
}
单击Android Studio 提供的“立即同步”菜单选项。这将下载意图测试库并正确配置它。
打开ExampleInstrumentedTest.java文件并添加IntentsTestRule而不是通常使用的AndroidTestRule。IntentTestRule是处理意图测试的特殊规则。
public class ExampleInstrumentedTest {
// ... code
@Rule
public IntentsTestRule<MainActivity> mActivityRule =
new IntentsTestRule<>(MainActivity.class);
// ... code
}
添加两个局部变量来设置测试电话号码和拨号程序包名称,如下所示,
public class ExampleInstrumentedTest {
// ... code
private static final String PHONE_NUMBER = "1 234-567-890";
private static final String DIALER_PACKAGE_NAME = "com.google.android.dialer";
// ... code
}
使用 android studio 提供的 Alt + Enter 选项修复导入问题,或者包含以下导入语句,
import android.content.Context; import android.content.Intent; import androidx.test.InstrumentationRegistry; import androidx.test.espresso.intent.rule.IntentsTestRule; import androidx.test.runner.AndroidJUnit4; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard; import static androidx.test.espresso.action.ViewActions.typeText; import static androidx.test.espresso.intent.Intents.intended; import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction; import static androidx.test.espresso.intent.matcher.IntentMatchers.hasData; import static androidx.test.espresso.intent.matcher.IntentMatchers.toPackage; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static org.hamcrest.core.AllOf.allOf; import static org.junit.Assert.*;
添加以下测试用例来测试拨号器是否被正确调用,
public class ExampleInstrumentedTest {
// ... code
@Test
public void validateIntentTest() {
onView(withId(R.id.edit_text_phone_number))
.perform(typeText(PHONE_NUMBER), closeSoftKeyboard());
onView(withId(R.id.button)) .perform(click());
intended(allOf(
hasAction(Intent.ACTION_DIAL),
hasData("tel:" + PHONE_NUMBER),
toPackage(DIALER_PACKAGE_NAME)));
}
// ... code
}
在这里,hasAction、hasData和toPackage匹配器与allOf匹配器一起使用,只有在所有匹配器都通过时才会成功。
现在,通过 Android studio 中的内容菜单运行ExampleInstrumentedTest。
打算()
Espresso 提供了一种特殊的方法——intending()来模拟外部意图操作。意图()接受要模拟的意图的包名称,并提供一个方法respondWith来设置需要如何响应模拟的意图,如下所示,
intending(toPackage("com.android.contacts")).respondWith(result);
在这里,respondWith()接受Instrumentation.ActivityResult类型的意图结果。我们可以创建新的存根意图并手动设置结果,如下所示,
// Stub intent
Intent intent = new Intent();
intent.setData(Uri.parse("content://com.android.contacts/data/1"));
Instrumentation.ActivityResult result =
new Instrumentation.ActivityResult(Activity.RESULT_OK, intent);
测试联系人应用程序是否正确打开的完整代码如下:
@Test
public void stubIntentTest() {
// Stub intent
Intent intent = new Intent();
intent.setData(Uri.parse("content://com.android.contacts/data/1"));
Instrumentation.ActivityResult result =
new Instrumentation.ActivityResult(Activity.RESULT_OK, intent);
intending(toPackage("com.android.contacts")).respondWith(result);
// find the button and perform click action
onView(withId(R.id.call_contact_button)).perform(click());
// get context
Context targetContext2 = InstrumentationRegistry.getInstrumentation().getTargetContext();
// get phone number
String[] projection = { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
Cursor cursor =
targetContext2.getContentResolver().query(Uri.parse("content://com.android.cont
acts/data/1"), projection, null, null, null);
cursor.moveToFirst();
int numberColumnIndex =
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
// now, check the data
onView(withId(R.id.edit_text_phone_number))
.check(matches(withText(number)));
}
在这里,我们创建了一个新的意图并将返回值(调用该意图时)设置为联系人列表的第一个条目content://com.android.contacts/data/1。然后我们设置了意图方法来模拟新创建的意图来代替联系人列表。当调用com.android.contacts包并返回列表的默认第一个条目时,它会设置并调用我们新创建的意图。然后,我们触发click()操作来启动模拟意图,最后检查调用模拟意图的电话号码与联系人列表中第一个条目的号码是否相同。
如果存在任何缺失的导入问题,请使用 android studio 提供的 Alt + Enter 选项修复这些导入问题,或者包含以下导入语句,
import android.app.Activity; import android.app.Instrumentation; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; import androidx.test.InstrumentationRegistry; import androidx.test.espresso.ViewInteraction; import androidx.test.espresso.intent.rule.IntentsTestRule; import androidx.test.runner.AndroidJUnit4; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard; import static androidx.test.espresso.action.ViewActions.typeText; import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.intent.Intents.intended; import static androidx.test.espresso.intent.Intents.intending; import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction; import static androidx.test.espresso.intent.matcher.IntentMatchers.hasData; import static androidx.test.espresso.intent.matcher.IntentMatchers.toPackage; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.core.AllOf.allOf; import static org.junit.Assert.*;
在测试类中添加以下规则以提供读取联系人列表的权限 -
@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS);
在应用程序清单文件AndroidManifest.xml中添加以下选项-
<uses-permission android:name = "android.permission.READ_CONTACTS" />
现在,确保联系人列表至少有一个条目,然后使用 Android Studio 的上下文菜单运行测试。