PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/java/client/test/org/openqa/selenium/support/PageFactoryTest.java

https://bitbucket.org/abahdanovich/selenium
Java | 242 lines | 160 code | 63 blank | 19 comment | 0 complexity | 578042bb73304989f8956027537fde8c 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;
  14. import static org.hamcrest.core.Is.is;
  15. import static org.hamcrest.core.IsEqual.equalTo;
  16. import static org.hamcrest.core.IsNull.notNullValue;
  17. import static org.hamcrest.core.IsNull.nullValue;
  18. import static org.junit.Assert.assertThat;
  19. import static org.junit.Assert.fail;
  20. import org.junit.Assert;
  21. import org.junit.Test;
  22. import org.openqa.selenium.WebDriver;
  23. import org.openqa.selenium.WebElement;
  24. import org.openqa.selenium.support.pagefactory.FieldDecorator;
  25. import org.openqa.selenium.testing.MockTestBase;
  26. import java.lang.reflect.Field;
  27. import java.util.List;
  28. public class PageFactoryTest extends MockTestBase {
  29. private WebDriver driver = null;
  30. @Test
  31. public void shouldProxyElementsInAnInstantiatedPage() {
  32. PublicPage page = new PublicPage();
  33. Assert.assertThat(page.q, is(nullValue()));
  34. assertThat(page.list, is(nullValue()));
  35. PageFactory.initElements(driver, page);
  36. assertThat(page.q, is(notNullValue()));
  37. assertThat(page.list, is(notNullValue()));
  38. }
  39. @Test
  40. public void shouldInsertProxiesForPublicWebElements() {
  41. PublicPage page = PageFactory.initElements(driver, PublicPage.class);
  42. assertThat(page.q, is(notNullValue()));
  43. assertThat(page.list, is(notNullValue()));
  44. }
  45. @Test
  46. public void shouldProxyElementsFromParentClassesToo() {
  47. ChildPage page = new ChildPage();
  48. PageFactory.initElements(driver, page);
  49. assertThat(page.q, is(notNullValue()));
  50. assertThat(page.list, is(notNullValue()));
  51. assertThat(page.submit, is(notNullValue()));
  52. }
  53. @Test
  54. public void shouldProxyRenderedWebElementFields() {
  55. PublicPage page = PageFactory.initElements(driver, PublicPage.class);
  56. assertThat(page.rendered, is(notNullValue()));
  57. }
  58. @Test
  59. public void shouldProxyPrivateElements() {
  60. PrivatePage page = new PrivatePage();
  61. PageFactory.initElements(driver, page);
  62. assertThat(page.getField(), is(notNullValue()));
  63. assertThat(page.getList(), is(notNullValue()));
  64. }
  65. @Test
  66. public void shouldUseAConstructorThatTakesAWebDriverAsAnArgument() {
  67. driver = mock(WebDriver.class);
  68. ConstructedPage page = PageFactory.initElements(driver, ConstructedPage.class);
  69. assertThat(driver, equalTo(page.driver));
  70. }
  71. @Test
  72. public void shouldNotDecorateFieldsWhenTheFieldDecoratorReturnsNull() {
  73. PublicPage page = new PublicPage();
  74. // Assign not-null values
  75. WebElement q = mock(WebElement.class);
  76. page.q = q;
  77. PageFactory.initElements(new FieldDecorator() {
  78. public Object decorate(ClassLoader loader, Field field) {
  79. return null;
  80. }
  81. }, page);
  82. assertThat(page.q, equalTo(q));
  83. }
  84. @Test
  85. public void triesToDecorateNonWebElements() {
  86. NonWebElementsPage page = new NonWebElementsPage();
  87. // Assign not-null values
  88. PageFactory.initElements(new FieldDecorator() {
  89. public Object decorate(ClassLoader loader, Field field) {
  90. return new Integer(5);
  91. }
  92. }, page);
  93. assertThat(page.num, equalTo(new Integer(5)));
  94. }
  95. @Test
  96. public void shouldNotDecorateListsOfWebElementsThatAreNotAnnotated() {
  97. UnmarkedListPage page = new UnmarkedListPage();
  98. PageFactory.initElements(driver, page);
  99. assertThat(page.elements, is(nullValue()));
  100. }
  101. @Test
  102. public void shouldNotDecorateListsThatAreTypedButNotWebElementLists() {
  103. UnmarkedListPage page = new UnmarkedListPage();
  104. PageFactory.initElements(driver, page);
  105. assertThat(page.objects, is(nullValue()));
  106. }
  107. @Test
  108. public void shouldNotDecorateUnTypedLists() {
  109. UnmarkedListPage page = new UnmarkedListPage();
  110. PageFactory.initElements(driver, page);
  111. assertThat(page.untyped, is(nullValue()));
  112. }
  113. @Test
  114. public void shouldComplainWhenMoreThanOneFindByAttributeIsSet() {
  115. GrottyPage page = new GrottyPage();
  116. try {
  117. PageFactory.initElements((WebDriver) null, page);
  118. fail("Should not have allowed page to be initialised");
  119. } catch (IllegalArgumentException e) {
  120. // this is expected
  121. }
  122. }
  123. @Test
  124. public void shouldComplainWhenMoreThanOneFindByShortFormAttributeIsSet() {
  125. GrottyPage2 page = new GrottyPage2();
  126. try {
  127. PageFactory.initElements((WebDriver) null, page);
  128. fail("Should not have allowed page to be initialised");
  129. } catch (IllegalArgumentException e) {
  130. // this is expected
  131. }
  132. }
  133. public static class PublicPage {
  134. @FindBy(name = "q")
  135. public WebElement q;
  136. @FindBy(name = "q")
  137. public List<WebElement> list;
  138. public WebElement rendered;
  139. }
  140. public static class ChildPage extends PublicPage {
  141. public WebElement submit;
  142. }
  143. public static class ConstructedPage {
  144. public WebDriver driver;
  145. public ConstructedPage(WebDriver driver) {
  146. this.driver = driver;
  147. }
  148. }
  149. public static class PrivatePage {
  150. private WebElement allMine = null;
  151. @FindBy(name = "q")
  152. private List<WebElement> list = null;
  153. public WebElement getField() {
  154. return allMine;
  155. }
  156. public List<WebElement> getList() {
  157. return list;
  158. }
  159. }
  160. public static class GrottyPage {
  161. @FindBy(how = How.XPATH, using = "//body", id = "cheese")
  162. private WebElement one;
  163. }
  164. public static class GrottyPage2 {
  165. @FindBy(xpath = "//body", id = "cheese")
  166. private WebElement two;
  167. }
  168. public static class UnmarkedListPage {
  169. private List<WebElement> elements;
  170. private List<Object> objects;
  171. private List untyped; // This list deliberately left untyped
  172. }
  173. public static class NonWebElementsPage {
  174. public Integer num;
  175. }
  176. }