Search in sources :

Example 6 with Alert

use of org.openqa.selenium.Alert in project keycloak by keycloak.

the class WebAuthnRegisterPage method registerWebAuthnCredential.

public void registerWebAuthnCredential(String authenticatorLabel) {
    if (!isRegisterAlertPresent(ALERT_DEFAULT_TIMEOUT)) {
        throw new TimeoutException("Cannot register Security Key due to missing prompt for registration");
    }
    Alert promptDialog = driver.switchTo().alert();
    promptDialog.sendKeys(authenticatorLabel);
    promptDialog.accept();
    waitForPageToLoad();
}
Also used : Alert(org.openqa.selenium.Alert) TimeoutException(org.openqa.selenium.TimeoutException)

Example 7 with Alert

use of org.openqa.selenium.Alert in project selenium_java by sergueik.

the class SeleniumEasyTest method alertPromptTest.

@Test(enabled = true)
public void alertPromptTest() {
    // wrong selector
    String buttonText = "Click for Prompt Box";
    String buttonSelector = String.format("//*[@id='easycont']//div[@class='panel-heading'][contains(normalize-space(.), '%s')]/..//button[contains(normalize-space(.), '%s')]", "Java Script Alert Box", buttonText);
    try {
        WebElement buttonElement = wait.until(new ExpectedCondition<WebElement>() {

            Predicate<WebElement> textCheck = _element -> {
                String _text = _element.getText();
                System.err.format("in filter: Text: \"%s\" expecting : \"%s\"\n", _text, buttonText);
                return (Boolean) (_text.contains(buttonText));
            };

            @Override
            public WebElement apply(WebDriver d) {
                System.err.println("Locating " + buttonSelector);
                Optional<WebElement> e = d.findElements(By.xpath(buttonSelector)).stream().filter(textCheck).findFirst();
                return (e.isPresent()) ? e.get() : (WebElement) null;
            }
        });
        System.err.println("Acting on: " + buttonElement.getAttribute("outerHTML"));
        highlight(buttonElement);
        flash(buttonElement);
        buttonElement.click();
        sleep(1000);
    } catch (Exception e) {
        System.err.println("Exception: " + e.toString());
        verificationErrors.append("Exception: " + e.toString());
    }
    try {
        // fill the data in the alert
        Alert alert = driver.switchTo().alert();
        System.err.println("In alert " + alert.toString());
        alert.sendKeys("Harry Potter");
        alert.accept();
    } catch (NoAlertPresentException ex) {
    // Alert not present - ignore
    } catch (WebDriverException ex) {
        System.err.println("Alert was not handled : " + ex.getStackTrace().toString());
        return;
    }
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("prompt-demo")));
    assertThat(wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("prompt-demo")))).getText(), is(equalTo("You have entered 'Harry Potter' !")));
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) CoreMatchers(org.hamcrest.CoreMatchers) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) Predicate(java.util.function.Predicate) By(org.openqa.selenium.By) WebDriver(org.openqa.selenium.WebDriver) WebDriverException(org.openqa.selenium.WebDriverException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) BeforeMethod(org.testng.annotations.BeforeMethod) WebElement(org.openqa.selenium.WebElement) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) ITestResult(org.testng.ITestResult) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Optional(java.util.Optional) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Alert(org.openqa.selenium.Alert) Method(java.lang.reflect.Method) WebDriver(org.openqa.selenium.WebDriver) Optional(java.util.Optional) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) Alert(org.openqa.selenium.Alert) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.testng.annotations.Test)

Example 8 with Alert

use of org.openqa.selenium.Alert in project devonfw-testing by devonfw.

the class WindowUtils method closeAlertIfPresent.

/**
 * Checks if Alert Pop Up was displayed and dismiss it if shown.
 *
 * @return true if alert was dismissed, false otherwise
 * @author
 */
public static boolean closeAlertIfPresent() {
    try {
        Alert alert = BasePage.getDriver().switchTo().alert();
        alert.dismiss();
        BFLogger.logDebug("Alert dismissed.");
        return true;
    } catch (NoAlertPresentException e) {
        return false;
    }
}
Also used : NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) Alert(org.openqa.selenium.Alert)

Example 9 with Alert

use of org.openqa.selenium.Alert in project OpenOLAT by OpenOLAT.

the class QTI12Page method endTest.

public QTI12Page endTest() {
    By endBy = By.cssSelector("div.o_button_group_right a");
    OOGraphene.waitElement(endBy, 5, browser);
    browser.findElement(endBy).click();
    // accept and go further
    Alert alert = browser.switchTo().alert();
    alert.accept();
    OOGraphene.waitAndCloseBlueMessageWindow(browser);
    return this;
}
Also used : By(org.openqa.selenium.By) Alert(org.openqa.selenium.Alert)

Example 10 with Alert

use of org.openqa.selenium.Alert in project java-client by appium.

the class BaseListenerTest method assertThatAlertListenerWorks.

protected boolean assertThatAlertListenerWorks(EmptyWebDriver driver, TestListener listener, String prefix) {
    try {
        Alert alert = driver.switchTo().alert();
        alert.accept();
        alert.dismiss();
        alert.sendKeys("Keys");
        assertThat(listener.messages, contains(prefix + "Attempt to accept alert", prefix + "The alert was accepted", prefix + "Attempt to dismiss alert", prefix + "The alert was dismissed", prefix + "Attempt to send keys to alert", prefix + "Keys were sent to alert"));
        return true;
    } finally {
        listener.messages.clear();
    }
}
Also used : Alert(org.openqa.selenium.Alert)

Aggregations

Alert (org.openqa.selenium.Alert)29 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)10 Test (org.junit.Test)7 NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)5 WebDriver (org.openqa.selenium.WebDriver)5 WebElement (org.openqa.selenium.WebElement)5 By (org.openqa.selenium.By)4 WebDriverException (org.openqa.selenium.WebDriverException)4 Test (org.testng.annotations.Test)4 Method (java.lang.reflect.Method)2 Optional (java.util.Optional)2 Predicate (java.util.function.Predicate)2 CoreMatchers (org.hamcrest.CoreMatchers)2 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 TimeoutException (org.openqa.selenium.TimeoutException)2 ExpectedCondition (org.openqa.selenium.support.ui.ExpectedCondition)2 ExpectedConditions (org.openqa.selenium.support.ui.ExpectedConditions)2 ITestResult (org.testng.ITestResult)2 AfterMethod (org.testng.annotations.AfterMethod)2