PerformException错误以5dp余量执行单击操作

时间:2019-10-19 05:48:43

标签: android testing automated-tests android-espresso

我正在编写仪表测试,但无法单击我的视图。这是我的观点

;; look here in an answer how to use cl-ppcre:split
;; https://stackoverflow.com/questions/15393797/lisp-splitting-input-into-separate-strings
(ql:quickload :cl-ppcre)

(defun read-file-lines (file-path)
  (with-open-file (f file-path :direction :input)
    (loop for line = (read-line f nil)
          while line
          collect line)))

(defun string-to-words (s) (cl-ppcre:split "\\s+" s))
(defun to-single-characters (s) (coerce s 'list))

(defun read-file-to-character-lists (file-path)
  (mapcan (lambda (s) 
            (mapcar #'to-single-characters
                    (string-to-words s)))
          (read-file-lines file-path)))

(read-file-to-character-lists "~/test.lisp.txt")
;; ((#\h #\e #\l #\l #\o) (#\t #\h #\i #\s) (#\i #\s) (#\a) (#\t #\e #\s #\t)
;;  (#\f #\i #\l #\e))

;; or use above's function:
(map-tree #'string (read-file-to-character-lists "~/test.lisp.txt"))
;; (("h" "e" "l" "l" "o") ("t" "h" "i" "s") ("i" "s") ("a") ("t" "e" "s" "t")
;;  ("f" "i" "l" "e"))


;; or:
(defun to-single-letter-strings (s) (cl-ppcre:split "\\s*" s))

(defun read-file-to-letter-lists (file-path)
  (mapcan (lambda (s) 
            (mapcar #'to-single-letter-strings
                    (string-to-words s)))
          (read-file-lines file-path)))

(read-file-to-letter-lists "~/test.lisp.txt")
;; (("h" "e" "l" "l" "o") ("t" "h" "i" "s") ("i" "s") ("a") ("t" "e" "s" "t")
;; ("f" "i" "l" "e"))

我的constraintLayout在左右两侧都有30dp的余量。我的测试用例如下-

<ConstraintLayout......>
 <ImageView
        android:id="@+id/go_to_next"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/rounded_bg"
        android:rotation="180"
        android:scaleType="centerInside"
        android:src="@drawable/ic_back"
        app:layout_constraintBottom_toBottomOf="@+id/mobile_number_edittext"
        app:layout_constraintEnd_toEndOf="parent" />
</ConstraintLayout/>

click()失败,并收到 @Test fun useAppContext() { // Context of the app under test. val appContext = InstrumentationRegistry.getInstrumentation().targetContext assertEquals("com.test", appContext.packageName) onView(withId(R.id.go_to_next)).check(matches(isDisplayingAtLeast(90))) onView(withId(R.id.go_to_next)).perform(click()) } 错误

如果我像5dp这样给ImageView提供页边距,这将起作用,但是如果没有页边距,则无法使用。我该如何解决?我的视图在布局中完全可见,只是它与最右端对齐。仅供参考,所有动画均已禁用

1 个答案:

答案 0 :(得分:1)

在视图上,setRotation调用似乎导致坐标计算中断。您可以尝试物理旋转文件,也可以创建自定义的点击操作来强制点击该文件:

public static ViewAction forceClick() {
    return new ViewAction() {
        @Override public Matcher<View> getConstraints() {
            return allOf(isClickable(), isEnabled(), isDisplayed());
        }

        @Override public String getDescription() {
            return "force click";
        }

        @Override public void perform(UiController uiController, View view) {
            view.performClick(); // perform click without checking view coordinates.
            uiController.loopMainThreadUntilIdle();
        }
    };
}

然后将其用于按钮或附加了点击监听器的任何视图:

onView(withId(R.id.go_to_next)).perform(forceClick());