Skip to content

Commit 7483c3b

Browse files
committed
Implement retry behavior within WebPage#get call
1 parent 5a000f9 commit 7483c3b

File tree

7 files changed

+124
-2
lines changed
  • dev
    • com.ibm.ws.jsf.2.2_fat.2/fat/src/com/ibm/ws/jsf22/fat
    • com.ibm.ws.jsf.2.2_fat/fat/src/com/ibm/ws/jsf22/fat
    • com.ibm.ws.jsf.2.3_fat.2/fat/src/com/ibm/ws/jsf23/fat
    • com.ibm.ws.jsf.2.3_fat/fat/src/com/ibm/ws/jsf23/fat
    • io.openliberty.faces.fat.selenium.util.internal/src/io/openliberty/faces/fat/selenium/util/internal
    • io.openliberty.org.apache.myfaces.4.0_fat/fat/src/io/openliberty/org/apache/myfaces40/fat

7 files changed

+124
-2
lines changed

dev/com.ibm.ws.jsf.2.2_fat.2/fat/src/com/ibm/ws/jsf22/fat/FATSuite.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ public static ExtendedWebDriver getWebDriver() throws Exception {
122122
if(DRIVER == null) {
123123
throw new Exception("Failed to initialize WebDriver after multiple attempts! See log for details.");
124124
}
125+
126+
// Register the driver reset callback statically for the WebPage to use if exceptions occur.
127+
WebPage.setDriverResetCallback(() -> {
128+
Log.info(c, "DriverResetCallback", "Resetting WebDriver due to some error. See log for details");
129+
if (DRIVER != null) {
130+
try {
131+
DRIVER.quit();
132+
} catch (Exception e) {
133+
// no-op
134+
}
135+
DRIVER = null;
136+
}
137+
DRIVER = getWebDriver();
138+
return DRIVER;
139+
});
140+
125141
return DRIVER;
126142
}
127143

dev/com.ibm.ws.jsf.2.2_fat/fat/src/com/ibm/ws/jsf22/fat/FATSuite.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ public static ExtendedWebDriver getWebDriver() throws Exception {
133133
if(DRIVER == null) {
134134
throw new Exception("Failed to initialize WebDriver after multiple attempts! See log for details.");
135135
}
136+
137+
// Register the driver reset callback statically for the WebPage to use if exceptions occur.
138+
WebPage.setDriverResetCallback(() -> {
139+
Log.info(c, "DriverResetCallback", "Resetting WebDriver due to some error. See log for details");
140+
if (DRIVER != null) {
141+
try {
142+
DRIVER.quit();
143+
} catch (Exception e) {
144+
// no-op
145+
}
146+
DRIVER = null;
147+
}
148+
DRIVER = getWebDriver();
149+
return DRIVER;
150+
});
151+
136152
return DRIVER;
137153
}
138154

dev/com.ibm.ws.jsf.2.3_fat.2/fat/src/com/ibm/ws/jsf23/fat/FATSuite.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ public static ExtendedWebDriver getWebDriver() throws Exception {
107107
if(DRIVER == null) {
108108
throw new Exception("Failed to initialize WebDriver after multiple attempts! See log for details.");
109109
}
110+
111+
// Register the driver reset callback statically for the WebPage to use if exceptions occur.
112+
WebPage.setDriverResetCallback(() -> {
113+
Log.info(c, "DriverResetCallback", "Resetting WebDriver due to some error. See log for details");
114+
if (DRIVER != null) {
115+
try {
116+
DRIVER.quit();
117+
} catch (Exception e) {
118+
// no-op
119+
}
120+
DRIVER = null;
121+
}
122+
DRIVER = getWebDriver();
123+
return DRIVER;
124+
});
125+
110126
return DRIVER;
111127
}
112128

dev/com.ibm.ws.jsf.2.3_fat/fat/src/com/ibm/ws/jsf23/fat/FATSuite.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@ public static ExtendedWebDriver getWebDriver() throws Exception {
139139
if(DRIVER == null) {
140140
throw new Exception("Failed to initialize WebDriver after multiple attempts! See log for details.");
141141
}
142+
143+
// Register the driver reset callback statically for the WebPage to use if exceptions occur.
144+
WebPage.setDriverResetCallback(() -> {
145+
Log.info(c, "DriverResetCallback", "Resetting WebDriver due to some error. See log for details");
146+
if (DRIVER != null) {
147+
try {
148+
DRIVER.quit();
149+
} catch (Exception e) {
150+
// no-op
151+
}
152+
DRIVER = null;
153+
}
154+
DRIVER = getWebDriver();
155+
return DRIVER;
156+
});
157+
142158
return DRIVER;
143159
}
144160

dev/io.openliberty.faces.fat.selenium.util.internal/src/io/openliberty/faces/fat/selenium/util/internal/WebPage.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ public class WebPage {
3838
public static final Duration LONG_TIMEOUT = Duration.ofMillis(16000);
3939

4040
protected ExtendedWebDriver webDriver;
41+
private static DriverResetCallback driverResetCallback = null;
42+
43+
/*
44+
* Interface to reset the driver, see setDriverResetCallback
45+
*/
46+
@FunctionalInterface
47+
public interface DriverResetCallback {
48+
ExtendedWebDriver resetDriver() throws Exception;
49+
}
50+
51+
/*
52+
* Set a callback to reset the driver
53+
* Must be called once within the FATSuite during driver initialization
54+
*/
55+
public static void setDriverResetCallback(DriverResetCallback callback) {
56+
driverResetCallback = callback;
57+
}
4158

4259
public WebPage(ExtendedWebDriver webDriver) {
4360
this.webDriver = webDriver;
@@ -52,7 +69,30 @@ public void setWebDriver(ExtendedWebDriver webDriver) {
5269
}
5370

5471
public void get(String url) {
55-
webDriver.get(url);
72+
int maxAttempts = 2;
73+
Exception lastException = null;
74+
75+
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
76+
try {
77+
webDriver.get(url);
78+
return;
79+
} catch (Exception e) {
80+
lastException = e;
81+
82+
// callback exists and we have attempts left
83+
if (driverResetCallback != null && attempt < maxAttempts) {
84+
try {
85+
Thread.sleep(10000); // Wait 10 seconds before retrying if it's some network issue.
86+
webDriver = driverResetCallback.resetDriver();
87+
} catch (Exception resetEx) {
88+
// no-op, as we may have attempts left
89+
}
90+
} else if (attempt == maxAttempts) {
91+
// Last attempt failed, throw exception
92+
throw new RuntimeException("Failed to navigate to URL after " + maxAttempts + " attempts: " + url, lastException);
93+
}
94+
}
95+
}
5696
}
5797

5898
public String getTitle() {

dev/io.openliberty.org.apache.myfaces.4.0_fat/fat/src/io/openliberty/org/apache/myfaces40/fat/FATSuite.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import io.openliberty.org.apache.myfaces40.fat.tests.bugfixes.MyFaces4628Test;
5757
import io.openliberty.org.apache.myfaces40.fat.tests.bugfixes.MyFaces4658Test;
5858

59+
import io.openliberty.faces.fat.selenium.util.internal.WebPage;
60+
5961
@RunWith(Suite.class)
6062
@SuiteClasses({
6163
AcceptInputFileTest.class,
@@ -152,6 +154,22 @@ public static ExtendedWebDriver getWebDriver() throws Exception {
152154
if(DRIVER == null) {
153155
throw new Exception("Failed to initialize WebDriver after multiple attempts! See log for details.");
154156
}
157+
158+
// Register the driver reset callback statically for the WebPage to use if exceptions occur.
159+
WebPage.setDriverResetCallback(() -> {
160+
Log.info(c, "DriverResetCallback", "Resetting WebDriver due to some error. See log for details");
161+
if (DRIVER != null) {
162+
try {
163+
DRIVER.quit();
164+
} catch (Exception e) {
165+
// no-op
166+
}
167+
DRIVER = null;
168+
}
169+
DRIVER = getWebDriver();
170+
return DRIVER;
171+
});
172+
155173
return DRIVER;
156174
}
157175

dev/io.openliberty.org.apache.myfaces.4.0_fat/fat/src/io/openliberty/org/apache/myfaces40/fat/tests/WebSocketTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void testOnErrorWebsocket() throws Exception {
8787

8888
String contextRoot = "WebSocket";
8989

90-
String url = JSFUtils.createSeleniumURLString(server, contextRoot, "OnErrorWebSocketTest.jsf");;
90+
String url = JSFUtils.createSeleniumURLString(server, contextRoot, "OnErrorWebSocketTest.jsf");
9191
WebPage page = new WebPage(driver);
9292
page.get(url);
9393
page.waitForPageToLoad();

0 commit comments

Comments
 (0)