gpt4 book ai didi

Android Espresso 设置错误,或者工作不稳定?

转载 作者:太空狗 更新时间:2023-10-29 13:21:31 25 4
gpt4 key购买 nike

我实现 android espresso 测试已经一个星期了。真正的东西是什么——实现服务器调用并用 Espresso 等待它。这称为空闲资源调用,我们必须遵循非常简单的规则。其实我找到了解决方案,但结果令人震惊 - 只有我做评论线我才成功

Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit"));
Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123"));

并将它们替换为“huck”:

final EditText email = (EditText) act.findViewById(R.id.email);
final EditText password = (EditText) act.findViewById(R.id.password);
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
email.setText("Engineer");
password.setText("2342");
}
});

..在模拟调用服务器后单击启动新 Activity 的按钮之前。这是我的全部文件:构建.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "shoppinglist.kizema.anton.testappespresso"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/DEPENDENCIES'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'

// App's dependencies, including test
compile 'com.android.support:support-annotations:21.0.3'

// Testing-only dependencies
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
}

LoginActivity(第一个 Activity ):

//set up initial listener
private void initLoginHelper(){
loginHelper = new Server() {
@Override
public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) {
//ask server
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

//done
Intent intent = new Intent(LoginActivity.this, SecondActivity.class);
startActivityForResult(intent, 0);
}
};
}

//onButtonClick handler
public void btnLogInSuka(View v) {
performLogin();
}
void performLogin() {
new Thread(new Runnable() {
@Override
public void run() {
loginHelper.login(emailParam,codeParam,phoneParam,passwordParam,false);
}
}).start();
}

和 AplicationTest.java(espresso 测试): @大测试 公共(public)类 ApplicationTest 扩展 ActivityInstrumentationTestCase2 {

public ApplicationTest() {
super(LoginActivity.class);
}

CountingIdlingResource idleRes;

@Override
public void setUp() throws Exception {
super.setUp();
getActivity();

idleRes = new CountingIdlingResource("server");
Espresso.registerIdlingResources(idleRes);
}

public void testSample(){
final LoginActivity act = (LoginActivity) getCurrentActivity();
Server aHelper = act.getUserHelper();

MyUserHelperExternalIdleRes helper2 = new MyUserHelperExternalIdleRes(idleRes, aHelper);
act.setUserHelper(helper2);


//if comment this and uncomment next two lines we receive PerformException
final EditText email = (EditText) act.findViewById(R.id.email);
final EditText password = (EditText) act.findViewById(R.id.password);
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
email.setText("Engineer");
password.setText("2342");
}
});

// Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit"));
// Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123"));

Espresso.closeSoftKeyboard();
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withId(R.id.secondActivityOpened)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
Espresso.pressBack();

Espresso.closeSoftKeyboard();
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());
}

Activity getCurrentActivity() {
getInstrumentation().waitForIdleSync();
final Activity[] activity = new Activity[1];
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = Iterables.getOnlyElement(activites);
}});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return activity[0];
}

class MyUserHelperExternalIdleRes implements Server {
private Server aHelper;
private CountingIdlingResource udleRes;

public MyUserHelperExternalIdleRes(CountingIdlingResource udleRes, Server aHelper) {
this.aHelper = aHelper;
this.udleRes = udleRes;
}

@Override
public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) {

udleRes.increment();
try {
aHelper.login(email,code, phone,password,loginByPhoneNumber);
} finally {
udleRes.decrement();
}
}
}

所以,如果我们实际进行基本的 Espresso 操作: Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123"));

我们收到 PerformException:无法通过 ID 找到按钮。如果我们做一个 huck,(在 UI 线程上设置 Runnable)我们就可以成功地完成这个简单的演示。在我的主应用程序中, Espresso 还有其他错误(上面写的“哈克”不起作用,我们收到了同样的错误)。可以肯定的是,我遇到了一些非常棘手的错误,我想 project(espresso ) 设置有问题 - 我对 gradle 太糟糕了。请帮我解决这个问题,或者提供一个带有 espresso 测试的 android studio 示例应用程序的链接(我发现没有,所有应用程序都配置错误(没有 gradle),并且在将它们导入 android studio 之后,我无法使用

gradlew connectedAndroidTest

最佳答案

我遇到了同样的问题,我认为主要问题是,按钮打开了一个新的缩进,但似乎仅此一项并不会导致错误,这很奇怪,它只发生在你在调用 click() 之前在 EditText 上键入文本。

P.S:我设法解决了这个问题,似乎我遇到了一些双重依赖问题,一旦我解决了这个问题,测试就没有任何问题,也不需要做任何变通。

我的 build.gradle 部分的 espresso 是这样结束的:

dependencies {
repositories {
mavenCentral()
}
// Espresso
compile 'org.apache.commons:commons-lang3:3.1'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
exclude group: 'com.google.guava'
exclude module: 'espresso-idling-resource'
}
androidTestCompile('com.android.support.test:testing-support-lib:0.1') {
exclude group: 'com.google.guava'
exclude module: 'espresso-idling-resource'
}
}

我还在依赖项之前添加了这个:

configurations {
androidTestCompile.exclude group: 'com.android.support'
androidTestCompile.exclude module: 'support-v4'
androidTestCompile.exclude module: 'appcompat-v7'
}

关于Android Espresso 设置错误,或者工作不稳定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28016562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com