Skip to content

Commit 089203b

Browse files
author
Mike Reiche
committed
Improved failsafe ness of WebDriver properties
1 parent 552756c commit 089203b

File tree

9 files changed

+169
-49
lines changed

9 files changed

+169
-49
lines changed

core/src/main/java/eu/tsystems/mms/tic/testframework/ioc/CoreHook.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
import eu.tsystems.mms.tic.testframework.testing.DefaultTestController;
5757
import eu.tsystems.mms.tic.testframework.testing.DefaultTestControllerOverrides;
5858
import eu.tsystems.mms.tic.testframework.testing.TestController;
59+
import eu.tsystems.mms.tic.testframework.utils.DefaultExecutionUtils;
5960
import eu.tsystems.mms.tic.testframework.utils.DefaultFormatter;
61+
import eu.tsystems.mms.tic.testframework.utils.ExecutionUtils;
6062
import eu.tsystems.mms.tic.testframework.utils.Formatter;
6163
import org.testng.annotations.Test;
6264

@@ -76,6 +78,7 @@ protected void configure() {
7678
bind(TestNGContextNameGenerator.class).to(DefaultTestNGContextGenerator.class).in(Scopes.SINGLETON);
7779
bind(IdGenerator.class).to(SequenceIdGenerator.class).in(Scopes.SINGLETON);
7880
bind(IExecutionContextController.class).to(DefaultExecutionContextController.class).in(Scopes.SINGLETON);
81+
bind(ExecutionUtils.class).to(DefaultExecutionUtils.class).in(Scopes.SINGLETON);
7982
}
8083

8184
@Override
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Testerra
3+
*
4+
* (C) 2021, Mike Reiche, T-Systems MMS GmbH, Deutsche Telekom AG
5+
*
6+
* Deutsche Telekom AG and all other contributors /
7+
* copyright owners license this file to you under the Apache
8+
* License, Version 2.0 (the "License"); you may not use this
9+
* file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package eu.tsystems.mms.tic.testframework.utils;
23+
24+
public class DefaultExecutionUtils implements ExecutionUtils {
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Testerra
3+
*
4+
* (C) 2021, Mike Reiche, T-Systems MMS GmbH, Deutsche Telekom AG
5+
*
6+
* Deutsche Telekom AG and all other contributors /
7+
* copyright owners license this file to you under the Apache
8+
* License, Version 2.0 (the "License"); you may not use this
9+
* file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package eu.tsystems.mms.tic.testframework.utils;
23+
24+
import eu.tsystems.mms.tic.testframework.logging.Loggable;
25+
import java.util.Optional;
26+
import java.util.function.Supplier;
27+
28+
public interface ExecutionUtils extends Loggable {
29+
default <T> Optional<T> getFailsafe(Supplier<T> supplier) {
30+
try {
31+
T returnVal = supplier.get();
32+
return Optional.ofNullable(returnVal);
33+
} catch (Throwable e) {
34+
log().warn("Unable to supply property", e);
35+
return Optional.empty();
36+
}
37+
}
38+
}

driver-ui/src/main/java/eu/tsystems/mms/tic/testframework/exceptions/PageFactoryException.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@
2121
*/
2222
package eu.tsystems.mms.tic.testframework.exceptions;
2323

24+
import eu.tsystems.mms.tic.testframework.common.Testerra;
2425
import eu.tsystems.mms.tic.testframework.pageobjects.PageObject;
26+
import eu.tsystems.mms.tic.testframework.utils.ExecutionUtils;
2527
import org.openqa.selenium.WebDriver;
2628

2729
public class PageFactoryException extends RuntimeException {
2830

31+
private static final ExecutionUtils executionUtils = Testerra.getInjector().getInstance(ExecutionUtils.class);
32+
2933
public PageFactoryException(Class<? extends PageObject> pageClass, WebDriver webDriver, Throwable cause) {
3034
super(String.format("Could not create instance of %s on \"%s\" (%s)",
3135
pageClass.getSimpleName(),
32-
(webDriver!=null)?webDriver.getTitle():"null",
33-
(webDriver!=null)?webDriver.getCurrentUrl():"null"
36+
executionUtils.getFailsafe(webDriver::getTitle).orElse("(na)"),
37+
executionUtils.getFailsafe(webDriver::getCurrentUrl).orElse("(na)")
3438
), cause);
3539
}
3640
}

driver-ui/src/main/java/eu/tsystems/mms/tic/testframework/utils/UITestUtils.java

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class UITestUtils implements WebDriverManagerProvider {
7474
* The logger for this class.
7575
*/
7676
private static final Logger LOGGER = LoggerFactory.getLogger(UITestUtils.class);
77+
private static final ExecutionUtils executionUtils = Testerra.getInjector().getInstance(ExecutionUtils.class);
7778

7879
/**
7980
* A date format for files like screenshots.
@@ -108,46 +109,33 @@ public static void takeScreenshot(WebDriver webDriver, Screenshot screenshot) {
108109
takeWebDriverScreenshotToFile(webDriver, screenshotFile);
109110

110111
// get page source (webdriver)
111-
String pageSource = webDriver.getPageSource();
112-
113-
if (pageSource == null) {
114-
LOGGER.error("getPageSource() returned nothing, skipping to add page source");
115-
} else {
116-
// save page source to file
112+
executionUtils.getFailsafe(webDriver::getPageSource).ifPresent(pageSource -> {
117113
savePageSource(pageSource, screenshot.createPageSourceFile());
118-
}
114+
});
115+
116+
Map<String, String> metaData = screenshot.getMetaData();
117+
sessionContext.map(SessionContext::getSessionKey).ifPresent(s -> metaData.put(Screenshot.MetaData.SESSION_KEY, s));
118+
sessionContext.map(SessionContext::getId).ifPresent(s -> metaData.put(Screenshot.MetaData.SESSION_CONTEXT_ID, s));
119+
executionUtils.getFailsafe(webDriver::getTitle).ifPresent(s -> metaData.put(Screenshot.MetaData.TITLE, s));
120+
executionUtils.getFailsafe(webDriver::getCurrentUrl).ifPresent(s -> metaData.put(Screenshot.MetaData.URL, s));
119121

120122
/*
121-
get infos
123+
window and focus infos
122124
*/
123-
try {
124-
Map<String, String> metaData = screenshot.getMetaData();
125-
metaData.put(Screenshot.MetaData.SESSION_KEY, sessionContext.map(SessionContext::getSessionKey).orElse(null));
126-
metaData.put(Screenshot.MetaData.SESSION_CONTEXT_ID, sessionContext.map(SessionContext::getId).orElse(null));
127-
metaData.put(Screenshot.MetaData.TITLE, webDriver.getTitle());
128-
129-
/*
130-
window and focus infos
131-
*/
132-
String window = "";
133-
String windowHandle = webDriver.getWindowHandle();
134-
Set<String> windowHandles = webDriver.getWindowHandles();
135-
if (windowHandles.size() < 2) {
136-
window = "#1/1";
137-
} else {
138-
String[] handleStrings = windowHandles.toArray(new String[0]);
139-
for (int i = 0; i < handleStrings.length; i++) {
140-
if (handleStrings[i].equals(windowHandle)) {
141-
window = "#" + (i + 1) + "/" + handleStrings.length;
142-
}
125+
String window = "";
126+
String windowHandle = executionUtils.getFailsafe(webDriver::getWindowHandle).orElse("");
127+
Set<String> windowHandles = webDriver.getWindowHandles();
128+
if (windowHandles.size() < 2) {
129+
window = "#1/1";
130+
} else {
131+
String[] handleStrings = windowHandles.toArray(new String[0]);
132+
for (int i = 0; i < handleStrings.length; i++) {
133+
if (handleStrings[i].equals(windowHandle)) {
134+
window = "#" + (i + 1) + "/" + handleStrings.length;
143135
}
144136
}
145-
146-
metaData.put(Screenshot.MetaData.WINDOW, window);
147-
metaData.put(Screenshot.MetaData.URL, webDriver.getCurrentUrl());
148-
} catch (Exception e) {
149-
LOGGER.warn("Unable to fulfill screenshot meta data: " + e.getMessage());
150137
}
138+
metaData.put(Screenshot.MetaData.WINDOW, window);
151139
}
152140

153141
private static Screenshot takeScreenshot(WebDriver eventFiringWebDriver, String originalWindowHandle) {
@@ -267,12 +255,7 @@ private static void switchToWindow(WebDriver webDriver, String windowHandle) {
267255
private static List<Screenshot> pTakeAllScreenshotsForSession(WebDriver webDriver) {
268256
final List<Screenshot> screenshots = new LinkedList<>();
269257
Set<String> windowHandles = webDriver.getWindowHandles();
270-
String originalWindowHandle;
271-
try {
272-
originalWindowHandle = webDriver.getWindowHandle();
273-
} catch (Exception e) {
274-
originalWindowHandle = windowHandles.stream().findFirst().orElse("");
275-
}
258+
String originalWindowHandle = executionUtils.getFailsafe(webDriver::getWindowHandle).orElseGet(() -> windowHandles.stream().findFirst().orElse(""));
276259

277260
if (windowHandles.size() > 1) {
278261
for (String windowHandle : windowHandles) {

driver-ui/src/main/java/eu/tsystems/mms/tic/testframework/utils/WebDriverUtils.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package eu.tsystems.mms.tic.testframework.utils;
2323

2424
import eu.tsystems.mms.tic.testframework.common.PropertyManager;
25+
import eu.tsystems.mms.tic.testframework.common.Testerra;
2526
import eu.tsystems.mms.tic.testframework.constants.TesterraProperties;
2627
import eu.tsystems.mms.tic.testframework.exceptions.SystemException;
2728
import eu.tsystems.mms.tic.testframework.transfer.ThrowablePackedResponse;
@@ -56,6 +57,7 @@ public final class WebDriverUtils {
5657
* Logger.
5758
*/
5859
private static final Logger LOGGER = LoggerFactory.getLogger(WebDriverUtils.class);
60+
private static final ExecutionUtils executionUtils = Testerra.getInjector().getInstance(ExecutionUtils.class);
5961

6062
/**
6163
* Timeout / maximum duration for Window Switching
@@ -73,13 +75,7 @@ public static boolean switchToWindow(Predicate<WebDriver> predicate) {
7375
}
7476

7577
public static boolean switchToWindow(WebDriver mainWebDriver, Predicate<WebDriver> predicate) {
76-
String mainWindowHandle;
77-
try {
78-
mainWindowHandle = mainWebDriver.getWindowHandle();
79-
} catch (Exception e) {
80-
mainWindowHandle = "";
81-
}
82-
String finalMainWindowHandle = mainWindowHandle;
78+
String finalMainWindowHandle = executionUtils.getFailsafe(mainWebDriver::getWindowHandle).orElse("");
8379

8480
return mainWebDriver.getWindowHandles().stream()
8581
.filter(windowHandle -> !windowHandle.equals(finalMainWindowHandle))

driver-ui/src/main/java/eu/tsystems/mms/tic/testframework/webdrivermanager/EventLoggingEventDriverListener.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
*/
2222
package eu.tsystems.mms.tic.testframework.webdrivermanager;
2323

24+
import eu.tsystems.mms.tic.testframework.common.Testerra;
2425
import eu.tsystems.mms.tic.testframework.logging.Loggable;
26+
import eu.tsystems.mms.tic.testframework.utils.ExecutionUtils;
2527
import org.openqa.selenium.By;
2628
import org.openqa.selenium.OutputType;
2729
import org.openqa.selenium.WebDriver;
@@ -30,6 +32,8 @@
3032

3133
public class EventLoggingEventDriverListener implements WebDriverEventListener, Loggable {
3234

35+
private static final ExecutionUtils executionUtils = Testerra.getInjector().getInstance(ExecutionUtils.class);
36+
3337
@Override
3438
public void beforeAlertAccept(WebDriver webDriver) {
3539

@@ -131,7 +135,7 @@ public void beforeSwitchToWindow(String s, WebDriver webDriver) {
131135

132136
@Override
133137
public void afterSwitchToWindow(String s, WebDriver webDriver) {
134-
log().info(String.format("Switched to window \"%s\" (%s)", webDriver.getTitle(), webDriver.getCurrentUrl()));
138+
log().info(String.format("Switched to window \"%s\" (%s)", executionUtils.getFailsafe(webDriver::getTitle).orElse("(na)"), executionUtils.getFailsafe(webDriver::getCurrentUrl).orElse("(na)")));
135139
}
136140

137141
@Override

driver-ui/src/main/java/eu/tsystems/mms/tic/testframework/webdrivermanager/IWebDriverManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import java.util.Optional;
3434
import java.util.function.BiConsumer;
3535
import java.util.function.Consumer;
36+
import java.util.function.Function;
3637
import java.util.function.Predicate;
38+
import java.util.function.Supplier;
3739
import java.util.stream.Stream;
3840
import org.openqa.selenium.WebDriver;
3941
import org.openqa.selenium.support.events.EventFiringWebDriver;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Testerra
3+
*
4+
* (C) 2021, Mike Reiche, T-Systems MMS GmbH, Deutsche Telekom AG
5+
*
6+
* Deutsche Telekom AG and all other contributors /
7+
* copyright owners license this file to you under the Apache
8+
* License, Version 2.0 (the "License"); you may not use this
9+
* file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package eu.tsystems.mms.tic.testframework.test.utils;
23+
24+
import eu.tsystems.mms.tic.testframework.common.Testerra;
25+
import eu.tsystems.mms.tic.testframework.internal.utils.ExceptionUtils;
26+
import eu.tsystems.mms.tic.testframework.testing.TesterraTest;
27+
import eu.tsystems.mms.tic.testframework.utils.ExecutionUtils;
28+
import java.util.Optional;
29+
import org.testng.Assert;
30+
import org.testng.annotations.Test;
31+
32+
public class ExecutionUtilsTest extends TesterraTest {
33+
34+
@Test
35+
public void test_getFailsafe() {
36+
String expected = "katze";
37+
ExecutionUtils executionUtils = Testerra.getInjector().getInstance(ExecutionUtils.class);
38+
Optional<Object> failsafe = executionUtils.getFailsafe(() -> {
39+
return expected;
40+
});
41+
42+
Assert.assertEquals(failsafe.get(), expected);
43+
}
44+
45+
@Test
46+
public void test_getFailsafe_null() {
47+
ExecutionUtils executionUtils = Testerra.getInjector().getInstance(ExecutionUtils.class);
48+
Optional<Object> failsafe = executionUtils.getFailsafe(() -> {
49+
return null;
50+
});
51+
52+
Assert.assertFalse(failsafe.isPresent());
53+
}
54+
55+
@Test
56+
public void test_getFailsafe_NPE() {
57+
ExecutionUtils executionUtils = Testerra.getInjector().getInstance(ExecutionUtils.class);
58+
Optional<Object> failsafe = executionUtils.getFailsafe(() -> {
59+
String expected = null;
60+
return expected.trim();
61+
});
62+
63+
Assert.assertFalse(failsafe.isPresent());
64+
}
65+
}

0 commit comments

Comments
 (0)