Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5714a2

Browse files
committed
added waitFor() -timeouts for all keywords
1 parent f8be01c commit c5714a2

File tree

7 files changed

+102
-14
lines changed

7 files changed

+102
-14
lines changed

src/main/java/JavaFXLibrary.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@
2121
import java.net.InetAddress;
2222
import java.net.UnknownHostException;
2323
import java.util.*;
24+
import java.util.concurrent.TimeUnit;
25+
import java.util.concurrent.TimeoutException;
26+
import java.util.concurrent.atomic.AtomicReference;
27+
2428
import javafxlibrary.exceptions.JavaFXLibraryFatalException;
2529
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
30+
import javafxlibrary.exceptions.JavaFXLibraryTimeoutException;
2631
import javafxlibrary.keywords.AdditionalKeywords.RunOnFailure;
2732
import javafxlibrary.utils.HelperFunctions;
2833
import javafxlibrary.utils.RobotLog;
@@ -32,6 +37,8 @@
3237
import org.robotframework.javalib.annotation.Autowired;
3338
import org.robotframework.javalib.library.AnnotationLibrary;
3439
import org.robotframework.remoteserver.RemoteServer;
40+
import org.testfx.util.WaitForAsyncUtils;
41+
3542
import javax.script.ScriptEngine;
3643
import javax.script.ScriptEngineManager;
3744
import javax.script.ScriptException;
@@ -70,10 +77,32 @@ public Object runKeyword(String keywordName, Object[] args) {
7077
else
7178
finalArgs = args;
7279

80+
AtomicReference<Object> retval = new AtomicReference<>();
81+
AtomicReference<RuntimeException> retExcep = new AtomicReference<>();
82+
7383
try {
74-
return super.runKeyword(keywordName, finalArgs);
75-
} catch (RuntimeException e) {
84+
// timeout + 1 so that underlying timeout has a chance to expire first
85+
WaitForAsyncUtils.waitFor(getWaitUntilTimeout() + 1, TimeUnit.SECONDS, () -> {
86+
87+
try {
88+
retval.set(super.runKeyword(keywordName, finalArgs));
89+
return true;
90+
91+
} catch (JavaFXLibraryTimeoutException jfxte){
92+
// timeout already expired, catch exception and jump out
93+
retExcep.set(jfxte);
94+
throw jfxte;
95+
96+
} catch (RuntimeException e){
97+
// catch exception and continue trying
98+
retExcep.set(e);
99+
return false;
100+
}
101+
});
102+
} catch (TimeoutException te) {
103+
RuntimeException e = retExcep.get();
76104
runOnFailure.runOnFailure();
105+
77106
if (e.getCause() instanceof JavaFXLibraryFatalException) {
78107
RobotLog.trace("JavaFXLibrary: Caught JavaFXLibrary FATAL exception: \n" + Throwables.getStackTraceAsString(e));
79108
throw e;
@@ -84,7 +113,11 @@ public Object runKeyword(String keywordName, Object[] args) {
84113
RobotLog.trace("JavaFXLibrary: Caught JavaFXLibrary RUNTIME exception: \n" + Throwables.getStackTraceAsString(e));
85114
throw e;
86115
}
116+
} catch (JavaFXLibraryTimeoutException jfxte){
117+
RobotLog.trace("JavaFXLibrary: Caught JavaFXLibrary TIMEOUT exception: \n" + Throwables.getStackTraceAsString(jfxte));
118+
throw jfxte;
87119
}
120+
return retval.get();
88121
}
89122

90123
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2017-2018 Eficode Oy
3+
* Copyright 2018- Robot Framework Foundation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package javafxlibrary.exceptions;
19+
20+
@SuppressWarnings("serial")
21+
public class JavaFXLibraryTimeoutException extends JavaFXLibraryKeywordException {
22+
23+
/**
24+
* This will be a non-fatal exception
25+
*/
26+
public static final boolean ROBOT_EXIT_ON_FAILURE = false;
27+
28+
/**
29+
* Avoid adding the exception type as a prefix to this failure exception
30+
*/
31+
public static final boolean ROBOT_SUPPRESS_NAME = true;
32+
33+
public JavaFXLibraryTimeoutException() {
34+
super();
35+
}
36+
37+
public JavaFXLibraryTimeoutException(String string) {
38+
super(string);
39+
}
40+
41+
public JavaFXLibraryTimeoutException(Throwable t) {
42+
super(t);
43+
}
44+
45+
public JavaFXLibraryTimeoutException(String string, Throwable t) {
46+
super(string, t);
47+
}
48+
}

src/main/java/javafxlibrary/keywords/Keywords/BoundsLocation.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javafx.geometry.BoundingBox;
2121
import javafx.geometry.Rectangle2D;
2222
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
23+
import javafxlibrary.exceptions.JavaFXLibraryTimeoutException;
2324
import javafxlibrary.utils.HelperFunctions;
2425
import javafxlibrary.utils.RobotLog;
2526
import javafxlibrary.utils.TestFxAdapter;
@@ -127,10 +128,12 @@ public Object getBounds(Object locator) {
127128
} catch (IllegalAccessException | InvocationTargetException e) {
128129
throw new JavaFXLibraryNonFatalException("Could not execute move to using locator \"" + locator + "\": "
129130
+ e.getCause().getMessage());
131+
132+
} catch (JavaFXLibraryTimeoutException | JavaFXLibraryNonFatalException e){
133+
throw e;
134+
130135
} catch (Exception e) {
131-
if ( e instanceof JavaFXLibraryNonFatalException )
132-
throw e;
133-
throw new JavaFXLibraryNonFatalException("Couldn't find \"" + locator + "\"");
136+
throw new JavaFXLibraryNonFatalException("Couldn't find \"" + locator + "\"", e);
134137
}
135138
}
136139
}

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javafx.stage.Stage;
2929
import javafx.stage.Window;
3030
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
31+
import javafxlibrary.exceptions.JavaFXLibraryTimeoutException;
3132
import javafxlibrary.matchers.ProgressBarMatchers;
3233
import javafxlibrary.utils.finder.Finder;
3334
import org.apache.commons.lang3.StringUtils;
@@ -87,7 +88,7 @@ public static Node waitUntilExists(String target, int timeout, String timeUnit)
8788
WaitForAsyncUtils.waitFor((long) timeout, getTimeUnit(timeUnit), () -> hasValidCoordinates(node));
8889
return node;
8990
} catch (TimeoutException te) {
90-
throw new JavaFXLibraryNonFatalException("Given element \"" + target + "\" was not found within given timeout of "
91+
throw new JavaFXLibraryTimeoutException("Given element \"" + target + "\" was not found within given timeout of "
9192
+ timeout + " " + timeUnit);
9293
} catch (Exception e) {
9394
RobotLog.trace("Exception in waitUntilExists: " + e + "\n" + e.getCause().toString());
@@ -111,7 +112,7 @@ public static Node waitUntilVisible(Object target, int timeout) {
111112
} catch (JavaFXLibraryNonFatalException nfe) {
112113
throw nfe;
113114
} catch (TimeoutException te) {
114-
throw new JavaFXLibraryNonFatalException("Given target \"" + target + "\" did not become visible within given timeout of "
115+
throw new JavaFXLibraryTimeoutException("Given target \"" + target + "\" did not become visible within given timeout of "
115116
+ timeout + " seconds.");
116117
} catch (Exception e) {
117118
throw new JavaFXLibraryNonFatalException("Something went wrong while waiting target to be visible: " + e.getMessage());
@@ -132,7 +133,7 @@ public static Node waitUntilEnabled(Object target, int timeout) {
132133
} catch (JavaFXLibraryNonFatalException nfe) {
133134
throw nfe;
134135
} catch (TimeoutException te) {
135-
throw new JavaFXLibraryNonFatalException("Given target \"" + target + "\" did not become enabled within given timeout of "
136+
throw new JavaFXLibraryTimeoutException("Given target \"" + target + "\" did not become enabled within given timeout of "
136137
+ timeout + " seconds.");
137138
} catch (Exception e) {
138139
throw new JavaFXLibraryNonFatalException("Something went wrong while waiting target to be enabled: " + e.getMessage());
@@ -443,10 +444,10 @@ public static Object checkClickTarget(Object target) {
443444
checkClickLocation(target);
444445
return target;
445446

447+
} catch (JavaFXLibraryTimeoutException | JavaFXLibraryNonFatalException jfxe) {
448+
throw jfxe;
446449
} catch (Exception e) {
447-
if (e instanceof JavaFXLibraryNonFatalException)
448-
throw e;
449-
throw new JavaFXLibraryNonFatalException("Click target check failed: " + e.getMessage());
450+
throw new JavaFXLibraryNonFatalException("Click target check failed: " + e.getMessage(), e);
450451
}
451452
}
452453

src/test/java/javafxlibrary/utils/HelperFunctionsTests/WaitUntilEnabledTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import javafx.scene.control.Button;
55
import javafxlibrary.TestFxAdapterTest;
66
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
7+
import javafxlibrary.exceptions.JavaFXLibraryTimeoutException;
78
import javafxlibrary.utils.HelperFunctions;
89
import mockit.Mock;
910
import mockit.MockUp;
@@ -46,7 +47,7 @@ public void waitUntilEnabled_IsEnabledWithDelay() {
4647
@Test
4748
public void waitUntilEnabled_IsNotEnabled() {
4849
button.setDisable(true);
49-
thrown.expect(JavaFXLibraryNonFatalException.class);
50+
thrown.expect(JavaFXLibraryTimeoutException.class);
5051
thrown.expectMessage("Given target \"" + button + "\" did not become enabled within given timeout of 1 seconds.");
5152
HelperFunctions.waitUntilEnabled(".button", 1);
5253
}

src/test/java/javafxlibrary/utils/HelperFunctionsTests/WaitUntilExistsTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import javafx.scene.control.Button;
55
import javafxlibrary.TestFxAdapterTest;
66
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
7+
import javafxlibrary.exceptions.JavaFXLibraryTimeoutException;
78
import javafxlibrary.utils.finder.Finder;
89
import javafxlibrary.utils.HelperFunctions;
910
import mockit.*;
@@ -70,7 +71,7 @@ public void waitUntilExists_DoesNotExist() {
7071
}
7172
};
7273

73-
thrown.expect(JavaFXLibraryNonFatalException.class);
74+
thrown.expect(JavaFXLibraryTimeoutException.class);
7475
thrown.expectMessage("Given element \".button\" was not found within given timeout of 500 MILLISECONDS");
7576
HelperFunctions.waitUntilExists(".button", 500, "MILLISECONDS");
7677
}

src/test/java/javafxlibrary/utils/HelperFunctionsTests/WaitUntilVisibleTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import javafx.scene.control.Button;
55
import javafxlibrary.TestFxAdapterTest;
66
import javafxlibrary.exceptions.JavaFXLibraryNonFatalException;
7+
import javafxlibrary.exceptions.JavaFXLibraryTimeoutException;
78
import javafxlibrary.utils.HelperFunctions;
89
import mockit.*;
910
import org.junit.*;
@@ -47,7 +48,7 @@ public void waitUntilVisible_IsVisibleWithDelay() {
4748
@Test
4849
public void waitUntilVisible_IsNotVisible() {
4950
button.setVisible(false);
50-
thrown.expect(JavaFXLibraryNonFatalException.class);
51+
thrown.expect(JavaFXLibraryTimeoutException.class);
5152
thrown.expectMessage("Given target \"" + button + "\" did not become visible within given timeout of 1 seconds.");
5253
HelperFunctions.waitUntilVisible(".button", 1);
5354
}

0 commit comments

Comments
 (0)