PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/java/client/test/org/openqa/selenium/support/pagefactory/DefaultFieldDecoratorTest.java

https://bitbucket.org/abahdanovich/selenium
Java | 185 lines | 143 code | 22 blank | 20 comment | 0 complexity | d14dbe45443d9c15a2d018f5c885a912 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 2007-2009 Selenium committers
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package org.openqa.selenium.support.pagefactory;
  14. import static org.hamcrest.core.Is.is;
  15. import static org.hamcrest.core.IsNull.notNullValue;
  16. import static org.hamcrest.core.IsNull.nullValue;
  17. import static org.junit.Assert.assertThat;
  18. import org.openqa.selenium.By;
  19. import org.openqa.selenium.HasInputDevices;
  20. import org.openqa.selenium.testing.MockTestBase;
  21. import org.openqa.selenium.Mouse;
  22. import org.openqa.selenium.WebDriver;
  23. import org.openqa.selenium.WebElement;
  24. import org.openqa.selenium.interactions.Actions;
  25. import org.openqa.selenium.interactions.internal.Coordinates;
  26. import org.openqa.selenium.internal.FindsById;
  27. import org.openqa.selenium.internal.FindsByLinkText;
  28. import org.openqa.selenium.internal.FindsByName;
  29. import org.openqa.selenium.internal.FindsByXPath;
  30. import org.openqa.selenium.internal.Locatable;
  31. import org.openqa.selenium.internal.WrapsElement;
  32. import org.openqa.selenium.support.FindBy;
  33. import org.openqa.selenium.support.FindBys;
  34. import org.openqa.selenium.support.PageFactory;
  35. import org.jmock.Expectations;
  36. import org.junit.Test;
  37. import java.lang.reflect.Field;
  38. import java.util.List;
  39. /**
  40. */
  41. public class DefaultFieldDecoratorTest extends MockTestBase {
  42. // Unusued fields are used by tests. Do not remove!
  43. @SuppressWarnings("unused") private WebElement element1;
  44. @SuppressWarnings("unused") private WebElement element2;
  45. @SuppressWarnings("unused") private List<WebElement> list1;
  46. @SuppressWarnings("unused") private List<Object> list2;
  47. @SuppressWarnings("unused") private Integer num;
  48. @SuppressWarnings("unused")
  49. @FindBy(tagName = "div")
  50. private List<WebElement> list3;
  51. @SuppressWarnings("unused")
  52. @FindBys({@FindBy(tagName = "div"), @FindBy(tagName = "a")})
  53. private List<WebElement> list4;
  54. @SuppressWarnings("unused")
  55. @FindBy(tagName = "div")
  56. private List<Object> list5;
  57. private FieldDecorator createDecoratorWithNullLocator() {
  58. return new DefaultFieldDecorator(new ElementLocatorFactory() {
  59. public ElementLocator createLocator(Field field) {
  60. return null;
  61. }
  62. });
  63. }
  64. private FieldDecorator createDecoratorWithDefaultLocator() {
  65. return new DefaultFieldDecorator(
  66. new DefaultElementLocatorFactory((WebDriver) null));
  67. }
  68. @Test
  69. public void decoratesWebElement() throws Exception {
  70. FieldDecorator decorator = createDecoratorWithDefaultLocator();
  71. assertThat(decorator.decorate(getClass().getClassLoader(),
  72. getClass().getDeclaredField("element1")),
  73. is(notNullValue()));
  74. assertThat(decorator.decorate(getClass().getClassLoader(),
  75. getClass().getDeclaredField("element2")),
  76. is(notNullValue()));
  77. }
  78. @Test
  79. public void decoratesAnnotatedWebElementList() throws Exception {
  80. FieldDecorator decorator = createDecoratorWithDefaultLocator();
  81. assertThat(decorator.decorate(getClass().getClassLoader(),
  82. getClass().getDeclaredField("list3")),
  83. is(notNullValue()));
  84. assertThat(decorator.decorate(getClass().getClassLoader(),
  85. getClass().getDeclaredField("list4")),
  86. is(notNullValue()));
  87. }
  88. @Test
  89. public void doesNotDecorateNonAnnotatedWebElementList() throws Exception {
  90. FieldDecorator decorator = createDecoratorWithDefaultLocator();
  91. assertThat(decorator.decorate(getClass().getClassLoader(),
  92. getClass().getDeclaredField("list1")),
  93. is(nullValue()));
  94. assertThat(decorator.decorate(getClass().getClassLoader(),
  95. getClass().getDeclaredField("list2")),
  96. is(nullValue()));
  97. }
  98. @Test
  99. public void doesNotDecorateNonWebElement() throws Exception {
  100. FieldDecorator decorator = createDecoratorWithDefaultLocator();
  101. assertThat(decorator.decorate(getClass().getClassLoader(),
  102. getClass().getDeclaredField("num")),
  103. is(nullValue()));
  104. }
  105. @Test
  106. public void doesNotDecorateListOfSomethingElse() throws Exception {
  107. FieldDecorator decorator = createDecoratorWithDefaultLocator();
  108. assertThat(decorator.decorate(getClass().getClassLoader(),
  109. getClass().getDeclaredField("list5")),
  110. is(nullValue()));
  111. }
  112. @Test
  113. public void doesNotDecorateNullLocator() throws Exception {
  114. FieldDecorator decorator = createDecoratorWithNullLocator();
  115. assertThat(decorator.decorate(getClass().getClassLoader(),
  116. getClass().getDeclaredField("element1")),
  117. is(nullValue()));
  118. assertThat(decorator.decorate(getClass().getClassLoader(),
  119. getClass().getDeclaredField("element2")),
  120. is(nullValue()));
  121. assertThat(decorator.decorate(getClass().getClassLoader(),
  122. getClass().getDeclaredField("list1")),
  123. is(nullValue()));
  124. assertThat(decorator.decorate(getClass().getClassLoader(),
  125. getClass().getDeclaredField("list2")),
  126. is(nullValue()));
  127. assertThat(decorator.decorate(getClass().getClassLoader(),
  128. getClass().getDeclaredField("num")),
  129. is(nullValue()));
  130. }
  131. @Test
  132. public void testDecoratingProxyImplementsRequiredInterfaces() throws Exception {
  133. final AllDriver driver = mock(AllDriver.class);
  134. final AllElement element = mock(AllElement.class);
  135. final Mouse mouse = mock(Mouse.class);
  136. checking(new Expectations() {{
  137. exactly(1).of(driver).getKeyboard();
  138. exactly(1).of(driver).getMouse();
  139. will(returnValue(mouse));
  140. exactly(1).of(driver).findElement(By.id("foo"));
  141. will(returnValue(element));
  142. exactly(1).of(element).getCoordinates();
  143. exactly(1).of(mouse).mouseMove(with(any(Coordinates.class)));
  144. }});
  145. Page page = new Page();
  146. PageFactory.initElements(driver, page);
  147. new Actions(driver).moveToElement(page.foo).build().perform();
  148. }
  149. private static class Page {
  150. @FindBy(id = "foo")
  151. public WebElement foo;
  152. }
  153. private interface AllDriver extends WebDriver, FindsById, FindsByLinkText, FindsByName,
  154. FindsByXPath, HasInputDevices {
  155. // Place holder
  156. }
  157. private interface AllElement extends WebElement, WrapsElement, Locatable {
  158. // Place holder
  159. }
  160. }