Search code examples
javajavafxtestfx

TestFX clicking menu items


I'm writing a JavaFX application with a menubar. I have given the menu items CSS ids, and I want to use TestFX to click on them. Here's the code:

clickOn("#menu-file").clickOn("#menu-file-new-project");

When I run this I can see my mouse move to the completely wrong place on the screen, and then the test fails saying it couldn't find any elements matching #menu-file-new-project. I'm assuming it can't find them because it's never opening the File menu, because it's clicking in the wrong location.

What should I do about that? Windows 10, Java 8u120, TestFX 4.0.6. For the record it fails the same way in monocle headless mode


Solution

  • Okay, this has nothing to do with the scale and I'm not even sure why it works, but overriding the point method as follows in my Test Application instance seems to work for some reason:

    @Override
    public PointQuery point(Node node) {
        Point2D topLeftPoint = node.localToScreen(0, 0);
        Point2D pos = new Point2D(topLeftPoint.getX(), topLeftPoint.getY());
    
        return super.point(node).atOffset(pos);
    }