PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/seleniumsimplified/webdriver/fluent/FluentWaitForWebElement.java

https://gitlab.com/chiavegatto/webDriverExperiments
Java | 77 lines | 61 code | 15 blank | 1 comment | 0 complexity | 2d8d1a6528f1d23b978e5b2566b0a496 MD5 | raw file
  1. package com.seleniumsimplified.webdriver.fluent;
  2. import com.google.common.base.Function;
  3. import com.google.common.base.Predicate;
  4. import org.junit.AfterClass;
  5. import org.junit.Before;
  6. import org.junit.BeforeClass;
  7. import org.junit.Test;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.firefox.FirefoxDriver;
  12. import org.openqa.selenium.support.ui.ExpectedConditions;
  13. import org.openqa.selenium.support.ui.FluentWait;
  14. import org.openqa.selenium.support.ui.WebDriverWait;
  15. import java.util.concurrent.TimeUnit;
  16. public class FluentWaitForWebElement {
  17. private static WebDriver driver;
  18. WebElement countdown;
  19. @BeforeClass
  20. public static void setup(){
  21. driver = new FirefoxDriver();
  22. //driver.get("http://stuntsnippets.com/javascript-countdown/");
  23. driver.get("http://seleniumsimplified.com/testpages/javascript_countdown.html");
  24. }
  25. @Before
  26. public void setupTest(){
  27. driver.navigate().refresh();
  28. countdown = driver.findElement(By.id("javascript_countdown_time"));
  29. new WebDriverWait(driver,10).
  30. until(ExpectedConditions.visibilityOf(countdown));
  31. }
  32. @Test
  33. public void waitForWebElementFluently(){
  34. new FluentWait<WebElement>(countdown).
  35. withTimeout(10, TimeUnit.SECONDS).
  36. pollingEvery(100,TimeUnit.MILLISECONDS).
  37. until(new Function<WebElement, Boolean>() {
  38. @Override
  39. public Boolean apply(WebElement element) {
  40. return element.getText().endsWith("04");
  41. }
  42. }
  43. );
  44. }
  45. @Test
  46. public void waitForWebElementFluentlyPredicate(){
  47. new FluentWait<WebElement>(countdown).
  48. withTimeout(10, TimeUnit.SECONDS).
  49. pollingEvery(100,TimeUnit.MILLISECONDS).
  50. until(new Predicate<WebElement>() {
  51. @Override
  52. public boolean apply(WebElement element) {
  53. return element.getText().endsWith("04");
  54. }
  55. }
  56. );
  57. }
  58. @AfterClass
  59. public static void tearDown(){
  60. driver.quit();
  61. }
  62. }