Skip to content

Commit e81d02b

Browse files
committed
Add keyword Launch Swing Application In Separate Thread
1 parent c4d819d commit e81d02b

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/main/java/javafxlibrary/keywords/AdditionalKeywords/ApplicationLauncher.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URLClassLoader;
3232
import java.util.*;
3333

34+
import static javafxlibrary.utils.HelperFunctions.createThreadedWrapperApplication;
3435
import static javafxlibrary.utils.HelperFunctions.createWrapperApplication;
3536
import static javafxlibrary.utils.HelperFunctions.getMainClassFromJarFile;
3637

@@ -66,21 +67,40 @@ public void launchJavafxApplication(String appName, String... appArgs) {
6667
@ArgumentNames({"appName", "*args"})
6768
public void launchSwingApplication(String appName, String... appArgs) {
6869
RobotLog.info("Starting application:" + appName);
69-
Class c;
70+
Class c = getMainClass(appName);
71+
Application app = createWrapperApplication(c, appArgs);
72+
createNewSession(app);
73+
RobotLog.info("Application: " + appName + " started.");
74+
}
75+
76+
@RobotKeyword("Creates a wrapper application the same way as in `Launch Swing Application`, but starts it in a new " +
77+
"thread. This is required when main method of the test application is blocked and execution does not " +
78+
"return after calling it until the application gets closed. Be sure to set the library timeout with " +
79+
"`Set Timeout` so that the test application will have enough time to load, as the test execution will " +
80+
"continue instantly after calling the main method.\n\n"
81+
+ "``appName`` is the name of the application to launch. \n\n"
82+
+ "``appArgs`` is a list of arguments to be passed for the application. \n\n"
83+
+ "Example:\n"
84+
+ "| Launch Swing Application In Separate Thread | _javafxlibrary.testapps.SwingApplication |\n"
85+
+ "| Launch Swing Application In Separate Thread | _TestApplication.jar_ |\n")
86+
@ArgumentNames({"appName", "*args"})
87+
public void launchSwingApplicationInSeparateThread(String appName, String... appArgs) {
88+
RobotLog.info("Starting application:" + appName);
89+
Class c = getMainClass(appName);
90+
Application app = createThreadedWrapperApplication(c, appArgs);
91+
createNewSession(app);
92+
RobotLog.info("Application: " + appName + " started.");
93+
}
7094

95+
private Class getMainClass(String appName) {
7196
try {
7297
if (appName.endsWith(".jar"))
73-
c = getMainClassFromJarFile(appName);
98+
return getMainClassFromJarFile(appName);
7499
else
75-
c = Class.forName(appName);
76-
100+
return Class.forName(appName);
77101
} catch (ClassNotFoundException e) {
78102
throw new JavaFXLibraryNonFatalException("Unable to launch application: " + appName, e);
79103
}
80-
81-
Application app = createWrapperApplication(c, appArgs);
82-
createNewSession(app);
83-
RobotLog.info("Application: " + appName + " started.");
84104
}
85105

86106
private void _addPathToClassPath(String path) {

src/main/java/javafxlibrary/utils/HelperFunctions.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,28 @@ public void start(Stage primaryStage) {
842842
}
843843
}
844844

845+
public static Application createThreadedWrapperApplication(Class c, String... appArgs) {
846+
try {
847+
Method main = c.getMethod("main", String[].class);
848+
return new Application() {
849+
@Override
850+
public void start(Stage primaryStage) {
851+
Thread t = new Thread (() -> {
852+
try {
853+
main.invoke(null, (Object) appArgs);
854+
855+
} catch (IllegalAccessException | InvocationTargetException e) {
856+
throw new JavaFXLibraryNonFatalException("Unable to launch application: " + c.getName(), e);
857+
}
858+
});
859+
t.start();
860+
}
861+
};
862+
} catch (NoSuchMethodException e) {
863+
throw new JavaFXLibraryNonFatalException("Couldn't create wrapper application for " + c.getName(), e);
864+
}
865+
}
866+
845867
public static Finder createFinder() {
846868
return new Finder();
847869
}

0 commit comments

Comments
 (0)