diff --git a/README.md b/README.md index 297e9eb..32d9457 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ We use interfaces where is possible, so you can implement your own version of ta com.github.aquality-automation aquality-selenium - 1.2.0 + 1.2.1 ``` diff --git a/pom.xml b/pom.xml index 170d58c..67099cc 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.aquality-automation aquality-selenium - 1.2.0 + 1.2.1 jar Aquality Selenium diff --git a/src/main/java/aquality/selenium/browser/Browser.java b/src/main/java/aquality/selenium/browser/Browser.java index 3f4744d..ba67a8e 100644 --- a/src/main/java/aquality/selenium/browser/Browser.java +++ b/src/main/java/aquality/selenium/browser/Browser.java @@ -13,11 +13,9 @@ import org.openqa.selenium.WebDriver.Navigation; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; - import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.Collections; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; @@ -165,8 +163,7 @@ public void waitForPageToLoad() { ConditionalWait.waitFor(condition, timeouts.getPageLoad(), timeouts.getPollingInterval(), - String.format(getLocManager().getValue("loc.browser.page.is.not.loaded"), timeouts.getPageLoad()), - Collections.emptyList()); + String.format(getLocManager().getValue("loc.browser.page.is.not.loaded"), timeouts.getPageLoad())); } /** diff --git a/src/main/java/aquality/selenium/elements/ElementFinder.java b/src/main/java/aquality/selenium/elements/ElementFinder.java index 0f14a56..3dfc110 100644 --- a/src/main/java/aquality/selenium/elements/ElementFinder.java +++ b/src/main/java/aquality/selenium/elements/ElementFinder.java @@ -9,9 +9,7 @@ import aquality.selenium.logger.Logger; import aquality.selenium.waitings.ConditionalWait; import org.openqa.selenium.*; - import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.function.Predicate; @@ -60,14 +58,12 @@ public List findElements(By locator, long timeout, ElementState stat } } - List findElements(By locator, long timeout, DesiredState desiredState) - { + List findElements(By locator, long timeout, DesiredState desiredState) { List foundElements = new ArrayList<>(); List resultElements = new ArrayList<>(); long zeroTimeout = 0L; getBrowser().setImplicitWaitTimeout(zeroTimeout); - try{ - + try { ConditionalWait.waitFor(driver -> { List allFoundElements = driver.findElements(locator); @@ -75,9 +71,8 @@ List findElements(By locator, long timeout, DesiredState desiredStat List filteredElements = filterByState(allFoundElements, desiredState.getDesiredStatePredicate()); resultElements.addAll(filteredElements); return !filteredElements.isEmpty(); - }, timeout, getTimeoutConfiguration().getPollingInterval(), - desiredState.getMessage(), Collections.emptyList()); - }catch (TimeoutException e){ + }, timeout, getTimeoutConfiguration().getPollingInterval(), desiredState.getMessage()); + } catch (TimeoutException e) { applyResult(locator, desiredState, foundElements); } getBrowser().setImplicitWaitTimeout(getTimeoutConfiguration().getImplicit()); diff --git a/src/main/java/aquality/selenium/elements/ElementStateProvider.java b/src/main/java/aquality/selenium/elements/ElementStateProvider.java index a334628..dee323c 100644 --- a/src/main/java/aquality/selenium/elements/ElementStateProvider.java +++ b/src/main/java/aquality/selenium/elements/ElementStateProvider.java @@ -10,8 +10,6 @@ import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.TimeoutException; import org.openqa.selenium.WebElement; - -import java.util.Collections; import java.util.List; import java.util.Objects; @@ -97,8 +95,7 @@ public boolean waitForNotExist(long timeout) { return ConditionalWait.waitFor(y -> findElements(zeroTimeout).isEmpty(), timeout, getTimeoutConfiguration().getPollingInterval(), - message, - Collections.emptyList()); + message); }catch (TimeoutException e){ getLogger().debug(getDesiredStateMessage("NOT EXIST", timeout)); return false; diff --git a/src/main/java/aquality/selenium/waitings/ConditionalWait.java b/src/main/java/aquality/selenium/waitings/ConditionalWait.java index 474d89e..6d7fe64 100644 --- a/src/main/java/aquality/selenium/waitings/ConditionalWait.java +++ b/src/main/java/aquality/selenium/waitings/ConditionalWait.java @@ -118,6 +118,24 @@ public static T waitFor(ExpectedCondition condition, String message) { Collections.singleton(StaleElementReferenceException.class)); } + /** + * Waits for function will be true or return some except false. + * StaleElementReferenceException will be handled by default + * @param condition Function for waiting {@link Function}., + * @param timeOutInSeconds Time-out in seconds + * @param pollingIntervalInMilliseconds interval in milliseconds between checks whether condition match + * @param message the message that will be added to an error in case if the condition is not matched during the timeout + * @param Type of object which is waiting + * @return Object which waiting for or null - is exceptions occurred + */ + public static T waitFor(ExpectedCondition condition, long timeOutInSeconds, long pollingIntervalInMilliseconds, String message) { + return waitFor(condition, + timeOutInSeconds, + pollingIntervalInMilliseconds, + message, + Collections.singleton(StaleElementReferenceException.class)); + } + /** * Waits for function will be true or return some except false. * diff --git a/src/test/java/aquality/selenium/waitings/ConditionalWaitTests.java b/src/test/java/aquality/selenium/waitings/ConditionalWaitTests.java index c166074..1599de4 100644 --- a/src/test/java/aquality/selenium/waitings/ConditionalWaitTests.java +++ b/src/test/java/aquality/selenium/waitings/ConditionalWaitTests.java @@ -5,13 +5,12 @@ import aquality.selenium.configuration.ITimeoutConfiguration; import org.openqa.selenium.StaleElementReferenceException; import org.testng.Assert; +import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; import utils.DurationSample; import utils.Timer; - import java.util.Collections; import java.util.concurrent.TimeoutException; - import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @@ -88,8 +87,6 @@ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndDefaul } catch (org.openqa.selenium.TimeoutException e) { DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition(), defaultDeviation); assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString()); - } finally { - BrowserManager.getBrowser().quit(); } } @@ -102,71 +99,122 @@ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndTimeou timer.start(); return false; }, waitForTimeoutCondition, waitForTimeoutPolling, - "Conditional should be true", Collections.singleton(StaleElementReferenceException.class)); + "Conditional should be true"); } catch (org.openqa.selenium.TimeoutException e) { DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation); assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString()); - } finally { - BrowserManager.getBrowser().quit(); } } @Test - public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver() { + public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndTimeoutIsOverWithIgnoredExceptions() { Timer timer = new Timer(); try { ConditionalWait.waitFor((driver) -> { timer.start(); - return true; - }, - "Conditional should be true"); - DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition()); - assertTrue(durationSample.getDuration() < getTimeoutConfig().getCondition()); - } finally { - BrowserManager.getBrowser().quit(); + return false; + }, waitForTimeoutCondition, waitForTimeoutPolling, + "Conditional should be true", Collections.emptyList()); + + } catch (org.openqa.selenium.TimeoutException e) { + DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation); + assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString()); } } @Test - public void testExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver(){ + public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver() { + Timer timer = new Timer(); + + ConditionalWait.waitFor((driver) -> + { + timer.start(); + return true; + }, + "Conditional should be true"); + DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition()); + assertTrue(durationSample.getDuration() < getTimeoutConfig().getCondition()); + } + + @Test + public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOverWithIgnoredExceptions() { + Timer timer = new Timer(); + boolean conditionResult = ConditionalWait.waitFor((driver) -> + { + timer.start(); + return true; + }, waitForTimeoutCondition, waitForTimeoutPolling, + "Conditional should be true"); + DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition); + assertTrue(durationSample.getDuration() < waitForTimeoutCondition); + assertTrue(conditionResult, "Condition result should be true"); + } + + @Test + public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver() { + Timer timer = new Timer(); + boolean conditionResult = ConditionalWait.waitFor((driver) -> + { + timer.start(); + return true; + }, waitForTimeoutCondition, waitForTimeoutPolling, + "Conditional should be true", Collections.singleton(IllegalArgumentException.class)); + DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition); + assertTrue(durationSample.getDuration() < waitForTimeoutCondition); + assertTrue(conditionResult, "Condition result should be true"); + } + + @Test + public void testExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver() { Timer timer = new Timer(); - try{ + try { ConditionalWait.waitFor((driver) -> { timer.start(); throw new IllegalArgumentException("I am exception"); }, waitForTimeoutCondition, waitForTimeoutPolling, "Conditional should be true", Collections.singleton(IllegalArgumentException.class)); - } catch (org.openqa.selenium.TimeoutException e){ + } catch (org.openqa.selenium.TimeoutException e) { DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation); assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString()); - } finally { - BrowserManager.getBrowser().quit(); } } @Test - public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver() { + public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver() { Timer timer = new Timer(); try { - boolean conditionResult = ConditionalWait.waitFor((driver) -> + ConditionalWait.waitFor((driver) -> { timer.start(); - return true; + throw new StaleElementReferenceException("I am StaleElementReferenceException"); }, waitForTimeoutCondition, waitForTimeoutPolling, - "Conditional should be true", Collections.singleton(IllegalArgumentException.class)); - DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition); - assertTrue(durationSample.getDuration() < waitForTimeoutCondition); - assertTrue(conditionResult, "Condition result should be true"); - } finally { - BrowserManager.getBrowser().quit(); + "Conditional should be true"); + } catch (org.openqa.selenium.TimeoutException e) { + DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation); + assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString()); + } + } + + @Test + public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver() { + Timer timer = new Timer(); + try { + ConditionalWait.waitFor((driver) -> + { + timer.start(); + throw new StaleElementReferenceException("I am StaleElementReferenceException"); + }, "Conditional should be true"); + } catch (org.openqa.selenium.TimeoutException e) { + DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition(), defaultDeviation); + assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString()); } } @Test - public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver(){ + public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver() { Timer timer = new Timer(); boolean conditionResult = ConditionalWait.waitFor(() -> { @@ -179,7 +227,7 @@ public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver(){ } @Test - public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(){ + public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver() { Timer timer = new Timer(); boolean conditionResult = ConditionalWait.waitFor(() -> { @@ -192,7 +240,7 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(){ } @Test - public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver(){ + public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver() { Timer timer = new Timer(); boolean conditionResult = ConditionalWait.waitFor(() -> { @@ -205,7 +253,7 @@ public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver() } @Test - public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver(){ + public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver() { Timer timer = new Timer(); boolean conditionResult = ConditionalWait.waitFor(() -> { @@ -217,7 +265,12 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver( assertFalse(conditionResult, "Condition result should be false"); } - private ITimeoutConfiguration getTimeoutConfig(){ + @AfterMethod + public void after() { + BrowserManager.getBrowser().quit(); + } + + private ITimeoutConfiguration getTimeoutConfig() { return Configuration.getInstance().getTimeoutConfiguration(); } } diff --git a/src/test/java/tests/usecases/BrowserFactoryTests.java b/src/test/java/tests/usecases/BrowserFactoryTests.java index 50d1030..c147156 100644 --- a/src/test/java/tests/usecases/BrowserFactoryTests.java +++ b/src/test/java/tests/usecases/BrowserFactoryTests.java @@ -84,8 +84,7 @@ public void testShouldBePossibleToOverrideDownloadDirectory() throws IOException BrowserManager.getBrowser().goTo(urlXlsSample); File fileDownloaded = new File(downloadDirFactoryInitialized + fileName); - boolean isFileDownloaded = ConditionalWait.waitFor(driver -> fileDownloaded.exists(), 120, 300, "File should be downloaded", Collections.singleton(StaleElementReferenceException.class)); - + boolean isFileDownloaded = ConditionalWait.waitFor(driver -> fileDownloaded.exists(), 120, 300, "File should be downloaded"); Assert.assertTrue(isFileDownloaded, "Downloaded file exists"); }