Example usage for org.openqa.selenium.support.ui ExpectedConditions alertIsPresent

List of usage examples for org.openqa.selenium.support.ui ExpectedConditions alertIsPresent

Introduction

In this page you can find the example usage for org.openqa.selenium.support.ui ExpectedConditions alertIsPresent.

Prototype

public static ExpectedCondition<Alert> alertIsPresent() 

Source Link

Usage

From source file:org.opennms.smoketest.OpenNMSSeleniumTestCase.java

License:Open Source License

public void deleteExistingRequisition(final String foreignSource) {
    provisioningPage();/*from w  w w.  j  a  v a2s. c o m*/

    LOG.debug("deleteExistingRequisition: Deleting Requisition: {}", foreignSource);
    if (getForeignSourceElement(foreignSource) == null) {
        LOG.debug("deleteExistingRequisition: Requisition {} is already gone.", foreignSource);
        return;
    }

    do {
        long nodesInRequisition = -1;
        long nodesInDatabase = -1;

        try {
            m_driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

            final WebElement foreignSourceElement = getForeignSourceElement(foreignSource);

            nodesInRequisition = getNodesInRequisition(foreignSourceElement);
            nodesInDatabase = getNodesInDatabase(foreignSourceElement);

            LOG.debug("deleteExistingRequisition: nodesInRequisition={}, nodesInDatabase={}",
                    nodesInRequisition, nodesInDatabase);

            if (nodesInDatabase > 0) {
                if (nodesInRequisition > 0) {
                    LOG.debug("deleteExistingRequisition: We have requisitioned nodes, deleting them.");
                    try {
                        final WebElement deleteNodesButton = foreignSourceElement
                                .findElement(By.xpath("//input[@type='button' and @value='Delete Nodes']"));
                        deleteNodesButton.click();
                        wait.until(ExpectedConditions.alertIsPresent()).accept();
                    } catch (final NoSuchElementException e) {
                    }
                } else {
                    LOG.debug(
                            "deleteExistingRequisition: We have no requisitioned nodes, but there are nodes in the database. Synchronizing.");
                    final WebElement synchronizeButton = foreignSourceElement
                            .findElement(By.xpath("//input[@type='button' and @value='Synchronize']"));
                    synchronizeButton.click();
                    wait.until(new WaitForNodesInDatabase(0));
                }
            } else {
                // no nodes in the database
                try {
                    LOG.debug(
                            "deleteExistingRequisition: We have no nodes in the database, time to delete the requisition. PUSH THE BUTTON FRANK.");
                    final WebElement deleteRequisitionButton = foreignSourceElement
                            .findElement(By.xpath("//input[@type='button' and @value='Delete Requisition']"));
                    deleteRequisitionButton.click();
                } catch (final NoSuchElementException e) {
                }
            }
        } finally {
            m_driver.manage().timeouts().implicitlyWait(LOAD_TIMEOUT, TimeUnit.MILLISECONDS);
        }
    } while (getForeignSourceElement(foreignSource) != null);
}

From source file:org.opennms.smoketest.OutagePageIT.java

License:Open Source License

@Test
public void testAllLinks() throws InterruptedException {
    findElementByLink("Current outages").click();
    findElementByXpath("//button[contains(@class, 'active') and contains(@onclick, 'current')]");
    findElementByXpath("//button[not(contains(@class, 'active')) and contains(@onclick, 'resolved')]");
    findElementByXpath("//button[not(contains(@class, 'active')) and contains(@onclick, 'both')]");
    findElementByLink("Interface");

    outagePage();//ww w .j a v  a2 s.  c  o  m
    findElementByLink("All outages").click();
    findElementByXpath("//button[not(contains(@class, 'active')) and contains(@onclick, 'current')]");
    findElementByXpath("//button[not(contains(@class, 'active')) and contains(@onclick, 'resolved')]");
    findElementByXpath("//button[contains(@class, 'active') and contains(@onclick, 'both')]");
    findElementByLink("Interface");

    outagePage();
    findElementByName("outageIdForm").findElement(By.xpath("//button[@type='submit']")).click();
    final Alert alert = wait.until(ExpectedConditions.alertIsPresent());
    assertNotNull(alert);
    assertEquals("Please enter a valid outage ID.", alert.getText());
    alert.dismiss();
}

From source file:org.opennms.smoketest.UserIT.java

License:Open Source License

@Test
public void testSubmitWithWrongPassword() throws InterruptedException {
    m_driver.findElement(By.linkText("Change Password")).click();
    enterText(By.cssSelector("input[type=password][name=oldpass]"), "12345");
    enterText(By.cssSelector("input[type=password][name=pass1]"), "23456");
    enterText(By.cssSelector("input[type=password][name=pass2]"), "34567");
    m_driver.findElement(By.cssSelector("button[type=submit]")).click();

    try {//from ww  w .  j  a v a  2s.  com
        final Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        alert.dismiss();
    } catch (final Exception e) {
        LOG.debug("Got an exception waiting for a 'wrong password' alert.", e);
        throw e;
    }
}

From source file:org.opennms.smoketest.UserTest.java

License:Open Source License

@Test
public void testSubmitWithWrongPassword() throws InterruptedException {
    m_driver.findElement(By.linkText("Change Password")).click();
    m_driver.findElement(By.cssSelector("input[type=password][name=oldpass]")).sendKeys("12345");
    m_driver.findElement(By.cssSelector("input[type=password][name=pass1]")).sendKeys("23456");
    m_driver.findElement(By.cssSelector("input[type=password][name=pass2]")).sendKeys("34567");
    m_driver.findElement(By.cssSelector("button[type=submit]")).click();

    try {//from w ww.  j a  va 2s .c  o  m
        final Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        alert.dismiss();
    } catch (final Exception e) {
        LOG.debug("Got an exception waiting for a 'wrong password' alert.", e);
    }
}

From source file:org.richfaces.tests.metamer.ftest.a4jAttachQueue.AbstractAttachQueueTest.java

License:Open Source License

@CoversAttributes("rendered")
protected void testRendered() {
    attachQueueAttrs1.set(AttachQueueAttributes.requestDelay, 2000);
    attachQueueAttrs1.set(AttachQueueAttributes.onrequestqueue, "alert('requestQueued')");
    attachQueueAttrs1.set(AttachQueueAttributes.onrequestdequeue, "alert('requestDequeued')");
    attachQueueAttrs1.set(AttachQueueAttributes.rendered, false);

    Graphene.guardAjax(queue).fireEvents(1);

    // check that no requestDelay is applied while renderer=false
    queue.checkLastDelay(0);/*www .  jav a 2s  .  co m*/
    try {
        Graphene.waitGui().withTimeout(2, TimeUnit.SECONDS).until(ExpectedConditions.alertIsPresent());
        fail("No alert should be present!");
    } catch (Exception e) {
        //ok
    }
}

From source file:org.richfaces.tests.metamer.ftest.a4jQueue.TestGlobalQueue.java

License:Open Source License

@Test
@CoversAttributes("rendered")
@IssueTracking("https://issues.jboss.org/browse/RF-9328")
public void testRendered() {
    attsSetter().setAttribute(QueueAttributes.requestDelay).toValue(2000)
            .setAttribute(QueueAttributes.onrequestqueue).toValue("alert('requestQueued')")
            .setAttribute(QueueAttributes.onrequestdequeue).toValue("alert('requestDequeued')")
            .setAttribute(QueueAttributes.rendered).toValue(false).asSingleAction().perform();

    Graphene.guardAjax(queue).fireEvents(1);

    // check that no requestDelay is applied while renderer=false
    queue.checkLastDelay(0);/*from  w  ww. j  a v  a2  s.  c o m*/
    try {
        Graphene.waitGui().withTimeout(2, TimeUnit.SECONDS).until(ExpectedConditions.alertIsPresent());
        fail("No alert should be present!");
    } catch (Exception e) {
        //ok
    }
}

From source file:org.safs.selenium.webdriver.lib.WDLibrary.java

License:Open Source License

/**
 * Wait for the presence of Alert-Modal-Dialog associated with a certain browser identified by ID.<br>
 * It will get the cached webdriver according to the browser's id, and get the 'alert' through that webdriver.<br>
 * <b>Note:</b>This API will NOT change the current WebDriver. {@link #getWebDriver()} will still return the same object.<br>
 * //from   w  w w  .  j a v a 2s.c o  m
 * @param optionals String...
 * <ul>
 * <b>optionals[0] timeoutWaitAlertPresence</b> int, timeout in seconds to wait for the presence of Alert.
 *                                                   If not provided, default is 2 seconds.<br>
 *                                                   If it is provided as {@link #TIMEOUT_NOWAIT}, this method will try to get Alert without waiting.<br>
 * <b>optionals[1] browserID</b> String, the ID to get the browser on which the 'alert' will be closed.
 *                                       If not provided, the current browser will be used.<br>
 * </ul>
 * @return Alert, the current Alert on browser; it could be null if it is not present within timeout.
 * @throws SeleniumPlusException, if WebDriver cannot be got according to the parameter browserID.<br>
 *                                if Alert is not present within timeout.<br>
 */
private static Alert waitAlert(String... optionals) throws SeleniumPlusException {
    String debugmsg = StringUtils.debugmsg(false);
    String browserID = null;
    int timeout = timeoutWaitAlert;
    Alert alert = null;

    if (optionals != null && optionals.length > 0) {
        if (StringUtils.isValid(optionals[0])) {
            try {
                timeout = Integer.parseInt(optionals[0]);
            } catch (NumberFormatException e) {
            }
        }
        if (optionals.length > 1 && StringUtils.isValid(optionals[1]))
            browserID = optionals[1];
    }

    WebDriver webdriver = getWebDriver(browserID);

    if (webdriver == null) {
        throw new SeleniumPlusException("cannot get webdriver according to id '" + browserID + "'",
                SeleniumPlusException.CODE_OBJECT_IS_NULL);
    }

    try {
        if (timeout == TIMEOUT_NOWAIT) {
            alert = webdriver.switchTo().alert();//NoAlertPresentException
        } else {
            WebDriverWait wait = new WebDriverWait(webdriver, timeout);
            alert = wait.until(ExpectedConditions.alertIsPresent());//TimeoutException
        }

        return alert;
    } catch (Exception e) {
        String message = "Fail to get alert dialog associated with "
                + (browserID == null ? "current browser." : "browser '" + browserID + "'.");
        IndependantLog.warn(debugmsg + message + " due to " + StringUtils.debugmsg(e));
        throw new SeleniumPlusException(message);
    }
}

From source file:org.xframium.page.keyWord.step.spi.KWSWindow.java

License:Open Source License

@Override
public boolean _executeStep(Page pageObject, WebDriver webDriver, Map<String, Object> contextMap,
        Map<String, PageData> dataMap, Map<String, Page> pageMap) throws Exception {
    if (log.isDebugEnabled())
        log.debug("Execution Function " + getName());

    if (getParameterList().size() < 1)
        throw new IllegalArgumentException(
                "First Parameter Switchtype should be provided with values BY_WINTITLE| BY_WINURL|BY_FRAME|BY_PARENTFRAME|BY_DEFAULT");

    try {//from   www.  ja  v a 2 s .  c  o m
        // Verify if the parameter-1 values are correct
        String switchType = getParameterValue(getParameterList().get(0), contextMap, dataMap) + "";
        String switchExpValue = "";

        switch (SwitchType.valueOf(switchType)) {
        case BY_WINTITLE:
            if (getParameterList().size() < 2)
                throw new IllegalArgumentException("Please provide the title for the window as a parameter");
            switchExpValue = getParameterValue(getParameterList().get(1), contextMap, dataMap) + "";
            return verifySwitchWindow(webDriver, switchType, switchExpValue);
        case BY_WINURL:
            if (getParameterList().size() < 2)
                throw new IllegalArgumentException("Please provide the URL for the window as a parameter");
            switchExpValue = getParameterValue(getParameterList().get(1), contextMap, dataMap) + "";
            return verifySwitchWindow(webDriver, switchType, switchExpValue);
        case BY_FRAME:
            if (getParameterList().size() < 2)
                throw new IllegalArgumentException("Please provide the Frame id for the Frame as a parameter");
            switchExpValue = getParameterValue(getParameterList().get(1), contextMap, dataMap) + "";
            webDriver.switchTo().frame(switchExpValue);
            break;

        case BY_PARENTFRAME:
            webDriver.switchTo().parentFrame();
            break;
        case BY_DEFAULT:
            webDriver.switchTo().defaultContent();
            break;
        case BY_WINCLOSE:
            webDriver.close();
            break;

        case BY_ELEMENT:
            Element currentElement = getElement(pageObject, contextMap, webDriver, dataMap);
            if (currentElement == null) {
                log.warn("Attempting to switch to frame identified by " + getName() + " that does not exist");
                return false;
            }

            WebElement nativeElement = (WebElement) currentElement.getNative();
            if (nativeElement instanceof MorelandWebElement)
                nativeElement = ((MorelandWebElement) nativeElement).getWebElement();
            webDriver.switchTo().frame(nativeElement);
            break;
        case BY_ALERT:
            WebDriverWait alertWait = new WebDriverWait(webDriver, 5);
            alertWait.until(ExpectedConditions.alertIsPresent());
            Alert alert = webDriver.switchTo().alert();
            alert.accept();
            break;
        case BY_MAXIMIZE:
            webDriver.manage().window().maximize();
            break;
        default:
            throw new IllegalArgumentException(
                    "Parameter switchtype should be BY_WINTITLE| BY_WINURL|BY_FRAME|BY_PARENTFRAME|BY_DEFAULT|BY_ALERT|BY_ELEMENT|BY_MAXIMIZE");
        }

    } catch (Exception e) {
        log.error("Error executing function for validation [" + getName() + "] on page [" + getPageName() + "]",
                e);
        return false;
    }

    return true;
}

From source file:org.xmlium.test.web.commons.xml.XMLTestSteps.java

License:LGPL

protected Object switchTo(SwitchTo to) {
    if (to.getAlert() != null) {
        WebDriverWait wait = new WebDriverWait(getSuite().getDriver(), 2);
        Object o = wait.until(ExpectedConditions.alertIsPresent());
        return o;
    }//  ww  w .  j  a  v a 2  s  .  c o  m
    if (to.getFrame() != null) {
        WebDriverWait wait = new WebDriverWait(getSuite().getDriver(), 10);
        Object o = wait.until(ExpectedConditions
                .frameToBeAvailableAndSwitchToIt(By.name(unformatValue(to.getFrame().getValue()))));
        return o;
    }

    return null;
}

From source file:PageObjects.Mst_FinishesPage.java

public Mst_FinishesPage pressDelete(int row) {
    By locator = By.cssSelector("#content > div.flexi-grid > table > tbody > tr:nth-child(" + row
            + ") > td.actions > a:nth-child(3) > span");
    WebElement element = new WebDriverWait(driver, DataItems.shortWait)
            .until(ExpectedConditions.elementToBeClickable(locator));
    element.click();//from  w w w  . j a v a 2s.c o m

    Alert alert = new WebDriverWait(driver, DataItems.shortWait).until(ExpectedConditions.alertIsPresent());
    alert.accept();

    return new Mst_FinishesPage(driver);
}