stf*_*tfn 5 android automated-tests ui-automation android-espresso
我对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());
Run Code Online (Sandbox Code Playgroud)
我尝试使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
攻丝器的代码:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误如下:
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
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助。
任何帮助和想法将不胜感激!
提前很多坦克!
Android中的轻拍由两个事件组成,一个是向下事件,另一个是向上/取消事件。如果要把这两个分开,则不能进行“抽头”,因为它们已经包含了两者。
就是说,您的想法行得通,您只需要使用较低级别的api,即UiController与中的helper方法一起使用MotionEvents。不过请注意:由于“释放”视图需要首先保持该视图,因此如果您不进行适当的清理,您的测试将相互依赖。
例如:在一个测试中,您按下一个视图,您的测试失败,然后在另一个测试中,您在未单击的视图上发布:您的第二个测试将通过,而本不应该通过。
我在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());
}
Run Code Online (Sandbox Code Playgroud)
然后是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);
}
}
}
Run Code Online (Sandbox Code Playgroud)