forked from eficode/JavaFXLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.java
More file actions
173 lines (155 loc) · 6.79 KB
/
Session.java
File metadata and controls
173 lines (155 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright 2017-2018 Eficode Oy
* Copyright 2018- Robot Framework Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javafxlibrary.utils;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
import org.testfx.api.FxRobot;
import org.testfx.api.FxToolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeoutException;
public class Session {
public Stage primaryStage;
public FxRobot sessionRobot;
public Application sessionApplication;
public String applicationName;
public String screenshotDirectory;
public String screenshotDirectoryInLogs;
public Session(String appName, String... appArgs) {
try {
// start the client
this.primaryStage = FxToolkit.registerPrimaryStage();
this.sessionApplication = FxToolkit.setupApplication((Class) Class.forName(appName), appArgs);
this.sessionRobot = new FxRobot();
this.applicationName = appName;
this.screenshotDirectory = System.getProperty("user.dir") + "/report-images/";
} catch (ClassNotFoundException e) {
throw new JavaFXLibraryNonFatalException("Couldn't find main application class from " + appName);
} catch (Exception e) {
throw new JavaFXLibraryNonFatalException("Problem launching the application: " + e.getMessage(), e);
}
}
public Session(Class<Application> appClass, String... appArgs) {
try {
this.primaryStage = FxToolkit.registerPrimaryStage();
this.sessionApplication = FxToolkit.setupApplication(appClass, appArgs);
this.sessionRobot = new FxRobot();
this.applicationName = appClass.toString();
this.screenshotDirectory = System.getProperty("user.dir") + "/report-images/";
} catch (TimeoutException e) {
throw new JavaFXLibraryNonFatalException("Problem launching the application: " + appClass.getSimpleName() + ", "
+ e.getMessage(), e);
}
}
public Session(Application application) {
try {
this.primaryStage = FxToolkit.registerPrimaryStage();
this.sessionApplication = FxToolkit.setupApplication(() -> application);
this.sessionRobot = new FxRobot();
this.applicationName = "JavaFXLibrary SwingWrapper";
this.screenshotDirectory = System.getProperty("user.dir") + "/report-images/";
} catch (TimeoutException e) {
throw new JavaFXLibraryNonFatalException("Problem launching the application: " + e.getMessage(), e);
}
}
/**
* Used when JavaFXLibrary is attached with java agent
*/
public Session(String applicationName) {
try {
Optional<Stage> existingStage = getExistingPrimaryStage();
if (!existingStage.isPresent()) {
throw new JavaFXLibraryNonFatalException("Could not hook to existing application: stage not found");
}
this.primaryStage = FxToolkit.registerStage(existingStage::get);
this.sessionRobot = new FxRobot();
this.applicationName = applicationName;
this.screenshotDirectory = System.getProperty("user.dir") + "/report-images/";
} catch (Exception e) {
throw new JavaFXLibraryNonFatalException("Problem launching the application: " + e.getMessage(), e);
}
}
public void closeApplication() {
try {
FxToolkit.hideStage();
FxToolkit.cleanupStages();
sessionRobot.release(new KeyCode[]{});
sessionRobot.release(new MouseButton[]{});
FxToolkit.cleanupApplication(sessionApplication);
} catch (Exception e) {
throw new JavaFXLibraryNonFatalException("Problem shutting down the application: " + e.getMessage(), e);
}
}
public void closeSwingApplication() {
Frame[] frames = Frame.getFrames();
for (Frame frame : frames) {
if (frame instanceof JFrame) {
JFrame jFrame = (JFrame) frame;
// EXIT_ON_CLOSE stops test execution on Jython (calls System.exit(0);)
jFrame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
jFrame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
closeApplication();
}
/**
* When running JavaFXLibrary as java agent this method tries to find first showing stage.
*/
private Optional<Stage> getExistingPrimaryStage() {
try {
ObservableList<Window> windows;
// getWindows method is added in Java 9
windows = (ObservableList<Window>) Window.class.getMethod("getWindows")
.invoke(null);
return windows.stream()
.filter(Stage.class::isInstance)
.map(Stage.class::cast)
.filter(Stage::isShowing)
.findFirst();
} catch (InvocationTargetException | IllegalArgumentException | IllegalAccessException | NoSuchMethodException e) {
// java 8 implementation
try {
Iterator<Window> it = (Iterator<Window>) Window.class.getMethod("impl_getWindows")
.invoke(null);
List<Window> windows = new ArrayList<>();
while (it.hasNext()) {
windows.add(it.next());
}
return windows.stream()
.filter(Stage.class::isInstance)
.map(Stage.class::cast)
.filter(Stage::isShowing)
.findFirst();
} catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException ex) {
e.printStackTrace();
}
}
return Optional.empty();
}
}