我觉得有点别扭约回答我的奖金问题,但在这里它是...
我已经搜查高和低就这个问题和不能相信没有任何地方公布答案。我走得很近。我肯定可以运行现在跨越活动的测试,但是我的实现似乎有一些时间问题,测试并不总是可靠地传递。这是我知道的成功进行多项活动测试的唯一例子。希望我的提取和匿名它没有引入错误。这是一个简单的测试,其中我键入用户名和密码输入到一个登录活动,然后观察适当的欢迎消息在不同的“欢迎”活动所示:
package com.mycompany;
import android.app.*;
import android.content.*;
import android.test.*;
import android.test.suitebuilder.annotation.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.*;
import static com.mycompany.R.id.*;
public class LoginTests extends InstrumentationTestCase {
@MediumTest
public void testAValidUserCanLogIn() {
Instrumentation instrumentation = getInstrumentation();
// Register we are interested in the authentication activiry...
Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(AuthenticateActivity.class.getName(), null, false);
// Start the authentication activity as the first activity...
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(instrumentation.getTargetContext(), AuthenticateActivity.class.getName());
instrumentation.startActivitySync(intent);
// Wait for it to start...
Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
assertThat(currentActivity, is(notNullValue()));
// Type into the username field...
View currentView = currentActivity.findViewById(username_field);
assertThat(currentView, is(notNullValue()));
assertThat(currentView, instanceOf(EditText.class));
TouchUtils.clickView(this, currentView);
instrumentation.sendStringSync("MyUsername");
// Type into the password field...
currentView = currentActivity.findViewById(password_field);
assertThat(currentView, is(notNullValue()));
assertThat(currentView, instanceOf(EditText.class));
TouchUtils.clickView(this, currentView);
instrumentation.sendStringSync("MyPassword");
// Register we are interested in the welcome activity...
// this has to be done before we do something that will send us to that
// activity...
instrumentation.removeMonitor(monitor);
monitor = instrumentation.addMonitor(WelcomeActivity.class.getName(), null, false);
// Click the login button...
currentView = currentActivity.findViewById(login_button;
assertThat(currentView, is(notNullValue()));
assertThat(currentView, instanceOf(Button.class));
TouchUtils.clickView(this, currentView);
// Wait for the welcome page to start...
currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
assertThat(currentActivity, is(notNullValue()));
// Make sure we are logged in...
currentView = currentActivity.findViewById(welcome_message);
assertThat(currentView, is(notNullValue()));
assertThat(currentView, instanceOf(TextView.class));
assertThat(((TextView)currentView).getText().toString(), is("Welcome, MyUsername!"));
}
}
此代码是显然不是很可读。其实我已经提取它与英国类似的API一个简单的库,所以我只能说这样的事情:
type("myUsername").intoThe(username_field);
click(login_button);
我测试过的约4活动的深度和很满意的办法,虽然工作正如我所说,似乎有一个偶然的时机问题,我还没有完全想到。我仍然有兴趣听到其他各种活动的测试方法。
作为安卓4.1的,现在有来自Android的一个新的测试框架,支持测试跨活动,甚至整个系统:http://developer.android.com/tools/testing/testing_ui。html –
[Robotium](https://code.google.com/p/robotium/)也适合这种需求,并且只需几行。 – Dori