From 75f1b8942c2da4cba3942aa4b233a61f5362c6cc Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Thu, 25 Jul 2019 15:20:06 +0200 Subject: [PATCH] Added implicit timeout handling --- src/test/java/BasicTest.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/test/java/BasicTest.java b/src/test/java/BasicTest.java index 1e53556..69be458 100644 --- a/src/test/java/BasicTest.java +++ b/src/test/java/BasicTest.java @@ -14,19 +14,36 @@ import org.testng.annotations.Test; public class BasicTest { private WebDriver driver; private WebDriverWait wait; + private implicitTimeoutMilliseconds = 3000; + private timeoutInSeconds = 20; String URL = "https://0.fredldev.fremo-net.eu"; @BeforeClass public void testSetUp() { driver = new HtmlUnitDriver(true); - wait = new WebDriverWait(driver, 15); + wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds)); + driver.manage().timeouts().implicitlyWait(implicitTimeoutMilliseconds, TimeUnit.MILLISECONDS); } @AfterClass public void tearDown() { driver.quit(); } + + private boolean elementExistsByXpath(String xpath) { + driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS); + boolean exists = driver.findElements( By.xpath(xpath) ).size() != 0; + driver.manage().timeouts().implicitlyWait(implicitTimeoutMilliseconds, TimeUnit.MILLISECONDS); + return exists; + } + + private boolean elementExistsById(String id) { + driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS); + boolean exists = driver.findElements( By.id(id) ).size() != 0; + driver.manage().timeouts().implicitlyWait(implicitTimeoutMilliseconds, TimeUnit.MILLISECONDS); + return exists; + } @Test public void verifyFredlPageTitle() { @@ -43,7 +60,7 @@ public class BasicTest { // String body = driver.getPageSource(); // System.out.println("HTML: " + body); - if (driver.findElements( By.id("privacy-cookie-statement") ).size() != 0) { + if (elementExistsById("privacy-cookie-statement")) { System.out.println("Cookies consent page is shown"); WebElement consentButton = driver.findElement(By.id("opt-in-button-allow")); consentButton.click();