我想做一个简单的 UI 测试,按下一个按钮并启动一个 Activity 。
我试过基于this documentation. (安卓开发者)
SplashActivity 代码:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
findViewById(R.id.launch_register_activity_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SplashActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
测试代码:
@RunWith(AndroidJUnit4.class)
public class SimpleIntentTest {
@Rule
public IntentsTestRule<SplashActivity> intentsRule = new IntentsTestRule<>(SplashActivity.class);
@Test
public void newActivityLaunchingTest() {
onView(withId(R.id.launch_register_activity_btn)).perform(click());
Log.i("register-package_name", RegisterActivity.class.getPackage().toString());
Log.i("register-class_name", RegisterActivity.class.getName());
Log.i("register-short_name", RegisterActivity.class.getSimpleName());
intended(allOf(
hasComponent(hasShortClassName(".RegisterActivity")),
toPackage("re.example.common")
));
}
}
日志:
01-22 13:37:51.615: I/register-package_name(25292): package re.example.common, Unknown, version 0.0
01-22 13:37:51.615: I/register-class_name(25292): re.example.common.RegisterActivity
01-22 13:37:51.615: I/register-short_name(25292): RegisterActivity
包结构:
测试输出:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.
IntentMatcher: (has component: has component with: class name: an instance of java.lang.String package name: an instance of java.lang.String short class name: is ".RegisterActivity" and resolvesTo: re.example.common)
Matched intents:[]
无论我如何尝试,测试都无法验证我是否启动了 RegisterActivity。测试失败。
据我所知,我所做的与上面链接中的 Espresso 测试示例完全一样。
我做错了什么?
我也试过:
<强>1。删除 . (点)来自“.RegisterActivity”
intended(allOf(
hasComponent(hasShortClassName("RegisterActivity")),
toPackage("re.example.common")
));
<强>2。删除 . (点)来自 ".RegisterActivity" 和 toPackage("re.example.common")
intended(allOf(
hasComponent(hasShortClassName("RegisterActivity"))
));
我的 RegisterActivity 的包名称是 "re.example.common.RegisterActivity"。 简称 名称是“RegisterActivity”。不是吗?
为什么测试失败?
这是怎么回事,我有什么误解或误解?
提前致谢。
最佳答案
Application id (app package name is) is: re.example
(source gradle)
Activity 的全名是:re.example.common.RegisterActivity
这样,hasShortClassName
意味着 .common.RegisterActivity
作为 shortName 而不是 .RegisterActivity
也不是 注册 Activity
嗯。
关于android - 使用 Espresso 测试启动 Activity Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48382075/