-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJavaFXLibrary.java
336 lines (312 loc) · 13.7 KB
/
JavaFXLibrary.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* 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.
*/
import javafxlibrary.exceptions.JavaFXLibraryFatalException;
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
import javafxlibrary.exceptions.JavaFXLibraryTimeoutException;
import javafxlibrary.keywords.AdditionalKeywords.RunOnFailure;
import javafxlibrary.utils.HelperFunctions;
import javafxlibrary.utils.RobotLog;
import javafxlibrary.utils.TestFxAdapter;
import javafxlibrary.utils.TestListener;
import org.apache.commons.io.FileUtils;
import org.python.google.common.base.Throwables;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.library.AnnotationLibrary;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.net.BindException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import static javafxlibrary.utils.HelperFunctions.getLibraryKeywordTimeout;
import static org.testfx.util.WaitForAsyncUtils.*;
public class JavaFXLibrary extends AnnotationLibrary {
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
public static final String ROBOT_LIBRARY_VERSION = loadRobotLibraryVersion();
public static final TestListener ROBOT_LIBRARY_LISTENER = new TestListener();
static List<String> noLibraryKeywordTimeoutKeywords = new ArrayList<String>() {{
add("launchJavafxApplication");
add("launchSwingApplication");
add("launchSwingApplicationInSeparateThread");
add("closeJavafxApplication");
add("closeSwingApplication");
add("waitForEventsInFxApplicationThread");
add("waitUntilElementDoesNotExists");
add("waitUntilElementExists");
add("waitUntilNodeIsEnabled");
add("waitUntilNodeIsNotEnabled");
add("waitUntilNodeIsNotVisible");
add("waitUntilNodeIsVisible");
add("waitUntilProgressBarIsFinished");
}};
static List<String> noWrappedAsyncFxKeywords = new ArrayList<String>() {{
add("callObjectMethodInFxApplicationThread");
add("captureImage");
add("capturePrimaryScreen");
add("captureSceneContainingNode");
add("clearObjectMap");
add("closeJavafxApplication");
add("closeSwingApplication");
add("dragFrom");
add("dropTo");
add("dropToCoordinates");
add("getCurrentApplication");
add("getLibraryVersion");
add("getScreenshot Directory");
add("getScreenshotDirectory");
add("getSystemProperty");
add("isJavaAgent");
add("launchJavafxApplication");
add("launchSwingApplication");
add("launchSwingApplicationInSeparateThread");
add("logApplicationClasspath");
add("logSystemProperties");
add("moveTo");
add("nodeShouldBeHoverable");
add("nodeShouldNotBeHoverable");
add("pushManyTimes");
add("scrollHorizontally");
add("scrollVertically");
add("setImageLogging");
add("setSafeClicking");
add("setScreenshotDirectory");
add("setSystemProperty");
add("setTimeout");
add("setToClasspath");
add("setWriteSpeed");
add("waitForEventsInFxApplicationThread");
add("waitUntilElementDoesNotExists");
add("waitUntilElementExists");
add("waitUntilNodeIsEnabled");
add("waitUntilNodeIsNotEnabled");
add("waitUntilNodeIsNotVisible");
add("waitUntilNodeIsVisible");
add("waitUntilProgressBarIsFinished");
add("writeTo");
}};
static List<String> includePatterns = new ArrayList<String>() {{
add("javafxlibrary/keywords/AdditionalKeywords/*.class");
add("javafxlibrary/keywords/Keywords/*.class");
add("javafxlibrary/keywords/*.class");
add("javafxlibrary/tests/*.class");
}};
public JavaFXLibrary() {
this(false);
}
public JavaFXLibrary(boolean headless) {
super(includePatterns);
if (headless) {
System.setProperty("testfx.robot", "glass");
System.setProperty("testfx.headless", "true");
System.setProperty("glass.platform", "Monocle");
System.setProperty("monocle.platform", "Headless");
System.setProperty("prism.order", "sw");
System.setProperty("prism.text", "t2k");
TestFxAdapter.isHeadless = true;
} else {
// v4.0.15-alpha sets default robot as glass, which breaks rolling
// Forcing usage of awt robot as previous versions
System.setProperty("testfx.robot", "awt");
}
}
public static String loadRobotLibraryVersion() {
try {
return ResourceBundle.getBundle(JavaFXLibrary.class.getCanonicalName().replace(".", File.separator))
.getString("version");
} catch (RuntimeException e) {
return "unknown";
}
}
@Autowired
protected RunOnFailure runOnFailure;
@Override
public Object runKeyword(String keywordName, List args, Map kwargs) {
if (kwargs == null) {
kwargs = new HashMap();
}
List finalArgs;
Map finalKwargs;
// JavalibCore changes arguments of Call Method keywords to Strings after this check, so they need to handle their own objectMapping
if (!(keywordName.equals("callObjectMethod") || keywordName.equals("callObjectMethodInFxApplicationThread"))) {
finalArgs = HelperFunctions.useMappedObjects(args);
finalKwargs = HelperFunctions.useMappedObjects(kwargs);
} else {
finalArgs = args;
finalKwargs = kwargs;
}
// Run keyword either in async or asyncFx thread with or without timeout
// Execution collects retval and retExcep from keyword
AtomicReference<Object> retval = new AtomicReference<>();
AtomicReference<RuntimeException> retExcep = new AtomicReference<>();
RobotLog.ignoreDuplicates();
try {
if (noWrappedAsyncFxKeywords.contains(keywordName)) {
// no asyncFx thread
if (noLibraryKeywordTimeoutKeywords.contains(keywordName)) {
// without timeout
retval.set(super.runKeyword(keywordName, finalArgs, finalKwargs));
} else {
// in async thread
retval.set(waitForAsync(getLibraryKeywordTimeout(TimeUnit.MILLISECONDS), () -> {
try {
return super.runKeyword(keywordName, finalArgs, finalKwargs);
} catch (RuntimeException rte) {
retExcep.set(rte);
return null;
}
}));
}
} else {
// in asyncFx thread
retval.set(waitForAsyncFx(getLibraryKeywordTimeout(TimeUnit.MILLISECONDS), () -> {
try {
return super.runKeyword(keywordName, finalArgs, finalKwargs);
} catch (RuntimeException rte) {
retExcep.set(rte);
return null;
}
}));
waitForFxEvents(5);
}
} catch (JavaFXLibraryTimeoutException jfxte) {
// timeout already expired, catch exception and jump out
retExcep.set(jfxte);
} catch (RuntimeException rte) {
// catch exception and continue trying
retExcep.set(rte);
}
// in failure take screenshot and handle exception
if (retExcep.get() != null) {
RobotLog.reset();
RuntimeException e = retExcep.get();
runOnFailure.runOnFailure();
if (e.getCause() instanceof JavaFXLibraryFatalException) {
RobotLog.trace("JavaFXLibrary: Caught JavaFXLibrary FATAL exception: \n" + Throwables.getStackTraceAsString(e));
throw e;
} else if (e.getCause() instanceof JavaFXLibraryNonFatalException) {
RobotLog.trace("JavaFXLibrary: Caught JavaFXLibrary NON-FATAL exception: \n" + Throwables.getStackTraceAsString(e));
throw e;
} else if (e.getCause() instanceof TimeoutException) {
throw new JavaFXLibraryNonFatalException("Library keyword timeout (" + getLibraryKeywordTimeout() + "s) for keyword: " + keywordName);
} else if (e.getCause() instanceof IllegalArgumentException) {
RobotLog.trace("JavaFXLibrary: Caught IllegalArgumentException: \n" + Throwables.getStackTraceAsString(e));
throw new JavaFXLibraryNonFatalException("Illegal arguments for keyword '" + keywordName + "':\n" +
" ARGS: " + Arrays.toString(args.toArray()) + "\n" +
" KWARGS: " + Arrays.toString(kwargs.entrySet().toArray()));
} else {
RobotLog.trace("JavaFXLibrary: Caught JavaFXLibrary RUNTIME exception: \n" + Throwables.getStackTraceAsString(e));
throw e;
}
}
RobotLog.reset();
return retval.get();
}
@Override
public String getKeywordDocumentation(String keywordName) {
if (keywordName.equals("__intro__")) {
try {
return FileUtils.readFileToString(new File("./src/main/java/libdoc-documentation.txt"), "utf-8");
} catch (IOException e) {
e.printStackTrace();
return "IOException occurred while reading the documentation file!";
}
} else if (keywordName.equals("__init__")) {
try {
return FileUtils.readFileToString(new File("./src/main/java/libdoc-init-documentation.txt"), "utf-8");
} catch (IOException e) {
e.printStackTrace();
return "IOException occurred while reading the init documentation file!";
}
} else {
try {
return super.getKeywordDocumentation(keywordName);
} catch (Exception e) {
return keywordName;
}
}
}
/**
* Returns the currently active JavaFXLibrary instance.
*
* @return the library instance
* @throws ScriptException - if error occurs in script
*/
public static JavaFXLibrary getLibraryInstance() throws ScriptException {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
engine.put("library", "JavaFXLibrary");
engine.eval("from robot.libraries.BuiltIn import BuiltIn");
engine.eval("instance = BuiltIn().get_library_instance(library)");
return (JavaFXLibrary) engine.get("instance");
}
public static void main(String[] args) throws Exception {
startServer(args);
}
public static void premain(String args) {
TestFxAdapter.isAgent = true;
Thread agentThread = new Thread(() -> {
try {
if (args == null) {
startServer();
} else {
startServer(args);
}
} catch (Exception e) {
e.printStackTrace();
}
});
agentThread.setDaemon(true);
agentThread.start();
}
public static void startServer(String... args) throws Exception {
int port = 8270;
InetAddress ipAddr = InetAddress.getLocalHost();
try {
JavaFXLibraryRemoteServer.configureLogging();
System.out.println("----------------------------= JavaFXLibrary =-----------------------------");
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
System.out.println("RemoteServer for JavaFXLibrary will be started at default port of: " + port + ".\n" +
"If you wish to use another port, restart the library and give port number\n" +
"as an argument.");
}
JavaFXLibraryRemoteServer server = new JavaFXLibraryRemoteServer(port);
server.putLibrary("/RPC2", new JavaFXLibrary());
server.start();
System.out.println("\n JavaFXLibrary " + ROBOT_LIBRARY_VERSION + " is now available at: " +
ipAddr.getHostAddress() + ":" + port + "\n");
} catch (NumberFormatException nfe) {
System.out.println("\n Error! Not a valid port number: " + args[0] + "\n");
System.exit(1);
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
System.exit(1);
} catch (BindException be) {
System.out.println("\n Error! " + be.getMessage() + ": " + ipAddr.getHostAddress() + ":" + port + "\n");
System.exit(1);
} catch (IOException ioe) {
System.out.println("\n Error! " + ioe.getMessage() + ": " + ipAddr.getHostAddress() + ":" + port + "\n");
System.exit(1);
}
}
}