【发布时间】:2015-03-16 23:34:34
【问题描述】:
我已经实施了一周的 android 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");
}
});
..在模拟调用服务器后单击启动新活动的按钮之前。 这是我的全部文件: build.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(第一个活动):
//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(浓缩咖啡测试): @大测试 公共类 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 : can not find button by id。 如果我们做一个哈克,(在 UI 线程上设置 Runnable)我们就可以成功完成这个简单的演示。 在我的主应用程序中,espresso 还有其他错误(上面写的“huck”不起作用,我们收到同样的错误)。当然我有一些非常棘手的错误,我想项目(espresso)设置是错误的 - 我对 gradle 太糟糕了。 请帮我解决这个问题,或者提供一个带有 espresso 测试的 android studio 示例应用程序的链接(我发现没有,所有应用程序都配置错误(没有 gradle),并且在将它们导入到 android studio 后,我无法使用
gradlew connectedAndroidTest
【问题讨论】:
-
我已经上传了示例小应用程序,它显示了这个问题 - github.com/spartacus777/android-espresso-strange-work