Tag: android espresso

Android,Espresso仪器化测试仅适用于Android 5.0+

我有很多意式浓缩咖啡的测试。 而且他们在Android 5.0上工作正常。 这是我的build.gradle: buildscript { repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'io.fabric' repositories { maven { url 'https://maven.fabric.io/public' } mavenCentral() } android { compileSdkVersion 25 buildToolsVersion "25.0.0" dexOptions { jumboMode = true } defaultConfig { applicationId "com.mycompany" minSdkVersion […]

在测试活动的onCreate期间开始使用Espresso来存根意图

我正在测试在onCreate()期间启动另一个活动的活动。 第二个活动是从startActivityForResult()开始的,然后主活动等待onActivityResult() 。 我试图用Espresso来测试这个,尝试用intending()来存储第二个活动,并且使用intended()来验证它是否发生了。 不过,似乎espresso-intents并不是用onCreate()方法启动的onCreate()来工作的( 请参阅这里最后几段的警告 )。 有没有人设法从onCreate()开始存根意图,如果是的话,怎么样?

当imageuri作为额外的传递时,Espresso测试相机意图

我需要通过在意图额外提供的路径创建一个图像文件存根相机意图。 意式浓缩咖啡只能以活动结果回应。 我在哪里可以执行操作来创建文件在传递路径从意图额外。 代码启动相机 File destination = new File(Environment.getExternalStorageDirectory(), "app_name" + System.currentTimeMillis() + ".jpg"); imageUri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext()。getPackageName()+“.fileprovider”,destination); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent,AppConstants.REQUEST_CODE_CAMERA); 代码测试中的stubbing意图 Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, null); intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);

在浓缩咖啡测试超时,测试不运行

有一个辅助类DialogIdlingResource,我试着运行我的测试。 测试发生,除了这一个。 按下按钮后,一切都停止。 AcceptanceHelper.clickOnButtonInLayout(R.id.mainSignButton, R.string.common_signin_button_text, R.id.inputLayout) 带有两个辅助方法的代码片段: @Test fun signInUserWithInvalidEmail() { goToSignIn() AcceptanceHelper.updateValidationTextView(R.string.ui_data_attribute_email, "kokojambo@mail.ru") AcceptanceHelper.updateValidationTextView(R.string.ui_data_attribute_password, VALID_PASSWORD) AcceptanceHelper.clickOnButtonInLayout(R.id.mainSignButton, R.string.common_signin_button_text, R.id.inputLayout) val idlingResource = DialogIdlingResource() registerDialogIdlingResource() unregisterDialogIdlingResource() } private fun registerDialogIdlingResource() { val instrumentation = InstrumentationRegistry.getInstrumentation() idlingResource = DialogIdlingResource() Espresso.registerIdlingResources(idlingResource) } private fun unregisterDialogIdlingResource() { Espresso.unregisterIdlingResources(idlingResource) } 可能由于regester和unregister(idlingResource)两种方法导致的错误, 但实际上它应该可以工作,但也可能是某个可能犯了错误的地方,即辅助类代码: class DialogIdlingResource(private val waitTimeSeconds: Int = 5) : […]

Dagger2注射咖啡测试

我想在我的Espresso测试中注入由匕首创建的组件。 组件应该是一样的,所以,没有必要用匕首来压倒任何东西。 我有以下班级: @RunWith(AndroidJUnit4.class) public class AccountRepositoryTest { @Inject AccountRepository repository; @Before public void setUp() throws Exception { new DaggerTestComponent().builder().build().inject(this); } } 由于我不能将AccountRepositoryTest添加到我的主要DaggerComponent类,所以我在我的androidTests文件夹中创建了另一个组件类: @Singleton @Component(modules = arrayOf(AppModule::class, DatabaseModule::class, RepositoryModule::class)) interface TestComponent: AppComponent { fun inject(accountRepositoryTest: AccountRepositoryTest) } 但是匕首永远不会从TestComponent接口生成ComponentClass,当我编译代码时,我总是收到这个错误: Error:(26, 7) error: cannot find symbol class DaggerTestComponent 如果我评论这行,我的代码会被编译,所以我确信这只是防止匕首生成类。 所以我的问题是:如何使匕首从androidTests文件夹中定义的接口生成组件类?

如何检测Android应用程序是否正在用Espresso进行UI测试

我正在为Android编写一些Espresso测试。 我正在运行在以下问题: 为了使某个测试用例正常运行,我需要禁用应用程序中的某些功能。 因此,在我的应用程序中,我需要检测是否正在运行Espresso测试,以便禁用它。 但是,我不想使用BuildConfig.DEBUG ,因为我不希望这些功能在调试版本中被禁用。 另外,我想避免创建一个新的buildConfig来避免创建太多的构建变体(我们已经定义了很多的变体)。 我正在寻找一种方法来定义buildConfigField进行测试,但是我在Google上找不到任何参考。

多次运行浓缩咖啡测试

有时我遇到了我的应用程序中的罕见错误。 但是我不能重现它,因为它非常罕见。 所以,我决定写简单的咖啡测试: @RunWith(AndroidJUnit4::class) @LargeTest class MainActivityTest { val password = "1234" @Rule @JvmField var mActivityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java) @Test fun checkNotesListNotEmpty() { onView(withId(R.id.password_edit_text)).perform(typeText(password)) onView(withId(R.id.notes_recycler_view)).check { view, noMatchingViewException -> if (noMatchingViewException != null) throw noMatchingViewException assertThat((view as RecyclerView).adapter.itemCount, Matchers.`is`(1)) } } } 如何循环测试并在匹配失败时停止测试?

Android Espresso无法输入TYPE_TEXT_VARIATION_NORMAL?

我在我的Android应用程序中有一个TextInputEditText视图,我正在通过编程(通过Anko和Kotlin)来设置它: textInputEditText { id = R.id.text_input_id inputType = EditorInfo.TYPE_TEXT_VARIATION_NORMAL hint = resources.getText(R.string.text_hint) } 然后在一个Espresso测试中,我把这一行称为: onView(withId(R.id.text_input_id)).perform(typeText(textToType)) 其中失败与此错误: Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: ((is displayed on the screen to the user) and (supports input methods or is assignable from class: […]

在Before方法中,ActivityTestRule.getActivity返回null

我需要在每次测试之前清空我的用户数据 // Kotlin code fun getActivity() = activityRule.getActivity() Before fun setUp() { cleanUp(getActivity()) } 我需要获取一个上下文才能这样做,但在setUp中,activityRule.getActivity()返回null。 我也试过: Before fun setUp() { val i = Intent() i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) activityRule.lauchActivity(i) cleanUp(getActivity()) } 我有一个活动,但清理工作只有一半的时间(我相信一些竞争条件适用于此) 我想避免清理后,以便手动查看我的应用程序状态,如果需要。 有没有在Before函数中获取上下文? 谢谢

Google Espresso:删除每个测试的用户数据

我正在寻找一种在每次使用Google Espresso进行测试之前从应用程序中删除数据库的方法。 我在@Before函数(kotlin代码)中有这段代码: @Before fun setUp() { val appCtx = InstrumentationRegistry.getTargetContext().applicationContext DBPrefsManager.getInstance(appCtx).resetAll() val client = appCtx.contentResolver.acquireContentProviderClient("fr.geobert.radis.db") val provider = client.localContentProvider as DbContentProvider provider.deleteDatabase(appCtx) client.release() val i = Intent() i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) activityRule.launchActivity(i) } 它曾经工作过,但我离开我的兼职项目一个月,现在,升级到API 23和新的测试框架版本后,我所有的测试无法启动,因为“activityRule.launchActivity(i)”。 所以我摆脱了最后3行,测试可以再次启动,但通过删除数据库代码失败一次在两个:( 在应用程序由测试框架启动之前,有没有更好的方法来删除用户数据?