我对Android上的Espresso很新,我遇到了以下问题: 我希望Espresso在按钮上执行longclick(或其他东西..),当按下按钮时,我想检查不同视图的状态。
(或多或少)伪代码:
onView(withId(button_id)).perform(pressButtonDown());
onView(withId(textBox_id)).check(matches(withText("Button is pressed")));
onView(withId(button_id)).perform(releaseButton());
我尝试用MotionEvents.sendDown()和.sendUp()编写2个自定义Taps,PRESS和RELEASE,但是没有让它工作。 如果这是正确的路径,我可以发布到目前为止我已经获得的代码。
修改
public class PressViewActions
{
public static ViewAction pressDown(){
return new GeneralClickAction(HoldTap.DOWN, GeneralLocation.CENTER, Press.THUMB);
}
public static ViewAction release() {
return new GeneralClickAction(HoldTap.UP, GeneralLocation.CENTER, Press.THUMB);
}
}
Tappers的代码:
public enum HoldTap implements Tapper {
DOWN {
@Override
public Tapper.Status sendTap(UiController uiController, float[] coordinates, float[] precision)
{
checkNotNull(uiController);
checkNotNull(coordinates);
checkNotNull(precision);
DownResultHolder res = MotionEvents.sendDown(uiController, coordinates, precision);
ResultHolder.setController(uiController);
ResultHolder.setResult(res);
return Status.SUCCESS;
}
},
UP{
@Override
public Tapper.Status sendTap(UiController uiController, float[] coordinates, float[] precision)
{
DownResultHolder res = ResultHolder.getResult();
UiController controller = ResultHolder.getController();
try {
if(!MotionEvents.sendUp(controller, res.down))
{
MotionEvents.sendCancel(controller, res.down);
return Status.FAILURE;
}
}
finally {
//res.down.recycle();
}
return Status.SUCCESS;
}
}
}
我得到的错误如下:
android.support.test.espresso.PerformException: Error performing 'up click' on view 'with id: de.test.app:id/key_ptt'.
...
...
Caused by: java.lang.RuntimeException: Couldn't click at: 281.5,1117.5 precision: 25.0, 25.0 . Tapper: UP coordinate provider: CENTER precision describer: THUMB. Tried 3 times. With Rollback? false
我希望,这有帮助。
任何帮助和想法都将不胜感激!
提前坦克!
答案 0 :(得分:5)
Android中的点按由两个事件组成,即向下事件和向上/取消事件。如果你想把这两个分开,你不能制作“轻拍”,因为它们已经包含了两者。
那就是说,你的想法有效,你只需要使用较低级别的api,即UiController
以及MotionEvents
中的帮助方法。但要注意:因为“释放”视图需要先持有它,所以如果你没有进行适当的清理,你的测试将依赖于彼此。
示例:在测试中按下View,您的测试失败,然后在另一个测试中,您在未单击的视图上发布:您的第二个测试将通过,而不应该没有。
我在my github上传了一份完整的样本。这里的关键点。首先是测试代码:
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
getActivity();
}
@After
public void tearDown() throws Exception {
super.tearDown();
LowLevelActions.tearDown();
}
@Test
public void testAssertWhilePressed() {
onView(withId(R.id.button)).perform(pressAndHold());
onView(withId(R.id.text)).check(matches(withText("Button is held down")));
onView(withId(R.id.button)).perform(release());
}
然后LowLevelActions:
public class LowLevelActions {
static MotionEvent sMotionEventDownHeldView = null;
public static PressAndHoldAction pressAndHold() {
return new PressAndHoldAction();
}
public static ReleaseAction release() {
return new ReleaseAction();
}
public static void tearDown() {
sMotionEventDownHeldView = null;
}
static class PressAndHoldAction implements ViewAction {
@Override
public Matcher<View> getConstraints() {
return isDisplayingAtLeast(90); // Like GeneralClickAction
}
@Override
public String getDescription() {
return "Press and hold action";
}
@Override
public void perform(final UiController uiController, final View view) {
if (sMotionEventDownHeldView != null) {
throw new AssertionError("Only one view can be held at a time");
}
float[] precision = Press.FINGER.describePrecision();
float[] coords = GeneralLocation.CENTER.calculateCoordinates(view);
sMotionEventDownHeldView = MotionEvents.sendDown(uiController, coords, precision).down;
// TODO: save view information and make sure release() is on same view
}
}
static class ReleaseAction implements ViewAction {
@Override
public Matcher<View> getConstraints() {
return isDisplayingAtLeast(90); // Like GeneralClickAction
}
@Override
public String getDescription() {
return "Release action";
}
@Override
public void perform(final UiController uiController, final View view) {
if (sMotionEventDownHeldView == null) {
throw new AssertionError("Before calling release(), you must call pressAndHold() on a view");
}
float[] coords = GeneralLocation.CENTER.calculateCoordinates(view);
MotionEvents.sendUp(uiController, sMotionEventDownHeldView, coords);
}
}
}