Skip to content

Commit 85eb68d

Browse files
committed
Add a public serialisation field to the UnhandledAlertException
How this exception is serialised isn't documented, but some local end bindings (such as python and .Net) expect it to be serialised as a JSON Object. By adding this accessor, the BeanToJsonConverter will add the right property.
1 parent a9da1a0 commit 85eb68d

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

java/client/src/org/openqa/selenium/BUCK

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ java_library(name = 'exceptions',
113113
'WebDriverException.java',
114114
'internal/BuildInfo.java',
115115
],
116+
deps = [
117+
':beta',
118+
],
116119
visibility = [
117120
'//java/client/src/org/openqa/selenium/interactions:exceptions',
118121
'//java/client/src/org/openqa/selenium/io:io',
119122
'//java/client/src/org/openqa/selenium/net:net',
120123
'//java/client/src/org/openqa/selenium/os:os',
121124
'//java/client/test/org/openqa/selenium/testing:helpers',
122125
],
123-
deps = [
124-
'//third_party/java/gson:gson',
125-
],
126126
)
127127

128128
java_library(

java/client/src/org/openqa/selenium/UnhandledAlertException.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package org.openqa.selenium;
1919

20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
2024
public class UnhandledAlertException extends WebDriverException {
2125

2226
private final String alertText;
@@ -36,4 +40,12 @@ public UnhandledAlertException(String message, String alertText) {
3640
public String getAlertText() {
3741
return alertText;
3842
}
43+
44+
// Used for serialising. Some of the drivers return the alert text like this.
45+
@Beta
46+
public Map<String, String> getAlert() {
47+
HashMap<String, String> toReturn = new HashMap<>();
48+
toReturn.put("text", getAlertText());
49+
return Collections.unmodifiableMap(toReturn);
50+
}
3951
}

java/client/test/org/openqa/selenium/remote/BeanToJsonConverterTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import com.google.common.collect.ImmutableMap;
3131
import com.google.common.collect.Lists;
3232
import com.google.common.collect.Maps;
33+
import com.google.gson.Gson;
34+
import com.google.gson.GsonBuilder;
3335
import com.google.gson.JsonArray;
3436
import com.google.gson.JsonObject;
3537
import com.google.gson.JsonParser;
@@ -41,13 +43,15 @@
4143
import org.openqa.selenium.Cookie;
4244
import org.openqa.selenium.Platform;
4345
import org.openqa.selenium.Proxy;
46+
import org.openqa.selenium.UnhandledAlertException;
4447
import org.openqa.selenium.WebDriverException;
4548
import org.openqa.selenium.logging.LogEntries;
4649
import org.openqa.selenium.logging.LogEntry;
4750
import org.openqa.selenium.logging.LogType;
4851
import org.openqa.selenium.logging.LoggingPreferences;
4952

5053
import java.awt.*;
54+
import java.io.StringReader;
5155
import java.util.Date;
5256
import java.util.HashMap;
5357
import java.util.HashSet;
@@ -327,6 +331,16 @@ public void testShouldBeAbleToConvertAWebDriverException() {
327331
verifyStackTraceInJson(raw, stackTrace);
328332
}
329333

334+
@Test
335+
public void testShouldConverUnhandledAlertException() {
336+
RuntimeException clientError = new UnhandledAlertException("unhandled alert", "cheese!");
337+
Map<?, ?> obj = new Gson()
338+
.fromJson(new StringReader(new BeanToJsonConverter().convert(clientError)), Map.class);
339+
assertTrue(obj.containsKey("alert"));
340+
assertEquals(ImmutableMap.of("text", "cheese!"), obj.get("alert"));
341+
}
342+
343+
330344
@Test
331345
public void testShouldConvertDatesToMillisecondsInUtcTime() {
332346
String jsonStr = new BeanToJsonConverter().convert(new Date(0));

0 commit comments

Comments
 (0)