使用UI测试用例在Android Studio中运行测试

我正在尝试在当前工作的应用程序中添加UI测试用例。 为此,我在app/src/androidtest/java/com.testapp

中添加了此内容
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.Viewactions.*
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Instrumented test,which will execute on an Android device.
 *l
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {


    @Test
    fun loginactivityTest() {
// Type text and then press the button.
        onView(withId(R.id.editTextusername)).perform(typeText(STRING_EMAIL),closeSoftKeyboard())
        onView(withId(R.id.editTextUserPassword)).perform(typeText(STRING_PASSWORD),closeSoftKeyboard())
        onView(withId(R.id.btnLoginSigin)).perform(click())

        // This view is in a different activity,no need to tell Espresso.
//        onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)))
    }

    companion object {
        val STRING_EMAIL = "test@test.com"
        val STRING_PASSWORD = "test1234"
    }
}

添加到build.gradle中:

// Testing-only dependencies
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
androidTestImplementation 'androidx.test:core:' + rootProject.coreversion
androidTestImplementation 'androidx.test:core-ktx:' + rootProject.coreversion
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test.ext:junit-ktx:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion

testImplementation 'androidx.test:core:' + rootProject.coreversion;
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
testImplementation 'androidx.test.espresso:espresso-intents:' + rootProject.espressoVersion
testImplementation 'androidx.test.ext:truth:' + rootProject.extTruthVersion


//Test
buildToolsVersion = "28.0.3"
androidxLibVersion = "1.0.0"
robolectricVersion = "4.3"
extTruthVersion = '1.3.0-alpha02'
coreversion = "1.2.1-alpha02"
extJUnitVersion = "1.1.2-alpha02"
runnerVersion = "1.3.0-alpha02"
espressoVersion = "3.3.0-alpha02"

但是在编辑配置后仅运行测试类。 我收到错误消息:

    Task :app:compileappnameqaDebugAndroidTestJavaWithJavac FAILED
F:\test_project\Android_new\app\build\generated\source\buildConfig\androidTest\debug\com\appname\test\BuildConfig.java:17: error: cannot find symbol
  public static final int appIcon = R.mipmap.ic_launcher;
                                     ^
  symbol:   variable mipmap
  location: class R
F:\test_project\Android_new\app\build\generated\source\buildConfig\androidTest\debug\test\com\appname\test\BuildConfig.java:23: error: cannot find symbol
  public static final int splashImage = R.drawable.splash;
                                         ^
  symbol:   variable drawable
  location: class R
2 errors

任何人都可以帮我知道如何在我的应用程序中运行UI测试用例吗?

ymh1982611 回答:使用UI测试用例在Android Studio中运行测试

从已测试的测试用例ExampleInstrumentedTest中,我找不到您要在其中启动模拟用户操作的活动的位置。请尝试先启动活动,然后执行测试用例。例如,如果您的活动是LoginActivity

    @RunWith(AndroidJUnit4.class)
    public class SampleTest {

      @Rule
      public ActivityTestRule<LoginActivity> activityRule 
         = new ActivityTestRule<>(
            LoginActivity.class,true,// initialTouchMode
            false);   // launchActivity. False to customize the intent

      @Test
      public void loginActivityTest() {
        Intent intent = new Intent();
        activityRule.launchActivity(intent);

onView(withId(R.id.editTextUserName)).perform(typeText(STRING_EMAIL),closeSoftKeyboard())
        onView(withId(R.id.editTextUserPassword)).perform(typeText(STRING_PASSWORD),closeSoftKeyboard())
        onView(withId(R.id.btnLoginSigin)).perform(click())
      }
    }
,

添加行后:

@get:Rule val activityRule = activityScenarioRule<LoginActivity>()

仍然遇到相同的问题。然后我复制了创建为res / drawable和res / mipmap的可绘制和mipmap文件,并将这些文件粘贴到文件夹中。 结果消除了BuildConfig错误。然后我开始遇到错误:

Command line is too long. Shorten command line for 'testconfiguration' or also for Android JUnit default Configuration

要克服这个问题: 测试配置设置中的select option "classpath file",它将运行测试。

本文链接:https://www.f2er.com/3024198.html

大家都在问