2016-01-21 13 views
20

Tôi đã dành cả ngày để thiết lập các bài kiểm tra thiết bị Junit4 với Espresso, nhưng dường như không thể có được bước cuối cùng đó. Không có vấn đề gì tôi làm nó sẽ không nhận ra phương pháp onView(). Tôi đã thử nhiều phiên bản SDK/hỗ trợ-lib và cho đến nay không có gì. Tôi đã đi qua tất cả các hướng dẫn thiết lập google và nhiều bài viết stackoverflow, biến đây là hy vọng cuối cùng của tôi.Kiểm tra giao diện người dùng Espresso không nhận ra onView()

Gradle phiên bản: com.android.tools.build:gradle:1.5.0

Gradle file:

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

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 
    useLibrary 'org.apache.http.legacy' 

    defaultConfig { 
     applicationId "lv.my.android" 
     minSdkVersion 9 
     targetSdkVersion 22 
     testApplicationId "lv.my.android.tests" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 

    packagingOptions { 
     exclude 'LICENSE.txt' 
     exclude 'META-INF/DEPENDENCIES' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/NOTICE' 
    } 

    compileOptions { 
     sourceCompatibility = 'VERSION_1_7' 
     targetCompatibility = 'VERSION_1_7' 
    } 

    signingConfigs { 
     beta { 
      storeFile file("beta.keystore") 
      storePassword "betabuild" 
      keyAlias "key" 
      keyPassword "betabuild" 
     } 
     release 
    } 

    buildTypes { 
     debug { 
      debuggable true 
      applicationIdSuffix '.debug' 
      versionNameSuffix '-DEV' 
      minifyEnabled false 
     } 

     beta { 
      debuggable true 
      applicationIdSuffix '.beta' 
      versionNameSuffix '-BETA' 
      signingConfig signingConfigs.beta 
      minifyEnabled false 
     } 

     release { 
      minifyEnabled false 
     } 
    } 
} 

dependencies { 
    compile files('src/main/libs/guice-3.0-no_aop.jar') 
    compile files('src/main/libs/javax.inject-1.jar') 
    compile files('src/main/libs/roboguice-2.0.jar') 
    compile files('src/main/libs/junit-4.11.jar') 
    compile files('src/main/libs/hamcrest-core-1.3.jar') 
    compile files('src/main/libs/GeoLib.jar') 
    compile files('src/main/libs/GeoPolygons.jar') 
    compile files('src/main/libs/universal-image-loader-1.9.4.jar') 
    compile files('src/main/libs/javax.annotation-3.2-b06-sources.jar') 
    compile 'uk.co.chrisjenx:calligraphy:2.1.0' 
    compile 'com.squareup:otto:1.3.5' 
    compile 'com.google.android.gms:play-services:6.5.87' 
    compile 'com.android.support:support-annotations:23.0.1' 
    compile 'com.android.support:appcompat-v7:23.0.1' 
    compile 'com.android.support:support-v4:23.0.1' 
    compile 'com.android.support:palette-v7:23.0.1' 
    compile 'com.google.code.findbugs:jsr305:2.0.1' 
    compile 'com.nineoldandroids:library:2.4.0' 
    compile 'pl.charmas.android:android-reactive-location:[email protected]' 
    compile 'io.reactivex:rxjava:1.0.3' 
    compile files('src/main/libs/FlurryAnalytics-6.1.0.jar') 
    compile 'com.github.castorflex.smoothprogressbar:library:1.1.0' 



    androidTestCompile 'com.android.support.test:testing-support-lib:0.1' 
    androidTestCompile'com.android.support.test:runner:0.4.1' 
    // Set this dependency to use JUnit 4 rules 
    androidTestCompile'com.android.support.test:rules:0.4' 
    // Set this dependency to build and run Espresso tests 
    androidTestCompile'com.android.support.test.espresso:espresso-core:2.2.1' 
    testCompile 'junit:junit:4.12' 
    testCompile "org.mockito:mockito-core:1.9.5" 
} 

thử nghiệm của tôi (nằm trong src/androidTest/java/lv/my/test)

package lv.my.test; 

import android.support.test.rule.ActivityTestRule; 
import android.support.test.runner.AndroidJUnit4; 
import android.test.suitebuilder.annotation.LargeTest; 
import lv.my.android.activities.LoginActivity; 

import org.junit.Before; 
import org.junit.Rule; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

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

    private String mStringToBetyped; 

    @Rule 
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(LoginActivity.class); 

    @Before 
    public void initValidString() { 

    } 

    @Test 
    public void changeText_sameActivity() { 
//  onView not recognized here 

    } 
} 

PS. Tôi sử dụng roboguice có thể đó là vấn đề?

+4

hãy thử thêm câu lệnh nhập là 'import static android.support.test.espresso.Espresso.onView;' – Raghunandan

Trả lời

44

Bạn nên hoặc sử dụng một khẩu tĩnh:

import static android.support.test.espresso.Espresso.onView; 

hoặc

import android.support.test.espresso.Espresso; 

và gọi nó theo cách sau

Espresso.onView() 
+6

Wow, nó đã hoạt động . Không có ví dụ hoặc hướng dẫn nào đề cập đến điều này, nó chỉ cho thấy rằng nó sẽ làm việc ra khỏi hộp. Vì vậy, tôi đã làm việc này 'Espresso.onView (ViewMatchers.withId (R.id.registerButton))' – somerandomusername

+2

Vậy tại sao nó không nói bất cứ điều gì về điều này ở đây: http://developer.android.com/training/testing/ ui-testing/espresso-testing.html # build – somerandomusername

+8

Đồng ý, sẽ tốt cho họ đề cập đến hàng nhập khẩu. – Egor

8

Để mở rộng những gì Egor đã đề nghị, trong Android Studio, sau khi bạn nhận được văn bản màu đỏ tại onView(), chỉ cần nhấn alt + nhập khi bóng đèn đỏ bật lên. Sau đó, chọn nhập tĩnh.

+4

+1 công trình này. Một đột quỵ quan trọng hấp dẫn. Tôi đang gặp vấn đề với withId là tốt và đã làm như bạn đã đề cập và nó đề nghị để nhập khẩu tĩnh với 'matchers.ViewMatchers'. Cảm ơn rất nhiều! –

0

Tiếp tục viết. Android Studio sẽ đề xuất nhập khẩu Espresso cần thiết, từ 'bên trong ra'. Ví dụ: onView (withId()) Đầu tiên sẽ phát hiện withId(), sau đó onView()

Các vấn đề liên quan