Android Marshmallow引入的新权限方案要求在运行时检查特定权限,这意味着需要根据用户是否拒绝或允许访问来提供不同的流.
当我们使用Espresso在我们的应用程序上运行自动UI测试时,我们如何模拟或更新权限状态以测试不同的场景?
permissions android android-espresso android-6.0-marshmallow
我正在寻求适用于Android的UI自动化测试框架,我偶然发现了UI Automator和Espresso,这是我感到困惑的部分-
Android只是框架所以我的疑问/怀疑是 -
UI Automator和之间的主要区别是Espresso什么?以下是我的Espresso测试用例之一.
public void testLoginAttempt() {
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@krossover.com"));
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));
Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
// AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP.
// Clicking launches a new activity that shows the text entered above. You don't need to do
// anything special to handle the activity transitions. Espresso takes care of waiting for the
// new activity to be resumed and its view hierarchy to be laid out.
Espresso.onView(ViewMatchers.withId(R.id.action_logout))
.check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));
}
Run Code Online (Sandbox Code Playgroud)
目前我所做的是检查新活动(R.id.action_logout)中的视图是否可见.如果可见,我将假设活动成功打开.但它似乎没有像我预期的那样工作.有没有更好的方法来检查是否成功启动了新活动而不是检查该活动中的视图是否可见?谢谢
在我的测试中,在一次操作之后,有两种可能的视图可以出现并且它们都是正确的.如何检查是否显示其中一个视图.对于我可以检查的单个视图是Displayed().但如果其他视图可见则会失败.如果显示这两个视图中的任何一个,我想通过测试.
onMyButton.perform(click());
onMyPageOne.check(matches(isDisplayed())); //view 1
or
onMyPageTwo.check(matches(isDisplayed())); //view 2
Run Code Online (Sandbox Code Playgroud)
之后,执行单击MyButton,预计会出现任何一个视图(1或2),但不会同时出现.不确定哪一个会被显示.如何检查是否显示其中任何一个?
我运行android espresso测试时遇到错误:
com.google.android.apps.common.testing.ui.espresso.PerformException:在视图'上执行'单击'时出错,ID为:<2131034173>'.
我的代码很简单:
onView(withId(R.id.btn)).perform(click());
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有错误:
onView(withId(R.id.btn)).check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)
我找不到它为什么会发生的原因.
我正在使用espresso-contrib来对a执行操作RecyclerView,并且它可以正常工作,例如:
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); //click on first item
Run Code Online (Sandbox Code Playgroud)
我需要对它进行断言.像这样的东西:
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, check(matches(withText("Test Text"))));
Run Code Online (Sandbox Code Playgroud)
但是,因为RecyclerViewActions当然是期待一个动作,它会说错误的第二个参数类型.RecyclerViewAssertionsespresso-contrib 上没有.
有没有办法在回收站视图上执行断言?
使用Espresso和Hamcrest,
如何计算recyclerView中可用的项目编号?
例子:我想检查特定的RecyclerView中是否显示了5个项目(必要时滚动).
有gridView有一些图像.gridView的单元格来自相同的预定义布局,具有相同的id和desc.
R.id.item_image == 2131493330
onView(withId(is(R.id.item_image))).perform(click());
Run Code Online (Sandbox Code Playgroud)
由于网格中的所有单元都具有相同的id,因此它得到了AmbiguousViewMatcherException.如何选择第一个或其中任何一个?谢谢!
android.support.test.espresso.AmbiguousViewMatcherException:'id:is <2131493330>'匹配层次结构中的多个视图.问题视图在下方标有"****MATCHES****".
+ -------------> ImageView {id = 2131493330,res-name = item_image,desc = Image,visibility = VISIBLE,width = 262,height = 262,has-focus = false,has -focusable = false,has-window-focus = true,is-clickable = false,is-enabled = true,is-focused = false,is-focusable = false,is-layout-requested = false,is-selected = false ,root-is-layout-requested = false,has-input-connection = false,x = 0.0,y = 0.0}****MATCHES****
+ -------------> ImageView {id = 2131493330,res-name = item_image,desc = Image,visibility = VISIBLE,width = 262,height = 262,has-focus = false,has -focusable = false,has-window-focus = true,is-clickable = false,is-enabled = …
我试图通过以下方式点击一些Espresso测试中的主页图标:
onView(withId(android.R.id.home)).perform(click());
Run Code Online (Sandbox Code Playgroud)
这适用于Android> 3.0 - 但旧版本失败,appcompat因为这个元素似乎没有使用此ID.做我想做的事情的好方法是什么?
如果测试跨越多个活动,是否有办法获得当前活动?
getActivtiy()方法只提供一个用于启动测试的活动.
我尝试过类似下面的内容,
public Activity getCurrentActivity() {
Activity activity = null;
ActivityManager am = (ActivityManager) this.getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
try {
Class<?> myClass = taskInfo.get(0).topActivity.getClass();
activity = (Activity) myClass.newInstance();
}
catch (Exception e) {
}
return activity;
}
Run Code Online (Sandbox Code Playgroud)
但我得到了空对象.