- Testing Accessibility
- Testing UI Performance
- Test Recorder
- Testing UI for Multiple Application
- Testing Intents
- Testing Asynchronous Operations
- Testing WebView
- Testing AdapterView
- View Actions
- View Assertions
- Custom View Matchers
- View Matchers
- Architecture
- Overview of JUnit
- Running Tests In Android Studio
- Setup Instructions
- Introduction
- Espresso Testing - Home
Espresso Testing Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Espresso Testing Framework - Intents
乙型斜体用于在内部(从产品清单屏幕上打开产品详细屏幕)或外部(如打开传话的传言)开展新的活动。 内部意向活动由压缩测试框架透明处理,不需要用户方面的任何具体工作。 然而,援引外部活动确实是一个挑战,因为它超出了我们的范围,正在测试之中。 用户一旦申请外部申请,并在测试申请之外申请,用户利用预先确定的行动顺序返回申请的机会就会减少。 因此,我们需要在测试申请之前采取用户行动。 Espresso为处理这种情况提供了两种选择。 页: 1
intended
这使得用户能够确保正确的意图从测试申请开始。
intending
这使得用户能够模拟外部活动,例如从摄像机中摄取照片,从接触清单中删除若干个,并以预先界定的一套价值(如从摄像机中预先确定的形象而不是实际形象)重新应用。
Setup
Espresso通过一个原始图书馆支持意向选择,图书馆需要在申请的梯度档案中配置。 组合办法如下:
dependencies { // ... androidTestImplementation androidx.test.espresso:espresso-intents:3.1.1 }
intended()
埃斯普诺意向书提供特殊配对人,检查所援引的意图是否是预期的意图。 提供配对器和配对人的目的如下:
hasAction
这接受意向行动,并交还一个符合具体意图的对口单位。
hasData
这接收了数据,并交还了一台配对机,在引用数据时将数据与意图相匹配。
toPackage
这接受一揽子意向书的名称,并交还一个配对器,与所援引意图的一揽子名称相符。
现在,让我们利用in 预期()来理解这一概念,制定新的申请并测试外部活动的申请。
开始陈设室。
创建先前讨论的新项目,名称为Intent SampleApp。
采用Re Factor ——Migrate 至Androidx 选项菜单。
建立一个案文箱、一个开放联系名单的纽伦和一个更改active_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 pst</string>
现在,在主要活动(MainActative.java)下,在onCreate方法下添加以下代码。
pubpc 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.setOnCpckListener(new View.OnCpckListener() { @Override pubpc void onCpck(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.setOnCpckListener(new View.OnCpckListener() { @Override pubpc void onCpck(View view) { if(phoneNumberEditView.getText() != null) { Uri number = Uri.parse("tel:" + phoneNumberEditView.getText()); Intent callIntent = new Intent(Intent.ACTION_DIAL, number); startActivity(callIntent); } } }); } // ... code }
在此,我们安排了纽芬兰语、-----contact_button,以开放联系名单,并以id语、button到拨打。
增加一个静态变量REquestST_CODE,MainActative。 下表所列类别:
pubpc class MainActivity extends AppCompatActivity { // ... private static final int REQUEST_CODE = 1; // ... }
现在,在MainActative上添加onActativeResult方法。 类别如下:
pubpc 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); } } }; // ... }
这里,如果用户在使用 电话_contact_button 纽顿和选择联系地址之后返回申请,将援引。 一旦采用ActativeRes方法,便可与用户选定联系,查找联系号码,并将其放入文字箱。
提出申请并确保一切都属于罚款。 下面是Intent samples Apppcation的最后概览。
现在,将申请的梯度卷宗中的标语混淆如下:
dependencies { // ... androidTestImplementation androidx.test.espresso:espresso-intents:3.1.1 }
Cpck the Sync 如今由陈列室提供的菜单。 这将下载意向测试图书馆并适当配置。
开放ExampleInstrumented.java http://www.un.org/Depts/DGACM/index_chinese.htm 《专利试验规则>是处理意向测试的特别规则。
pubpc class ExampleInstrumentedTest { // ... code @Rule pubpc IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(MainActivity.class); // ... code }
添加两个地方变量,将测试电话号码和方言包装名称如下:
pubpc 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 }
通过使用Alt + 业余演播室提供的选修办法,确定进口问题,或者包括以下进口说明:
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.cpck; 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.*;
添加以下测试案例,以检验是否适当传唤了方言,
pubpc class ExampleInstrumentedTest { // ... code @Test pubpc void vapdateIntentTest() { onView(withId(R.id.edit_text_phone_number)) .perform(typeText(PHONE_NUMBER), closeSoftKeyboard()); onView(withId(R.id.button)) .perform(cpck()); intended(allOf( hasAction(Intent.ACTION_DIAL), hasData("tel:" + PHONE_NUMBER), toPackage(DIALER_PACKAGE_NAME))); } // ... code }
这里,hasAction,hasData和toPackage。 配对器和allOf配对机一起使用,只有所有配对机通过时才能成功。
目前有ExampleInstrumented Test。 通过文稿载于Antoin 工作室。
intending()
Espresso提供了一种特殊方法——inring()模拟一种外部意图行动。 inping( 接受拟作模拟的整套意图的名称,并提供一种方法respondWith,以确定如何按照以下具体规定对模拟意图作出反应:
intending(toPackage("com.android.contacts")).respondWith(result);
此处respondWith() 接受“侵扰”的意向。 活动。 我们可以产生新的顽固意图,人工确定以下具体结果:
// 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 pubpc 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 cpck action onView(withId(R.id.call_contact_button)).perform(cpck()); // 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。 然后,我们制定了inping方法,以模拟新产生的意向,取代联系名单。 当一揽子计划(com.android.contacts)被援引时,它确定并称之为我们新创造的意图,而名单的首次出现被退回。 然后,我们启动了cpck(>>>>)行动,以启动模拟意图,最后检查电话号码是否从援引模拟意图和联系名单上第一个条目数目相同。
不存在任何缺失的进口问题,然后通过使用Alt + 试验室提供的替代品来确定这些进口问题,或者包括以下进口表,
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.cpck; 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 pubpc GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS);
在申请清单中添加以下选择:AndroidManifest.xml
<uses-permission android:name = "android.permission.READ_CONTACTS" />
现在,确保联络名单至少有一个条目,然后使用“安”演播室的环境菜单进行测试。
Advertisements