Search in sources :

Example 16 with NoAlertPresentException

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

the class KeyMasterTest method confirmAlertIsPresent.

// TODO: parameter
private void confirmAlertIsPresent() {
    try {
        alert = driver.switchTo().alert();
        String alert_text = alert.getText();
        System.err.println("Accepted alert: " + alert_text);
        // Accepted alert: o, enter or left pressed on
        // target = [object HTMLBodyElement]
        // srcElement = [object HTMLBodyElement] !
        // confirm alert
        alert.accept();
    } catch (NoAlertPresentException e) {
        throw new RuntimeException("Alert was not present.");
    } catch (WebDriverException e) {
        System.err.println("Alert was not handled : " + e.getStackTrace().toString());
        return;
    }
}
Also used : NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebDriverException(org.openqa.selenium.WebDriverException)

Example 17 with NoAlertPresentException

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

the class NgWay2AutomationIntegrationTest method testInvitateNewCustomerToOpenAccount.

// @Ignore
@Test
public void testInvitateNewCustomerToOpenAccount() {
    if (isCIBuild) {
        return;
    }
    // When I proceed to "Bank Manager Login"
    ngDriver.findElement(NgBy.buttonText("Bank Manager Login")).click();
    // And I proceed to "Add Customer"
    ngDriver.findElement(NgBy.partialButtonText("Add Customer")).click();
    NgWebElement ng_firstName = ngDriver.findElement(NgBy.model("fName"));
    assertThat(ng_firstName.getAttribute("placeholder"), equalTo("First Name"));
    ng_firstName.sendKeys("John");
    NgWebElement ng_lastName = ngDriver.findElement(NgBy.model("lName"));
    assertThat(ng_lastName.getAttribute("placeholder"), equalTo("Last Name"));
    ng_lastName.sendKeys("Doe");
    NgWebElement ng_postCode = ngDriver.findElement(NgBy.model("postCd"));
    assertThat(ng_postCode.getAttribute("placeholder"), equalTo("Post Code"));
    ng_postCode.sendKeys("11011");
    // And I add no accounts
    // NOTE: there are two 'Add Customer' buttons on this form
    WebElement ng_addCustomerButtonElement = ngDriver.findElements(NgBy.partialButtonText("Add Customer")).get(1);
    ng_addCustomerButtonElement.submit();
    try {
        alert = seleniumDriver.switchTo().alert();
        Pattern pattern = Pattern.compile("Customer added successfully with customer id :(\\d+)");
        Matcher matcher = pattern.matcher(alert.getText());
        if (matcher.find()) {
            System.err.println("New Customer id: " + matcher.group(1));
        }
        // confirm alert
        alert.accept();
    } catch (NoAlertPresentException ex) {
        // Alert not present
        System.err.println(ex.getStackTrace());
        return;
    } catch (WebDriverException ex) {
        // Alert not handled by PhantomJS
        System.err.println("Alert was not handled by PhantomJS: " + ex.getStackTrace());
        return;
    }
    // And I switch to "Home" screen
    NgWebElement ng_home = ngDriver.findElement(NgBy.buttonText("Home"));
    highlight(ng_home);
    ng_home.click();
    // And I proceed to "Customer Login"
    ngDriver.findElement(NgBy.buttonText("Customer Login")).click();
    for (WebElement currentCustomer : ngDriver.findElement(NgBy.model("custId")).findElements(NgBy.repeater("cust in Customers"))) {
        if (currentCustomer.getText().indexOf("John Doe") >= 0) {
            System.err.println(currentCustomer.getText());
            currentCustomer.click();
        }
    }
    NgWebElement ng_login = ngDriver.findElement(NgBy.buttonText("Login"));
    assertTrue(ng_login.isEnabled());
    highlight(ng_login);
    ng_login.click();
    ngDriver.waitForAngular();
    // Then I am greeted as "John Doe"
    NgWebElement ng_user = ngDriver.findElement(NgBy.binding("user"));
    assertThat(ng_user.getText(), containsString("John"));
    assertThat(ng_user.getText(), containsString("Doe"));
    // And I am invited to open an account
    Object noAccount = ng_user.evaluate("noAccount");
    assertTrue(parseBoolean(noAccount.toString()));
    boolean hasAccounts = !(parseBoolean(noAccount.toString()));
    System.err.println("Has accounts: " + hasAccounts);
    WebElement message = seleniumDriver.findElement(By.cssSelector("span[ng-show='noAccount']"));
    assertTrue(message.isDisplayed());
    highlight(message);
    assertThat(message.getText(), containsString("Please open an account"));
    System.err.println(message.getText());
    // And I see no accounts
    NgWebElement accountNo = ngDriver.findElement(NgBy.binding("accountNo"));
    assertFalse(accountNo.isDisplayed());
    // And I cannot choose an account to view
    List<WebElement> accounts = ngDriver.findElements(NgBy.repeater("account for account in Accounts"));
    assertThat(accounts.size(), equalTo(0));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.junit.Test)

Example 18 with NoAlertPresentException

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

the class AppTest method testOpenAccount.

// @Ignore
@Test
public void testOpenAccount() throws Exception {
    ngDriver.navigate().to(urlBanking);
    // bank manager login
    ngDriver.findElement(NgBy.buttonText("Bank Manager Login")).click();
    ngDriver.findElement(NgBy.partialButtonText("Open Account")).click();
    // wait for customers info get loaded
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.repeater("cust in Customers")).getWrappedElement()));
    NgWebElement selectCustomer = ngDriver.findElement(NgBy.model("custId"));
    assertThat(selectCustomer.getAttribute("id"), containsString("userSelect"));
    List<WebElement> customers = new NgWebElement(ngDriver, selectCustomer).findElements(NgBy.repeater("cust in Customers"));
    // pick random customer to log in
    int random_customer_index = 1 + (int) (Math.random() * (customers.size() - 1));
    WebElement customer = customers.get(random_customer_index);
    String customerName = customer.getText();
    System.err.println(customerName);
    customer.click();
    NgWebElement ng_selectCurrencies = ngDriver.findElement(NgBy.model("currency"));
    actions.moveToElement(ng_selectCurrencies.getWrappedElement()).build().perform();
    highlight(ng_selectCurrencies.getWrappedElement());
    // use core Selenium
    Select selectCurrencies = new Select(ng_selectCurrencies.getWrappedElement());
    List<WebElement> accountCurrencies = selectCurrencies.getOptions();
    // select "Dollars"
    selectCurrencies.selectByVisibleText("Dollar");
    // add the account
    WebElement submitButton = ngDriver.getWrappedDriver().findElement(By.xpath("/html/body//form/button[@type='submit']"));
    assertThat(submitButton.getText(), containsString("Process"));
    actions.moveToElement(submitButton).build().perform();
    highlight(submitButton);
    submitButton.click();
    String newAccount = null;
    try {
        alert = seleniumDriver.switchTo().alert();
        String alert_text = alert.getText();
        assertThat(alert_text, containsString("Account created successfully with account Number"));
        Pattern pattern = Pattern.compile("(\\d+)");
        Matcher matcher = pattern.matcher(alert_text);
        if (matcher.find()) {
            newAccount = matcher.group(1);
            System.err.println("New account id " + newAccount);
        }
        // confirm the alert
        alert.accept();
    } catch (NoAlertPresentException ex) {
        // Alert not present
        System.err.println("NoAlertPresentException: " + ex.getStackTrace());
    // observed in Chrome. Ignore
    // return;
    } catch (WebDriverException ex) {
        // fullStackTrace =
        // org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(ex);
        // System.err.println("Alert was not handled by PhantomJS: " +
        // fullStackTrace);
        System.err.println("Alert was not handled by PhantomJS: " + ex.getStackTrace().toString());
        return;
    }
    // And I switch to "Home" screen
    NgWebElement ng_home = ngDriver.findElement(NgBy.buttonText("Home"));
    highlight(ng_home);
    ng_home.click();
    // And I proceed to "Bank Manager Login"
    ngDriver.findElement(NgBy.buttonText("Bank Manager Login")).click();
    ngDriver.findElement(NgBy.partialButtonText("Customers")).click();
    // wait for customers info get loaded
    ngDriver.waitForAngular();
    // I can find the Customers Account I just Added
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.repeater("cust in Customers")).getWrappedElement()));
    Enumeration<WebElement> customersEnum = Collections.enumeration(ngDriver.findElements(NgBy.repeater("cust in Customers")));
    while (customersEnum.hasMoreElements()) {
        // find the customer
        WebElement currentCustomer = customersEnum.nextElement();
        if (currentCustomer.getText().indexOf(customerName) >= 0) {
            // System.err.println("Current customer: " + currentCustomer.getText());
            highlight(currentCustomer);
            NgWebElement ng_currentCustomer = new NgWebElement(ngDriver, currentCustomer);
            Enumeration<WebElement> accountsEnum = Collections.enumeration(ng_currentCustomer.findElements(NgBy.repeater("account in cust.accountNo")));
            while (accountsEnum.hasMoreElements()) {
                // find the account
                WebElement currentAccount = accountsEnum.nextElement();
                if (currentAccount.getText().indexOf(newAccount) >= 0) {
                    highlight(currentAccount);
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) Select(org.openqa.selenium.support.ui.Select) NgWebElement(com.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) NgWebElement(com.jprotractor.NgWebElement) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.junit.Test)

Example 19 with NoAlertPresentException

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

the class SeleniumEasyTest method alertTest.

@Test(enabled = true)
public void alertTest() {
    // wrong selector
    WebElement buttonElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format("//*[@id='easycont']//div[@class='panel-heading'][contains(normalize-space(.), '%s')]/..//button[contains(normalize-space(.), '%s')]", "Java Script Alert Box", "Click me!"))));
    System.err.println("Acting on: " + buttonElement.getAttribute("outerHTML"));
    highlight(buttonElement);
    flash(buttonElement);
    buttonElement.click();
    sleep(1000);
    try {
        // confirm alert
        driver.switchTo().alert().accept();
    } catch (NoAlertPresentException ex) {
    // Alert not present - ignore
    } catch (WebDriverException ex) {
        System.err.println("Alert was not handled : " + ex.getStackTrace().toString());
        return;
    }
}
Also used : NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.testng.annotations.Test)

Example 20 with NoAlertPresentException

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

the class SeleniumEasyTest method alertConfirmTest.

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

            Predicate<WebElement> textCheck = _element -> {
                String _text = _element.getText();
                System.err.println("in filter: Text = " + _text);
                return (Boolean) (_text.contains("Click me!"));
            };

            @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 {
        // dismiss alert
        driver.switchTo().alert().dismiss();
    } catch (NoAlertPresentException ex) {
    // Alert not present - ignore
    } catch (WebDriverException ex) {
        System.err.println("Alert was not handled : " + ex.getStackTrace().toString());
        return;
    }
    assertThat(driver.findElement(By.id("confirm-demo")).getText(), is(equalTo("You pressed Cancel!")));
}
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) 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)

Aggregations

NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)23 WebDriverException (org.openqa.selenium.WebDriverException)20 WebElement (org.openqa.selenium.WebElement)20 Test (org.testng.annotations.Test)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Test (org.junit.Test)9 NgWebElement (com.github.sergueik.jprotractor.NgWebElement)6 Pattern (java.util.regex.Pattern)6 Matcher (java.util.regex.Matcher)5 WebDriver (org.openqa.selenium.WebDriver)5 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Alert (org.openqa.selenium.Alert)4 By (org.openqa.selenium.By)4 ExpectedCondition (org.openqa.selenium.support.ui.ExpectedCondition)4 AfterMethod (org.testng.annotations.AfterMethod)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3