Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. package com.accelior.mars.test;
  2.  
  3. import com.accelior.mars.test.bo.enumerators.PageUrlEnum;
  4. import com.accelior.mars.test.utility.ConfigProperties;
  5. import org.openqa.selenium.*;
  6. import org.openqa.selenium.support.FindBy;
  7. import org.openqa.selenium.support.ui.Wait;
  8. import org.openqa.selenium.support.ui.WebDriverWait;
  9.  
  10. import java.lang.reflect.Field;
  11. import java.util.concurrent.TimeUnit;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14.  
  15. public abstract class BasePage extends ContainerWebDriver {
  16.  
  17. /** Common class for all pages **/
  18.  
  19. private final PageUrlEnum pageUrl;
  20. public Wait<WebDriver> waitFor = new WebDriverWait(driver, 4, 500)
  21. .withMessage("Element was not found");
  22.  
  23. /**
  24. * @param pageUrl
  25. * url of testing page
  26. **/
  27. public BasePage(WebDriver driver, PageUrlEnum pageUrl) {
  28. super(driver);
  29. this.pageUrl = pageUrl;
  30. }
  31.  
  32. protected void type(WebElement webElement, String text) {
  33. webElement.clear();
  34. webElement.sendKeys(text);
  35.  
  36. }
  37.  
  38. public void open() {
  39. System.out.println("Open page: " + getPageFullUrl());
  40. driver.get(getPageFullUrl());
  41. }
  42.  
  43. public < T extends BasePage> T refresh() {
  44. System.out.println("Refreshing page...");
  45. driver.navigate().refresh();
  46. return (T) this;
  47. }
  48.  
  49. public String getPageFullUrl() {
  50. return pageUrl.getUrl();
  51. }
  52.  
  53. public boolean isAlertPresent() {
  54. try {
  55. driver.switchTo().alert();
  56. return true;
  57. }
  58. catch (NoAlertPresentException Ex) {
  59. return false;
  60. }
  61. }
  62.  
  63. public boolean isElementVisible(By by) {
  64. try {
  65. driver.findElement(by).isDisplayed();
  66. return true;
  67. } catch (NoSuchElementException ignored) {
  68. return false;
  69. } catch (ElementNotVisibleException ignored) {
  70. return false;
  71. } catch (StaleElementReferenceException ignored) {
  72. return false;
  73. }
  74. }
  75.  
  76. public boolean isElementVisible(WebElement element) {
  77. try {
  78. element.isDisplayed();
  79. return true;
  80. } catch (NoSuchElementException ignored) {
  81. return false;
  82. } catch (ElementNotVisibleException ignored) {
  83. return false;
  84. } catch (StaleElementReferenceException ignored) {
  85. return false;
  86. }
  87. }
  88.  
  89.  
  90.  
  91. /**
  92. * @return true if element is absent, and false if element is present
  93. ** @param locator
  94. * of element, for example isElementAbsent(By.xpath(
  95. * "//a[text()='Forgot your password?']")
  96. **/
  97. public boolean isElementAbsent(By locator) {
  98. if (isElementPresent(locator)) {
  99. System.out.println("False: Element " + locator + " is present");
  100. return false;
  101. } else {
  102. System.out.println("True: Element " + locator + " is absent");
  103. return true;
  104. }
  105. }
  106.  
  107. /**
  108. * @return true if element is present, and false if element is absent
  109. * @param locator
  110. * of element, for example isElementPresent(By.xpath(
  111. * "//a[text()='Forgot your password?']")
  112. **/
  113. public boolean isElementPresent(By locator) {
  114. driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  115. boolean result = driver.findElements(locator).size() > 0;
  116. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  117. return result;
  118. }
  119.  
  120. /** @return true if element is present, and false if element is absent. Waiting for 5sec **/
  121. public boolean isElementPresent(WebElement element) {
  122. boolean result = false;
  123. driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  124. try {
  125. element.getTagName();
  126. result = true;
  127. } catch (NoSuchElementException e) {
  128. System.out.println(element + " wasn't found");
  129. }
  130. driver.manage().timeouts()
  131. .implicitlyWait(ConfigProperties.getWaitTime(),TimeUnit.SECONDS);
  132. return result;
  133. }
  134. /** same as isElementPeresent, but don't wait 5 seconds until evaluate **/
  135. public boolean isElementPresent0(WebElement element) {
  136. driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
  137. try {
  138. element.getTagName();
  139. System.out.println(element + " is present");
  140. return true;
  141. } catch (NoSuchElementException e) {
  142. System.out.println(element + " wasn't found");
  143. return false;
  144. } finally {
  145. driver.manage().timeouts()
  146. .implicitlyWait(ConfigProperties.getWaitTime(),TimeUnit.SECONDS);
  147.  
  148. }
  149. }
  150.  
  151. public boolean isElementPresent0(By by) {
  152. driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
  153. boolean result = driver.findElements(by).size() > 0;
  154. driver.manage().timeouts().implicitlyWait(ConfigProperties.getWaitTime(), TimeUnit.SECONDS);
  155. return result;
  156. }
  157.  
  158. // TODO Not The best implementation!!!!
  159. public void waitForElementAbsent(By locator) {
  160. driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
  161. System.out.print("Waiting");
  162. while (isElementVisible(locator)) {
  163. try {
  164. driver.findElement(By.id("NOT EXISTING ELEMENT"));
  165. } catch (NoSuchElementException e) {
  166. System.out.print(".");
  167. }
  168. }
  169. driver.manage().timeouts()
  170. .implicitlyWait(ConfigProperties.getWaitTime(),TimeUnit.SECONDS);
  171. }
  172.  
  173.  
  174. public static <T extends BasePage> T create(WebDriver driver,
  175. Class<T> pageClass) {
  176. return PageFactoryEx.init(driver, pageClass);
  177. }
  178.  
  179. public String getMatcherByRegExp(String link, String pattern) {
  180. String otp = "";
  181. Matcher matcher = Pattern.compile(pattern).matcher(link);
  182. while (matcher.find()) {
  183. otp = matcher.group(1);
  184. }
  185. return otp;
  186. }
  187.  
  188. // TODO explore how it should be use
  189. public static String getIdValueOfAnnotation(String paramName, Class<?> clazz)
  190. throws NoSuchFieldException {
  191. Field field = clazz.getDeclaredField(paramName);
  192. FindBy annotation = field.getAnnotation(FindBy.class);
  193. return annotation.id();
  194. }
  195.  
  196. public WebElement scrollToElement(By by) {
  197. return scrollToElement(driver.findElement(by));
  198. }
  199.  
  200. public WebElement scrollToElement(WebElement element) {
  201. System.out.println("text_ " + element.getText());
  202. int elementPosition = element.getLocation().getY();
  203. System.out.println("elementPosition " + elementPosition);
  204. String js = String.format("window.scroll(0, %s) ", elementPosition);
  205. ((JavascriptExecutor) driver).executeScript(js);
  206. System.out.println("Scrolled");
  207. return element;
  208. }
  209.  
  210.  
  211. public void scrollUntilVisible(WebElement by, By expected) {
  212. while (!isElementPresent0(expected)) {
  213. scrollToElement(by);
  214. }
  215.  
  216.  
  217. }
  218.  
  219. public void acceptAllert() {
  220. Alert alert = driver.switchTo().alert();
  221. alert.accept();
  222. }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement