AKR
AKR

Reputation: 531

JavaFX: Getting Stage of running Applications

For testing a application with TestFX i need to get the actual primary stage of a running application. This means that i haven't the code, i can just run the application through a jar.

Is there any possible solution for this? Scenic View does this already, but i was not able to reproduce this functionallity, especially because it seems that they use the deprecated funtion

Windows.impl_getWindows

which is not working in my case.

Upvotes: 3

Views: 633

Answers (1)

Renato
Renato

Reputation: 13690

Try this:

import com.sun.javafx.robot.impl.FXRobotHelper;

static Collection<Stage> getAllJavaFXStages() {
    try {
        return FXRobotHelper.getStages();
    } catch ( NullPointerException npe ) {
        // nasty NPE if no stages exist
        return Collections.emptyList();
    }
}

```

Based on my own testing framework code: Automaton.

EDIT:

If you want to get a Stage from a different JVM instance than where you're running your code, then there's no simple way.

You're right, ScenicView does it, but it uses tools.jar to do it. This is not a standard jar you get in your runtime, so you must add it manually (placing it in jre/lib/ext should do it, you'll normally find it in lib only).

I tracked down the code where ScenicView seems to be doing it in their BitBucket repo.

Check the function getRunningJavaFXApplications for example.

Have fun using that in your tests!

Upvotes: 2

Related Questions