Search in sources :

Example 1 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project opennms by OpenNMS.

the class JmxConfigurationGeneratorIT method configureJMXConnection.

protected void configureJMXConnection(final boolean skipDefaultVM) throws Exception {
    final long end = System.currentTimeMillis() + LOAD_TIMEOUT;
    final WebDriverWait shortWait = new WebDriverWait(m_driver, 10);
    final String skipDefaultVMxpath = "//span[@id='skipDefaultVM']/input";
    final boolean selected = waitForElement(By.xpath(skipDefaultVMxpath)).isSelected();
    LOG.debug("skipDefaultVM selected: {}", selected);
    if (selected != skipDefaultVM) {
        waitForElement(By.xpath(skipDefaultVMxpath)).click();
    }
    // configure authentication
    final WebElement authenticateElement = waitForElement(By.id("authenticate"));
    if (!authenticateElement.isSelected()) {
        authenticateElement.findElement(By.tagName("input")).click();
    }
    /*
         * Sometimes, Vaadin just loses input, or focus.  Or both!  I suspect it's
         * because of the way Vaadin handles events and DOM-redraws itself, but...
         * ¯\_(ツ)_/¯
         *
         * To make sure it really really *really* works, there are multiple layers
         * of belt-and-suspenders-and-glue-and-staples:
         *
         * 1. Click first to ensure the field is focused
         * 2. Clear the current contents of the field
         * 3. Send the text to the field
         * 4. Click it *again* because sometimes this wakes up the event handler
         * 5. Do it all in a loop that checks for validation errors, because
         *    *sometimes* even with all that, it will fail to fill in a field...
         *    In that case, hit escape to clear the error message and start all
         *    over and try again.
         */
    boolean found = false;
    do {
        setVaadinValue("authenticateUser", "admin");
        setVaadinValue("authenticatePassword", "admin");
        // go to next page
        waitForElement(By.id("next")).click();
        try {
            setImplicitWait(1, TimeUnit.SECONDS);
            found = shortWait.until(new ExpectedCondition<Boolean>() {

                @Override
                public Boolean apply(final WebDriver driver) {
                    try {
                        final WebElement elem = driver.findElement(By.cssSelector("div.v-Notification-error h1"));
                        LOG.debug("Notification error element: {}", elem);
                        if (elem != null) {
                            elem.sendKeys(Keys.ESCAPE);
                            return false;
                        }
                    } catch (final NoSuchElementException | StaleElementReferenceException e) {
                        LOG.warn("Exception while checking for errors message.", e);
                    }
                    try {
                        final Boolean contains = pageContainsText(MBEANS_VIEW_TREE_WAIT_NAME).apply(driver);
                        LOG.debug("Page contains '{}'? {}", MBEANS_VIEW_TREE_WAIT_NAME, contains);
                        return contains;
                    } catch (final Exception e) {
                        LOG.warn("Exception while checking for next page.", e);
                    }
                    return false;
                }
            });
        } catch (final Exception e) {
            LOG.debug("Failed to configure authentication and port.", e);
        } finally {
            setImplicitWait();
        }
    } while (System.currentTimeMillis() < end && !found);
}
Also used : WebDriver(org.openqa.selenium.WebDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) StaleElementReferenceException(org.openqa.selenium.StaleElementReferenceException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) NoSuchElementException(org.openqa.selenium.NoSuchElementException) StaleElementReferenceException(org.openqa.selenium.StaleElementReferenceException) NoSuchElementException(org.openqa.selenium.NoSuchElementException)

Example 2 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project opennms by OpenNMS.

the class BSMAdminIT method verifyElementNotPresent.

/**
 * Verifies that the provided element is not present.
 * @param by
 */
private static void verifyElementNotPresent(OpenNMSSeleniumTestCase testCase, final By by) {
    new WebDriverWait(testCase.getDriver(), 5).until(ExpectedConditions.not(new ExpectedCondition<Boolean>() {

        @Nullable
        @Override
        public Boolean apply(@Nullable WebDriver input) {
            try {
                // the default implicit wait timeout is too long, make it shorter
                input.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                WebElement elementFound = input.findElement(by);
                return elementFound != null;
            } catch (NoSuchElementException ex) {
                return false;
            } finally {
                // set the implicit wait timeout back to the value it has been before
                input.manage().timeouts().implicitlyWait(LOAD_TIMEOUT, TimeUnit.MILLISECONDS);
            }
        }
    }));
}
Also used : WebDriver(org.openqa.selenium.WebDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) Nullable(javax.annotation.Nullable) NoSuchElementException(org.openqa.selenium.NoSuchElementException)

Example 3 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project ORCID-Source by ORCID.

the class BBBUtil method angularHasFinishedProcessing.

public static ExpectedCondition<Boolean> angularHasFinishedProcessing() {
    /*
         * Getting complex. 1. We want to make sure Angular is done. So you call
         * the rootScope apply 2. We want to make sure the browser is done
         * rendering the DOM so we call $timeout
         * http://blog.brunoscopelliti.com/run-a-directive-after-the-dom-has-
         * finished-rendering/ 3. make sure there are no pending AJAX request,
         * if so start over
         */
    return new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {
            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            javascriptExecutor.executeScript(jQueryWaitScript);
            Object obj = javascriptExecutor.executeScript("" + "return window._selenium_jquery_done;");
            Boolean jqueryDone = (obj == null ? false : Boolean.valueOf(obj.toString()));
            if (jqueryDone) {
                NgWebDriver ngWebDriver = new NgWebDriver(javascriptExecutor);
                ngWebDriver.waitForAngularRequestsToFinish();
            }
            return jqueryDone;
        }
    };
}
Also used : WebDriver(org.openqa.selenium.WebDriver) BlackBoxWebDriver.getWebDriver(org.orcid.integration.blackbox.api.BlackBoxWebDriver.getWebDriver) NgWebDriver(com.paulhammant.ngwebdriver.NgWebDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) NgWebDriver(com.paulhammant.ngwebdriver.NgWebDriver) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition)

Example 4 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project ORCID-Source by ORCID.

the class OauthAuthorizationPageHelper method clickAuthorizeOnAuthorizeScreen.

public static void clickAuthorizeOnAuthorizeScreen(final WebDriver webDriver, boolean longLife) {
    if (webDriver.getTitle().equals("ORCID Playground")) {
        return;
    } else {
        try {
            BBBUtil.extremeWaitFor(ExpectedConditions.presenceOfElementLocated(By.xpath("//p[contains(text(),'has asked for the following access to your ORCID Record')]")), webDriver);
            if (longLife == false) {
                // disablePersistentToken
                WebElement persistentElement = webDriver.findElement(By.id("enablePersistentToken"));
                if (persistentElement.isDisplayed()) {
                    if (persistentElement.isSelected()) {
                        persistentElement.click();
                    }
                }
            }
            WebElement authorizeButton = webDriver.findElement(By.id("authorize"));
            authorizeButton.click();
        } catch (TimeoutException e) {
            // It might be the case that we are already in the ORCID Playground page, so, lets check for that case
            (new WebDriverWait(webDriver, BBBUtil.TIMEOUT_SECONDS)).until(new ExpectedCondition<Boolean>() {

                public Boolean apply(WebDriver d) {
                    return d.getTitle().equals("ORCID Playground");
                }
            });
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) TimeoutException(org.openqa.selenium.TimeoutException)

Example 5 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project flue2ent by DefinityLabs.

the class WaiterPluginTest method expectedCondition_until_callsWaitUntil.

@Test
public void expectedCondition_until_callsWaitUntil() {
    WaiterPlugin waiter = new WaiterPlugin(website);
    WebElement mockedElement = mock(WebElement.class);
    when(wait.until(any())).thenReturn(mockedElement);
    ExpectedCondition<WebElement> expectedCondition = driver -> mockedElement;
    WebElement element = waiter.until(expectedCondition);
    verify(wait).until(same(expectedCondition));
    assertThat(element).isSameAs(mockedElement);
}
Also used : Arrays(java.util.Arrays) Matchers.same(org.mockito.Matchers.same) Mock(org.mockito.Mock) WebDriver(org.openqa.selenium.WebDriver) RunWith(org.junit.runner.RunWith) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) FluentWait(org.openqa.selenium.support.ui.FluentWait) Mockito(org.mockito.Mockito) SeleniumElementCreator(org.definitylabs.flue2ent.element.SeleniumElementCreator) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Website(org.definitylabs.flue2ent.Website) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) Before(org.junit.Before) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ExpectedCondition (org.openqa.selenium.support.ui.ExpectedCondition)57 WebDriver (org.openqa.selenium.WebDriver)54 WebElement (org.openqa.selenium.WebElement)45 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)44 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)31 By (org.openqa.selenium.By)19 Wait (org.openqa.selenium.support.ui.Wait)19 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 List (java.util.List)15 WebDriverException (org.openqa.selenium.WebDriverException)15 Test (org.testng.annotations.Test)15 NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)14 BeforeMethod (org.testng.annotations.BeforeMethod)14 Map (java.util.Map)13 ExpectedConditions (org.openqa.selenium.support.ui.ExpectedConditions)12 AfterMethod (org.testng.annotations.AfterMethod)12 ArrayList (java.util.ArrayList)11 Iterator (java.util.Iterator)11 Optional (java.util.Optional)11