Skip to content

Commit b8b2f61

Browse files
authored
issue-1533899181 (#11551)
* issue-1533899181 solve array ClassCastException * replace var (jeps-286) with int * Update WebElementToJsonConverterTest.java Add test code for specific type of array * Update WebElementToJsonConverter.java --------- Co-authored-by: Diego Molina <[email protected]> Fixes #11550
1 parent 57a4060 commit b8b2f61

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

java/src/org/openqa/selenium/remote/internal/WebElementToJsonConverter.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import org.openqa.selenium.WrapsElement;
2222
import org.openqa.selenium.remote.Dialect;
2323
import org.openqa.selenium.remote.RemoteWebElement;
24-
24+
import java.util.ArrayList;
25+
import java.util.List;
2526
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.HashMap;
2829
import java.util.Map;
2930
import java.util.function.Function;
30-
31+
import java.lang.reflect.Array;
3132
import static java.util.stream.Collectors.toList;
3233

3334
/**
@@ -58,7 +59,7 @@ public Object apply(Object arg) {
5859
}
5960

6061
if (arg.getClass().isArray()) {
61-
arg = Arrays.asList((Object[]) arg);
62+
arg = arrayToList(arg);
6263
}
6364

6465
if (arg instanceof Collection<?>) {
@@ -83,4 +84,12 @@ public Object apply(Object arg) {
8384
throw new IllegalArgumentException(
8485
"Argument is of an illegal type: " + arg.getClass().getName());
8586
}
87+
88+
private static List<Object> arrayToList(Object array) {
89+
List<Object> list = new ArrayList<>();
90+
for (int i = 0; i < Array.getLength(array); i++) {
91+
list.add(Array.get(array, i));
92+
}
93+
return list;
94+
}
8695
}

java/test/org/openqa/selenium/remote/internal/WebElementToJsonConverterTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,18 @@ void convertsAMapWithAWebElement() {
191191

192192
@Test
193193
void convertsAnArray() {
194-
Object value = CONVERTER.apply(new Object[] {
194+
Object value1 = CONVERTER.apply(new Object[] {
195195
"abc123", true, 123, Math.PI
196196
});
197197

198-
assertThat(value).isInstanceOf(Collection.class);
199-
assertContentsInOrder(new ArrayList<>((Collection<?>) value),
198+
assertThat(value1).isInstanceOf(Collection.class);
199+
assertContentsInOrder(new ArrayList<>((Collection<?>) value1),
200200
"abc123", true, 123, Math.PI);
201+
202+
Object value2 = CONVERTER.apply(new int[] { 123, 456, 789 });
203+
204+
assertThat(value2).isInstanceOf(Collection.class);
205+
assertContentsInOrder(new ArrayList<>((Collection<?>) value2), 123, 456, 789);
201206
}
202207

203208
@Test

0 commit comments

Comments
 (0)