'Check checkbox with Espresso

I have a problem with checking the checkbox using Espresso library. My checkbox contains hyperlink (clickable text which opens web brower). When i use onView(withId(R.id.termsAndCondition)).perform(click()) it just opens web brower.

Is there any possibility to check this chechkbox without changing program code? Checkbox hyperlink must be clickable



Solution 1:[1]

Ok I found a good solution for that by clicking at the position 0, 0 of the checkbox

onView(withId(R.id.view)).check(matches(isNotChecked())).perform(clickPercent(0F, 0F));

and the clickPercent function

public static ViewAction clickPercent(final float pctX, final float pctY) {
    return new GeneralClickAction(
            Tap.SINGLE,
            view -> {
                final int[] screenPos = new int[2];
                view.getLocationOnScreen(screenPos);
                int w = view.getWidth();
                int h = view.getHeight();

                float x = w * pctX;
                float y = h * pctY;

                final float screenX = screenPos[0] + x;
                final float screenY = screenPos[1] + y;
                float[] coordinates = {screenX, screenY};

                return coordinates;
            },
            Press.FINGER,
            InputDevice.SOURCE_MOUSE,
            MotionEvent.BUTTON_PRIMARY);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guillaume