Skip to content

Commit db0bcb3

Browse files
committed
Properly fill out the w3c unexpected alert expcetion alert text
As described in https://w3c.github.io/webdriver/webdriver-spec.html#dfn-handle-any-user-prompts When returning an error with unexpected alert open, a remote end may return the current text (as if returned by Get Alert Text) of the prompt as the value of the text key in the JSON Object that is the value of the data key in the error response, as shown in this non-normative example: ```{ error: "unexpected alert open", message: "implementation defined", data: { text: "the text from the alert" } }```
1 parent 4337919 commit db0bcb3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

java/client/src/org/openqa/selenium/remote/http/W3CHttpResponseCodec.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
import static com.google.common.base.Strings.nullToEmpty;
2121
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
22+
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
2223
import static java.net.HttpURLConnection.HTTP_OK;
2324

2425
import com.google.common.base.Strings;
2526
import com.google.gson.JsonElement;
2627
import com.google.gson.JsonObject;
2728
import com.google.gson.JsonParser;
2829

30+
import org.openqa.selenium.UnhandledAlertException;
2931
import org.openqa.selenium.WebDriverException;
3032
import org.openqa.selenium.remote.ErrorCodes;
3133
import org.openqa.selenium.remote.JsonToBeanConverter;
@@ -96,7 +98,22 @@ public Response decode(HttpResponse encodedResponse) {
9698

9799
response.setState(error);
98100
response.setStatus(errorCodes.toStatus(error, Optional.of(encodedResponse.getStatus())));
99-
response.setValue(createException(error, message));
101+
102+
// For now, we'll inelegantly special case unhandled alerts.
103+
if ("unexpected alert open".equals(error) &&
104+
HTTP_INTERNAL_ERROR == encodedResponse.getStatus()) {
105+
String text = "";
106+
JsonElement data = obj.get("data");
107+
if (data != null) {
108+
JsonElement rawText = data.getAsJsonObject().get("text");
109+
if (rawText != null) {
110+
text = rawText.getAsString();
111+
}
112+
}
113+
response.setValue(new UnhandledAlertException(message, text));
114+
} else {
115+
response.setValue(createException(error, message));
116+
}
100117
return response;
101118
}
102119

java/client/test/org/openqa/selenium/remote/http/W3CHttpResponseCodecTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
import static org.junit.Assert.assertTrue;
2525
import static org.openqa.selenium.remote.ErrorCodes.METHOD_NOT_ALLOWED;
2626

27+
import com.google.common.collect.ImmutableMap;
2728
import com.google.gson.Gson;
2829

2930
import org.junit.Test;
31+
import org.openqa.selenium.UnhandledAlertException;
3032
import org.openqa.selenium.UnsupportedCommandException;
3133
import org.openqa.selenium.WebDriverException;
3234
import org.openqa.selenium.remote.ErrorCodes;
3335
import org.openqa.selenium.remote.Response;
3436

37+
import java.io.Serializable;
3538
import java.util.HashMap;
3639
import java.util.Map;
3740

@@ -89,6 +92,22 @@ public void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForConformingImple
8992
assertTrue(((WebDriverException) decoded.getValue()).getMessage().contains("I like peas"));
9093
}
9194

95+
@Test
96+
public void shouldPopulateTheAlertTextIfThrowingAnUnhandledAlertException() {
97+
ImmutableMap<String, ImmutableMap<String, Serializable>> data = ImmutableMap.of(
98+
"value", ImmutableMap.of(
99+
"error", "unexpected alert open",
100+
"message", "Modal dialog present",
101+
"stacktrace", "",
102+
"data", ImmutableMap.of("text", "cheese")));
103+
104+
HttpResponse response = createValidResponse(500, data);
105+
Response decoded = new W3CHttpResponseCodec().decode(response);
106+
107+
UnhandledAlertException ex = (UnhandledAlertException) decoded.getValue();
108+
assertEquals("cheese", ex.getAlertText());
109+
}
110+
92111
private HttpResponse createValidResponse(int statusCode, Map<String, ?> data) {
93112
byte[] contents = new Gson().toJson(data).getBytes(UTF_8);
94113

0 commit comments

Comments
 (0)