Espresso测试失败的原因是“与依赖项冲突”

时间:2018-11-18 15:43:09

标签: android android-espresso-recorder

我只是想开始使用Android Studio 3.2.1版中的“ Record Espresso测试”集成我的APP的一些基础测试。 我可以记录测试。之后,Studio提示我,某些依赖项丢失了,是否应该添加它们。 在这里,我选择是。 Gradle希望再次同步,并且该过程没有任何问题。 如果我现在想运行其中一项测试,则构建过程开始并开始。失败了

Conflict with dependency 'androidx.annotation:annotation' in project ':app'. Resolved versions for app (1.0.0-rc01) and test app (1.0.0) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

我查看了提供的链接,但并没有真正让我更进一步。

有人能指导我正确的方向,我做错了什么吗?

由于30000个字符的限制,“ androidDependencies”的结果太长了,无法附加到我的问题中。

我的Android应用Gradle文件

apply plugin: 'com.android.application'

project.ext.appPlayStoreVersion = 68
project.ext.appVersion = '1.8.2'
project.ext.appLabel = 'FreightWeight'

project.ext.compileSdk = 28
project.ext.minSdk = 21
project.ext.targetSdk = 28

project.ext.firebase_version = '16.0.1'
project.ext.firebase_ui_version = '2.1.1'
project.ext.support_library_version = '27.1.1'
project.ext.buildTools = '28.0.3'
project.ext.multidex_version = '1.0.1'
project.ext.glide_version = '3.7.0'
project.ext.constraints_version = '1.0.2'

// Create a variable called keystorePropertiesFile, and initialize it to your
// keystore.properties file, in the rootProject folder.
def keystorePropertiesFile = rootProject.file("keystore.properties")

// Initialize a new Properties() object called keystoreProperties.
def keystoreProperties = new Properties()

// Load your keystore.properties file into the keystoreProperties object.
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))


android {
    signingConfigs {
        configRelease {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
    compileSdkVersion compileSdk
    defaultConfig {
        applicationId "de.mobacomp.android.freightweight"
        minSdkVersion minSdk
        targetSdkVersion targetSdk
        versionCode appPlayStoreVersion
        versionName "$appVersion"
        // Enabling multidex support.
        multiDexEnabled false
        resValue "string", "app_version_name", versionName
        signingConfig signingConfigs.configRelease
        testInstrumentationRunner = 'android.support.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled true
            resValue "string", "app_name", appLabel
            resValue "string", "mobile_ads_id", "ca-app-pub-xyz"
            resValue "string", "ad_unit_banner_1", "ca-app-pub-xyz"
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.configRelease
            resValue "string", "APP_FILEPROVIDER", defaultConfig.applicationId + ".release" + ".fileprovider"
            // lets generate a proper file name
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    def fileName = applicationId + "-v" + appVersion + "_" + versionCode + ".apk"
                    outputFileName = new File("release", fileName)

                }
            }
        }
        debug {
            minifyEnabled false
            resValue "string", "app_name", "FW-debug"
            resValue "string", "mobile_ads_id", "ca-app-pub-xyz"
            resValue "string", "ad_unit_banner_1", "ca-app-pub-xyz"
            applicationIdSuffix ".debug"
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.configRelease
            resValue "string", "APP_FILEPROVIDER", defaultConfig.applicationId + ".debug" + ".fileprovider"
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
        jumboMode true
    }
    productFlavors {
    }
    buildToolsVersion '28.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'

    // GLide
    implementation "com.github.bumptech.glide:glide:$glide_version"

    // Firebase stuff
    implementation "com.firebaseui:firebase-ui-database:4.2.1"
    implementation "com.firebaseui:firebase-ui-auth:4.2.1"
    implementation "com.google.firebase:firebase-core:16.0.5"
    implementation "com.google.firebase:firebase-database:16.0.5"
    implementation "com.google.firebase:firebase-storage:16.0.5"
    implementation "com.google.firebase:firebase-auth:16.0.5"

    implementation 'com.google.firebase:firebase-ml-vision:18.0.1'
    implementation 'com.google.firebase:firebase-ml-vision-image-label-model:17.0.2'

    // Google Play stuff
    implementation "com.google.android.gms:play-services-auth:16.0.1"
    implementation "com.google.android.gms:play-services-base:16.0.1"
    implementation "com.google.android.gms:play-services-analytics:16.0.5"
    implementation "com.google.android.gms:play-services-cast-framework:16.1.0"
    implementation 'com.google.gms:google-services:4.1.0'

    implementation "com.android.support:preference-v14:28.0.0"
    implementation "com.android.support:mediarouter-v7:28.0.0"
    implementation "com.android.support:design:28.0.0"
    implementation "com.android.support:customtabs:28.0.0"
    implementation "com.android.support:cardview-v7:28.0.0"
    implementation "com.android.support:recyclerview-v7:28.0.0"
    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support:appcompat-v7:28.0.0"
    implementation "com.android.support:palette-v7:28.0.0"
    implementation "com.android.support.constraint:constraint-layout:1.1.3"
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
}



apply plugin: 'com.google.gms.google-services'

最后,我记录了一个简短的测试(也尝试了更长的测试,同样的错误)

package de.mobacomp.android.freightweight;


import androidx.test.espresso.ViewInteraction;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
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.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainFragmentActivityTest {

    @Rule
    public ActivityTestRule<MainFragmentActivity> mActivityTestRule = new ActivityTestRule<>(MainFragmentActivity.class);

    @Test
    public void mainFragmentActivityTest() {
        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(7000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction button = onView(
                allOf(withId(R.id.buttonContinueStartApp),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        0),
                                1),
                        isDisplayed()));
        button.check(matches(isDisplayed()));

        ViewInteraction button2 = onView(
                allOf(withId(R.id.buttonContinueStartApp),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        0),
                                1),
                        isDisplayed()));
        button2.check(matches(isDisplayed()));

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction overflowMenuButton = onView(
                allOf(withContentDescription("More options"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.action_bar),
                                        2),
                                0),
                        isDisplayed()));
        overflowMenuButton.perform(click());

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction appCompatTextView = onView(
                allOf(withId(R.id.title), withText("Datenschutz Hinweise"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        appCompatTextView.perform(click());

        pressBack();

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction overflowMenuButton2 = onView(
                allOf(withContentDescription("More options"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.action_bar),
                                        2),
                                0),
                        isDisplayed()));
        overflowMenuButton2.perform(click());

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction appCompatTextView2 = onView(
                allOf(withId(R.id.title), withText("Settings"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        appCompatTextView2.perform(click());

        ViewInteraction linearLayout = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.recycler_view),
                                childAtPosition(
                                        withId(android.R.id.list_container),
                                        0)),
                        1),
                        isDisplayed()));
        linearLayout.perform(click());

        ViewInteraction linearLayout2 = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.recycler_view),
                                childAtPosition(
                                        withId(android.R.id.list_container),
                                        0)),
                        3),
                        isDisplayed()));
        linearLayout2.perform(click());

        ViewInteraction linearLayout3 = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.recycler_view),
                                childAtPosition(
                                        withId(android.R.id.list_container),
                                        0)),
                        4),
                        isDisplayed()));
        linearLayout3.perform(click());
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

“ androidDependencies”的输出很大,因此这里是其链接。 Link to the file on my webserver

按照建议删除了“'com.google.gms:google-services:4.1.0'”,保持不变。

2 个答案:

答案 0 :(得分:0)

删除for(int i=0; i<ar.length; i++) { if(ar[i] % 2 == 0) continue; for(int j=i; j<ar.length; j++) { if(ar[j] % 2 == 0) continue; if(ar[i] > ar[j]) { int temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } } ,这是完全错误的。

问题可能是您将implementation 'com.google.gms:google-services:4.1.0'com.android.support一起使用 并且androidx.testtestInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"的错误测试运行程序。

app没有单一的androidx.test依赖关系-除了androidx目录中的某些.jar之外。运行libs来查看不合算的./gradlew app:dependencies > ./dependencies.txt && gedit ./dependencies.txt的来源。

答案 1 :(得分:0)

就这样,我可以将其标记为完成:

我已经在Studio中使用重构到androidx了。只需让它再次运行,它又发现了11个要转换的零件,现在就开始测试。我假设Studio中的Recording Espresso Test功能没有通过喷射转换为androidx,因此会产生冲突的配置。

所以现在我可以记录Espresso测试并进行编译。

感谢您的帮助。