我对 Espresso 不太有经验,但我终于成功地运行了它。
我有一个应用程序需要通过 Proguard 缩小才能处于 56K 方法之下。
该应用程序以 3 秒的动画开始,因此我需要等到该动画结束才能继续。这就是我尝试用该方法做的事情waitFor(long delay)
我尝试将此方法作为独立类、内部类甚至项目类=相同的错误。
这是 Proguard 文件:
android {
...
compileSdkVersion ANDROID_BUILD_SDK_VERSION
buildToolsVersion ANDROID_BUILD_TOOLS_VERSION
productFlavors {
// Define separate dev and prod product flavors.
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the application.
minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION
}
}
defaultConfig {
applicationId "fr.xs.app.y"
targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION
minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION
versionCode ANDROID_BUILD_VERSION_CODE
versionName ANDROID_BUILD_APP_VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
// point sur le build de test
testBuildType = "validation"
buildTypes {
release {
debuggable false
ext.enableCrashlytics = true
renderscriptOptimLevel 3
signingConfig android.signingConfigs.release
zipAlignEnabled true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules-release.pro'
}
debug {
debuggable true
renderscriptOptimLevel 3
applicationIdSuffix ".debug"
versionNameSuffix "debug"
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-debug.pro', 'proguard-rules-dontobfuscate.pro'
}
validation.initWith(debug)
validation {
debuggable false
renderscriptOptimLevel 3
applicationIdSuffix ".validation"
versionNameSuffix "validation"
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-debug.pro', 'proguard-rules-dontobfuscate.pro'
testProguardFile('proguard-rules-test.pro')
}
}
...
}
混淆器配置
以下是 Proguard 文件(调试和测试)
-保留类 fr.xs.app.y.** { *; }
-不要混淆
我检查了这个类在mapping.txt(build/output/mapping/androidTest/dev)中,并且没有转换为另一个字符串。
这是测试类:
import android.app.Activity;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingPolicies;
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.core.deps.guava.collect.Iterables;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.Stage;
import android.test.suitebuilder.annotation.LargeTest;
import com.squareup.spoon.Spoon;
import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.concurrent.TimeUnit;
import fr.xs.app.y.Activity.SplashScreenActivity;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.allOf;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {
@Rule
public ActivityTestRule<SplashScreenActivity> mActivityRule = new ActivityTestRule<>(SplashScreenActivity.class);
@Test
public void seeHomeScreen() throws Throwable {
// SpashScreen
takeScreenShot("Chargement Application");
onView(allOf(withText("Réveille l'amateur qui est en toi"),
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
.check(matches(isCompletelyDisplayed()));
takeScreenShot("Splash Screen en cours");
waitForDelay(3000);
takeScreenShot("Splash Screen à la fin");
// HomeScree
onView(allOf(withText("Nouvelle Dégustation"),
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
.check(matches(isCompletelyDisplayed()));
takeScreenShot("Home Screen");
// Preference
onView(withId(R.id.iv_seeParameters)).perform(click());
onView(withId(R.id.content_frame_sign_unsign)).check(matches(isDisplayed()));
takeScreenShot("Preferences Screen");
Activity getCurrentActivity() throws Throwable {
getInstrumentation().waitForIdleSync();
final Activity[] activity = new Activity[1];
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
java.util.Collection activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = (Activity) Iterables.getOnlyElement(activites);
}
});
return activity[0];
}
private static void waitForDelay(long waitingTime) {
// Make sure Espresso does not time out
IdlingPolicies.setMasterPolicyTimeout(waitingTime * 2, TimeUnit.MILLISECONDS);
IdlingPolicies.setIdlingResourceTimeout(waitingTime * 2, TimeUnit.MILLISECONDS);
// Now we wait
IdlingResource idlingResource = new ElapsedTimeIdlingResource(waitingTime);
Espresso.registerIdlingResources(idlingResource);
// Clean up
Espresso.unregisterIdlingResources(idlingResource);
}
private void takeScreenShot(String title) throws Throwable {
String string = StringUtils.stripAccents(title).replace(" ", "_");
Spoon.screenshot(getCurrentActivity(), string);
}
static class ElapsedTimeIdlingResource implements IdlingResource {
private final long startTime;
private final long waitingTime;
private ResourceCallback resourceCallback;
public ElapsedTimeIdlingResource(long waitingTime) {
this.startTime = System.currentTimeMillis();
this.waitingTime = waitingTime;
}
@Override
public String getName() {
return ElapsedTimeIdlingResource.class.getName() + ":" + waitingTime;
}
@Override
public boolean isIdleNow() {
long elapsed = System.currentTimeMillis() - startTime;
boolean idle = (elapsed >= waitingTime);
if (idle) {
resourceCallback.onTransitionToIdle();
}
return idle;
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
}
}
这是日志:
04-26 10:15:52.322 ? D/LifecycleMonitor:生命周期状态更改:
fr.xs.app.y.Activity.SplashScreenActivity@6f8513d
于:暂停 04-26 10:15:52.322 ? D/LifecycleMonitor:生命周期状态
改变:
fr.xs.app.y.Activity.SplashScreenActivity@6f8513d
于: 04-26 10:15:52.702 恢复? D/ActivityManager:bindService
callerProcessName:fr.xs.app.y.validation,
calleePkgName:com.google.android.gms,操作:
com.google.android.gms.analytics.service.START 04-26 10:15:53.012 ?
I/ActivityManager:显示
fr.xs.app.y.validation/fr.xs.app.y.Activity.SplashScreenActivity:
+1s745ms(总计+36m45s584ms)04-26 10:15:53.762? I/时间线:时间线:Activity_windows_visible id:ActivityRecord{2b3eacb1 u0
fr.xs.app.y.validation/fr.xs.app.y.Activity.SplashScreenActivity
t5952} 时间:259410459 04-26 10:15:54.592 ? I/art:拒绝重新初始化
以前不及格的班级
java.lang.Class
04-26 10:15:54.622 ?我/测试运行程序:失败:
请参阅HomeScreen(fr.xs.app.y.HelloWorldEspressoTest)
04-26 10:15:54.622 ? I/TestRunner:java.lang.NoClassDefFoundError:
fr.xs.app.y.HelloWorldEspressoTest$ElapsedTimeIdlingResource
在 fr.xs.app.y.HelloWorldEspressoTest.waitForDelay(HelloWorldEspressoTest.java:166)
在 fr.xs.app.y.HelloWorldEspressoTest.seeHomeScreen(HelloWorldEspressoTest.java:56)
在 java.lang.reflect.Method.invoke(本机方法)
在 java.lang.reflect.Method.invoke(Method.java:372)
在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
在 android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
在 android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
在 org.junit.rules.RunRules.evaluate(RunRules.java:20)
在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
在 org.junit.runners.ParentRunner.run(ParentRunner.java:363)
在 org.junit.runners.Suite.runChild(Suite.java:128)
在 org.junit.runners.Suite.runChild(Suite.java:27)
在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
在 org.junit.runners.ParentRunner.run(ParentRunner.java:363)
在 org.junit.runner.JUnitCore.run(JUnitCore.java:137)
在 org.junit.runner.JUnitCore.run(JUnitCore.java:115)
在 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1873)
04-26 10:15:54.622 ?我/测试运行者:完成:
请参阅HomeScreen(fr.xs.app.y.HelloWorldEspressoTest)
04-26 10:15:54.642 ? D/LifecycleMonitor:生命周期状态更改:
fr.xs.app.y.Activity.SplashScreenActivity@6f8513d
于:暂停 04-26 10:15:54.672 ? I/TestRunner:开始:
初始化错误(fr.xs.app.y.suites.system.LogSupportUser)
04-26 10:15:54.732 ?我/测试运行程序:失败:
初始化错误(fr.xs.app.y.suites.system.LogSupportUser)
04-26 10:15:54.732 ?我/测试运行者:完成:
初始化错误(fr.xs.app.y.suites.system.LogSupportUser)
04-26 10:15:55.692 ? D/KidsModeInstallReceiver:onReceive 意图数据
= 包:fr.xs.app.y.validation.test 04-26 10:15:56.752 ? E/SPPClientService:[PackageInfoChangeReceiver]
[handlePkgRemovedEvent] 包名称:
fr.xs.app.y.validation.test,真,假 04-26
10:15:56.932? D/LifecycleMonitor:生命周期状态更改:
fr.xs.app.y.Activity.SplashScreenActivity@6f8513d
于:停止 04-26 10:15:56.932 ? D/LifecycleMonitor:生命周期状态
改变:
fr.xs.app.y.Activity.SplashScreenActivity@6f8513d
于: 毁坏 04-26 10:15:57.302 ? I/ActivityManager:强制停止
fr.xs.app.y.validation appid=10582 用户=0:
完成安装
到目前为止还没有办法运行它!