PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/abahdanovich/selenium
Java | 336 lines | 253 code | 64 blank | 19 comment | 0 complexity | 40e6f1262253dd590d3e7ecc779a110b MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-1.0, MIT, Apache-2.0, BSD-3-Clause, GPL-2.0
  1. /*
  2. Copyright 2012 Selenium committers
  3. Copyright 2012 Software Freedom Conservancy
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package org.openqa.selenium.remote.internal;
  15. import static org.hamcrest.Matchers.instanceOf;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNull;
  18. import static org.junit.Assert.assertThat;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import com.google.common.collect.ImmutableList;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.google.common.collect.Lists;
  24. import org.openqa.selenium.By;
  25. import org.openqa.selenium.Dimension;
  26. import org.openqa.selenium.Point;
  27. import org.openqa.selenium.WebElement;
  28. import org.openqa.selenium.internal.WrapsElement;
  29. import org.openqa.selenium.remote.RemoteWebElement;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.junit.runners.JUnit4;
  33. import java.util.Collection;
  34. import java.util.List;
  35. import java.util.Map;
  36. /**
  37. * Unit tests for {@link WebElementToJsonConverter}.
  38. */
  39. @RunWith(JUnit4.class)
  40. public class WebElementToJsonConverterTest {
  41. private static final WebElementToJsonConverter CONVERTER = new WebElementToJsonConverter();
  42. @Test
  43. public void returnsPrimitivesAsIs() {
  44. assertNull(CONVERTER.apply(null));
  45. assertEquals("abc", CONVERTER.apply("abc"));
  46. assertEquals(Boolean.TRUE, CONVERTER.apply(Boolean.TRUE));
  47. assertEquals(Integer.valueOf(123), CONVERTER.apply(123));
  48. assertEquals(Math.PI, CONVERTER.apply(Math.PI));
  49. }
  50. @Test
  51. public void convertsRemoteWebElementToWireProtocolMap() {
  52. RemoteWebElement element = new RemoteWebElement();
  53. element.setId("abc123");
  54. Object value = CONVERTER.apply(element);
  55. assertIsWebElementObject(value, "abc123");
  56. }
  57. @Test
  58. public void unwrapsWrappedElements() {
  59. RemoteWebElement element = new RemoteWebElement();
  60. element.setId("abc123");
  61. Object value = CONVERTER.apply(wrapElement(element));
  62. assertIsWebElementObject(value, "abc123");
  63. }
  64. @Test
  65. public void unwrapsWrappedElements_multipleLevelsOfWrapping() {
  66. RemoteWebElement element = new RemoteWebElement();
  67. element.setId("abc123");
  68. WrappingWebElement wrapped = wrapElement(element);
  69. wrapped = wrapElement(wrapped);
  70. wrapped = wrapElement(wrapped);
  71. wrapped = wrapElement(wrapped);
  72. Object value = CONVERTER.apply(wrapped);
  73. assertIsWebElementObject(value, "abc123");
  74. }
  75. @Test
  76. public void convertsSimpleCollections() {
  77. Object converted = CONVERTER.apply(Lists.newArrayList(null, "abc", true, 123, Math.PI));
  78. assertThat(converted, instanceOf(Collection.class));
  79. List<?> list = Lists.newArrayList((Collection<?>) converted);
  80. assertContentsInOrder(list, null, "abc", true, 123, Math.PI);
  81. }
  82. @Test
  83. public void convertsNestedCollections_simpleValues() {
  84. List<?> innerList = Lists.newArrayList(123, "abc");
  85. List<Object> outerList = Lists.newArrayList((Object) "apples", "oranges");
  86. outerList.add(innerList);
  87. Object converted = CONVERTER.apply(outerList);
  88. assertThat(converted, instanceOf(Collection.class));
  89. List<?> list = ImmutableList.copyOf((Collection<?>) converted);
  90. assertEquals(3, list.size());
  91. assertEquals("apples", list.get(0));
  92. assertEquals("oranges", list.get(1));
  93. assertThat(list.get(2), instanceOf(Collection.class));
  94. list = ImmutableList.copyOf((Collection<?>) list.get(2));
  95. assertContentsInOrder(list, 123, "abc");
  96. }
  97. @Test
  98. public void requiresMapsToHaveStringKeys() {
  99. try {
  100. CONVERTER.apply(ImmutableMap.of(new Object(), "bunny"));
  101. fail();
  102. } catch (IllegalArgumentException expected) {
  103. }
  104. }
  105. @Test
  106. public void requiresNestedMapsToHaveStringKeys() {
  107. try {
  108. CONVERTER.apply(ImmutableMap.of(
  109. "one", ImmutableMap.of(
  110. "two", ImmutableMap.of(
  111. Integer.valueOf(3), "not good"))));
  112. fail();
  113. } catch (IllegalArgumentException expected) {
  114. }
  115. }
  116. @Test
  117. public void convertsASimpleMap() {
  118. Object converted = CONVERTER.apply(ImmutableMap.of(
  119. "one", 1,
  120. "fruit", "apples",
  121. "honest", true));
  122. assertThat(converted, instanceOf(Map.class));
  123. @SuppressWarnings("unchecked")
  124. Map<String, Object> map = (Map<String, Object>) converted;
  125. assertEquals(3, map.size());
  126. assertEquals(1, map.get("one"));
  127. assertEquals("apples", map.get("fruit"));
  128. assertEquals(true, map.get("honest"));
  129. }
  130. @SuppressWarnings("unchecked")
  131. @Test
  132. public void convertsANestedMap() {
  133. Object converted = CONVERTER.apply(ImmutableMap.of(
  134. "one", 1,
  135. "fruit", "apples",
  136. "honest", true,
  137. "nested", ImmutableMap.of("bugs", "bunny")));
  138. assertThat(converted, instanceOf(Map.class));
  139. Map<String, Object> map = (Map<String, Object>) converted;
  140. assertEquals(4, map.size());
  141. assertEquals(1, map.get("one"));
  142. assertEquals("apples", map.get("fruit"));
  143. assertEquals(true, map.get("honest"));
  144. assertThat(map.get("nested"), instanceOf(Map.class));
  145. map = (Map<String, Object>) map.get("nested");
  146. assertEquals(1, map.size());
  147. assertEquals("bunny", map.get("bugs"));
  148. }
  149. @SuppressWarnings("unchecked")
  150. @Test
  151. public void convertsAListWithAWebElement() {
  152. RemoteWebElement element = new RemoteWebElement();
  153. element.setId("abc123");
  154. RemoteWebElement element2 = new RemoteWebElement();
  155. element2.setId("anotherId");
  156. Object value = CONVERTER.apply(Lists.newArrayList(element, element2));
  157. assertThat(value, instanceOf(Collection.class));
  158. List<Object> list = Lists.newArrayList((Collection<Object>) value);
  159. assertEquals(2, list.size());
  160. assertIsWebElementObject(list.get(0), "abc123");
  161. assertIsWebElementObject(list.get(1), "anotherId");
  162. }
  163. @SuppressWarnings("unchecked")
  164. @Test
  165. public void convertsAMapWithAWebElement() {
  166. RemoteWebElement element = new RemoteWebElement();
  167. element.setId("abc123");
  168. Object value = CONVERTER.apply(ImmutableMap.of("one", element));
  169. assertThat(value, instanceOf(Map.class));
  170. Map<String, Object> map = (Map<String, Object>) value;
  171. assertEquals(1, map.size());
  172. assertIsWebElementObject(map.get("one"), "abc123");
  173. }
  174. @SuppressWarnings("unchecked")
  175. @Test
  176. public void convertsAnArray() {
  177. Object value = CONVERTER.apply(new Object[] {
  178. "abc123", true, 123, Math.PI
  179. });
  180. assertThat(value, instanceOf(Collection.class));
  181. assertContentsInOrder(Lists.newArrayList((Collection) value),
  182. "abc123", true, 123, Math.PI);
  183. }
  184. @SuppressWarnings("unchecked")
  185. @Test
  186. public void convertsAnArrayWithAWebElement() {
  187. RemoteWebElement element = new RemoteWebElement();
  188. element.setId("abc123");
  189. Object value = CONVERTER.apply(new Object[] { element });
  190. assertContentsInOrder(Lists.newArrayList((Collection) value),
  191. ImmutableMap.of("ELEMENT", "abc123"));
  192. }
  193. @Test
  194. public void rejectsUnrecognizedTypes() {
  195. try {
  196. CONVERTER.apply(new Object());
  197. fail();
  198. } catch (IllegalArgumentException expected) {
  199. }
  200. }
  201. private static WrappingWebElement wrapElement(WebElement element) {
  202. return new WrappingWebElement(element);
  203. }
  204. private static void assertIsWebElementObject(Object value, String expectedKey) {
  205. assertThat(value, instanceOf(Map.class));
  206. Map<?, ?> map = (Map<?, ?>) value;
  207. assertEquals(1, map.size());
  208. assertTrue(map.containsKey("ELEMENT"));
  209. assertEquals(expectedKey, map.get("ELEMENT"));
  210. }
  211. private static void assertContentsInOrder(List<?> list, Object... expectedContents) {
  212. List<Object> expected = Lists.newArrayList(expectedContents);
  213. assertEquals(expected, list);
  214. }
  215. private static class WrappingWebElement implements WebElement, WrapsElement {
  216. private WebElement element;
  217. public WrappingWebElement(WebElement element) {
  218. this.element = element;
  219. }
  220. public WebElement getWrappedElement() {
  221. return element;
  222. }
  223. public void click() {
  224. throw new UnsupportedOperationException();
  225. }
  226. public void submit() {
  227. throw new UnsupportedOperationException();
  228. }
  229. public void sendKeys(CharSequence... keysToSend) {
  230. throw new UnsupportedOperationException();
  231. }
  232. public void clear() {
  233. throw new UnsupportedOperationException();
  234. }
  235. public String getTagName() {
  236. throw new UnsupportedOperationException();
  237. }
  238. public String getAttribute(String name) {
  239. throw new UnsupportedOperationException();
  240. }
  241. public boolean isSelected() {
  242. throw new UnsupportedOperationException();
  243. }
  244. public boolean isEnabled() {
  245. throw new UnsupportedOperationException();
  246. }
  247. public String getText() {
  248. throw new UnsupportedOperationException();
  249. }
  250. public List<WebElement> findElements(By by) {
  251. throw new UnsupportedOperationException();
  252. }
  253. public WebElement findElement(By by) {
  254. throw new UnsupportedOperationException();
  255. }
  256. public boolean isDisplayed() {
  257. throw new UnsupportedOperationException();
  258. }
  259. public Point getLocation() {
  260. throw new UnsupportedOperationException();
  261. }
  262. public Dimension getSize() {
  263. throw new UnsupportedOperationException();
  264. }
  265. public String getCssValue(String propertyName) {
  266. throw new UnsupportedOperationException();
  267. }
  268. }
  269. }