0

How can you make an assumption that checks if a test is executed in an interactive session?

FEST robot clicks and such are ignored on the CI server now since the session is not interactive, but I don't want to hard-codedly @Disable them. I would rather execute some assumption that tests if my BasicRobot can do anything at all. I refer to org.fest.swing.core.BasicRobot / org.assertj.swing.core.BasicRobot.

I would rather avoid displaying an auxiliary Window @BeforeEach (or even @BeforeAll).

This, on the other hand, relies on an initial pointer location which makes it a bit fragile.

    @BeforeEach
    void testRobot() {
        PointerInfo initialPointerInfo = MouseInfo.getPointerInfo();
        Point initialLocation = initialPointerInfo.getLocation();
        robot.moveMouse(initialLocation.x + 5, initialLocation.y + 5);
        PointerInfo subsequentPointerInfo = MouseInfo.getPointerInfo();
        Point subsequentLocation = subsequentPointerInfo.getLocation();
        assumeFalse(Objects.equals(initialLocation, subsequentLocation), "Robot commands ignored. Non-interactive session?");
    }

Please note it is not about headless / not headless (the server does have a graphic environment).

Java 8.

1 Answer 1

0
    private boolean interactionPossible() {
        PointerInfo initialPointerInfo = MouseInfo.getPointerInfo();
        Point initialLocation = initialPointerInfo.getLocation();
        Point requestedLocation = getRandomPointOnScreen();
        robot.moveMouse(requestedLocation);
        PointerInfo subsequentPointerInfo = MouseInfo.getPointerInfo();
        Point subsequentLocation = subsequentPointerInfo.getLocation();
        boolean robotMovedPointer = !Objects.equals(initialLocation, subsequentLocation);
        return robotMovedPointer;
    }

    private Point getRandomPointOnScreen() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Random random = new Random();
        int x = random.nextInt(screenSize.width);
        int y = random.nextInt(screenSize.height);
        Point point = new Point(x, y);
        return point;
    }

There's a vanishingly small possibility that a new point is identical to the current point. However, it arguably could be neglected.

UPD: For some reason, on our CI server, a first mouse move positions the cursor in an entirely different (seemingly random) point, not the one the robot requested. Subsequent mouse moves are, as expected, ignored due to a non-interactive session. Locally, though, my solution works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.