Search in sources :

Example 6 with NoAlertPresentException

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

the class NgButtonIntegrationTest method testButtonNgIf.

@Test
public void testButtonNgIf() {
    // if (isCIBuild) { // Alert not handled by PhantomJS
    // return;
    // }
    getPageContent("ng_watch_ng_if.htm");
    WebElement button = seleniumDriver.findElement(By.cssSelector("button.btn"));
    ngDriver.waitForAngular();
    CommonFunctions.highlight(button);
    NgWebElement ng_button = new NgWebElement(ngDriver, button);
    assertThat(ng_button, notNullValue());
    assertThat(ng_button.getAttribute("ng-click"), equalTo("house.frontDoor.open()"));
    assertThat(ng_button.getText(), equalTo("Open Door"));
    ngDriver.waitForAngular();
    try {
        state = ((Boolean) ng_button.evaluate("!house.frontDoor.isOpen")).booleanValue();
    } catch (Exception ex) {
        System.err.println("evaluate was not handled : " + ex.getStackTrace().toString());
    }
    assertTrue(state);
    button.click();
    try {
        // confirm alert
        seleniumDriver.switchTo().alert().accept();
    } catch (NoAlertPresentException ex) {
    // Alert not present - ignore
    } catch (WebDriverException ex) {
        System.err.println("Alert was not handled : " + ex.getStackTrace().toString());
        return;
    }
    button = seleniumDriver.findElement(By.cssSelector("button.btn"));
    CommonFunctions.highlight(button);
    ng_button = new NgWebElement(ngDriver, button);
    assertThat(ng_button, notNullValue());
    assertThat(ng_button.getAttribute("ng-click"), equalTo("house.frontDoor.close()"));
    assertThat(ng_button.getText(), equalTo("Close Door"));
    try {
        state = ((Boolean) ng_button.evaluate("house.frontDoor.isOpen")).booleanValue();
    } catch (Exception ex) {
        // [JavaScript Error: "e is null"] ?
        System.err.println("evaluate was not handled : " + ex.getStackTrace().toString());
    }
    assertTrue(state);
}
Also used : 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) IOException(java.io.IOException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.junit.Test)

Example 7 with NoAlertPresentException

use of org.openqa.selenium.NoAlertPresentException 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 NoAlertPresentException

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

the class SuvianTest method test13_2.

// This test does switch frame fefore find the button in the iframed document
@Test(enabled = true)
public void test13_2() {
    // Arrange
    driver.get("http://suvian.in/selenium/2.3frame.html");
    WebElement frameElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".container .row .intro-message iframe")));
    assertThat(frameElement, notNullValue());
    String frameElementDocument = frameElement.getAttribute("outerHTML");
    System.err.println("Switching to frame:\n" + (frameElementDocument.length() > 80 ? frameElementDocument.substring(0, 80) + "..." : frameElementDocument));
    driver.switchTo().frame(frameElement);
    System.err.println("Inside the frame");
    try {
        frameElement.getTagName();
    } catch (org.openqa.selenium.StaleElementReferenceException e) {
        // Element belongs to a different frame than the current one
        System.err.println("Exception (expected, ignored): " + e.toString());
    }
    WebElement rootElement = driver.findElement(By.xpath("/*"));
    String documentText = rootElement.getAttribute("outerHTML");
    System.err.println("Root element:\n" + (documentText.length() > 80 ? documentText.substring(0, 80) + "..." : documentText));
    String pageSource = driver.getPageSource();
    System.err.println("Page source:\n" + (pageSource.length() > 80 ? pageSource.substring(0, 80) + "..." : pageSource));
    // Act
    WebElement buttonElement = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("h3 button[onclick = 'myFunction()']"))));
    assertThat(buttonElement, notNullValue());
    buttonElement.click();
    try {
        // confirm alert
        alert = driver.switchTo().alert();
        String alertText = alert.getText();
        assertTrue(alertText.contains("You clicked a button within a frame"));
        alert.accept();
    } catch (NoAlertPresentException e) {
    // Alert not present - ignore
    } 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) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.testng.annotations.Test)

Example 9 with NoAlertPresentException

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

the class SuvianTest method test18_2.

// compute the size of the progressbar
@Test(enabled = true)
public void test18_2() {
    // Arrange
    driver.get("http://suvian.in/selenium/2.8progressBar.html");
    WebElement button1 = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector(".container .row .intro-message button:nth-of-type(1)"))));
    assertThat(button1, notNullValue());
    assertThat(button1.getAttribute("disabled"), nullValue());
    WebElement button2 = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("button2"))));
    assertThat(button2, notNullValue());
    assertThat(button2.getAttribute("disabled"), notNullValue());
    WebElement progressBar = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("myBar"))));
    assertThat(progressBar, notNullValue());
    assertThat(progressBar.getAttribute("style"), notNullValue());
    WebElement progressBarContainer = progressBar.findElement(By.xpath(".."));
    int denom = Integer.parseInt(styleOfElement(progressBarContainer, "width").replace("px", ""));
    // Act
    button1.click();
    // Evaluate the progressBar width to reach 100% - need a longer wait timeout
    try {
        (new WebDriverWait(driver, 60)).until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver d) {
                Pattern p = Pattern.compile("([0-9.]+)px", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
                float percentage = Float.parseFloat(p.matcher(styleOfElement(progressBar, "width")).replaceAll("$1")) / denom;
                System.err.println("Progress Bar: " + percentage);
                return (percentage >= .97);
            }
        });
    } catch (Exception e) {
        System.err.println("Exception: " + e.toString());
    }
    // Assert
    sleep(100);
    System.err.println("Button2 attribute check (1) : " + button2.getAttribute("outerHTML"));
    button2.click();
    try {
        // confirm alert
        driver.switchTo().alert().accept();
    } catch (NoAlertPresentException e) {
    // Alert not present - ignore
    } catch (WebDriverException e) {
        System.err.println("Alert was not handled : " + e.getStackTrace().toString());
        return;
    }
    // NOTE: latency
    System.err.println("Button2 attribute check (2) : " + button2.getAttribute("outerHTML"));
}
Also used : WebDriver(org.openqa.selenium.WebDriver) Pattern(java.util.regex.Pattern) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) WebElement(org.openqa.selenium.WebElement) Point(org.openqa.selenium.Point) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriverException(org.openqa.selenium.WebDriverException) InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.testng.annotations.Test)

Example 10 with NoAlertPresentException

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

the class SuvianTest method test17.

// wait while the alert is being displayed
@Test(enabled = true)
public void test17() {
    // Arrange
    driver.get("http://suvian.in/selenium/2.7waitUntil.html");
    WebElement clickElement = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector(".container .row .intro-message h3 a"))));
    assertThat(clickElement, notNullValue());
    assertThat(clickElement.getText(), containsString("Click Me"));
    // Act
    long startTime = System.currentTimeMillis();
    clickElement.click();
    // Assert
    long retryInterval = 1000;
    long maxRetry = 30;
    long checkRetryDelay = 0;
    while (true) {
        try {
            // confirm alert
            driver.switchTo().alert().accept();
            break;
        } catch (NoAlertPresentException e) {
            // check if waited long enough already
            checkRetryDelay = System.currentTimeMillis() - startTime;
            if (Math.ceil(checkRetryDelay / retryInterval) > maxRetry + 1) {
                System.err.format("Alert not present after %d second\n", checkRetryDelay);
                throw new RuntimeException();
            }
            try {
                System.err.print(String.format("Sleep %4.2f sec ...", Math.ceil(retryInterval / 1000)));
                Thread.sleep(retryInterval);
            } catch (InterruptedException e2) {
                System.err.println("Unexpected Interrupted Exception: " + e.getStackTrace().toString());
                throw new RuntimeException(e.toString());
            }
        } catch (Exception e) {
            System.err.println("Unexpected exception: " + e.getStackTrace().toString());
            throw new RuntimeException(e.toString());
        }
    }
    long delaySecond = (checkRetryDelay / 1000) % 60;
    long delayMinute = (checkRetryDelay / (1000 * 60)) % 60;
    long delayHour = (checkRetryDelay / (1000 * 60 * 60)) % 24;
    String delayTime = String.format("%02d:%02d:%02d", delayHour, delayMinute, delaySecond);
    System.err.format("Alert was confirmed at %s\n", delayTime);
}
Also used : NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriverException(org.openqa.selenium.WebDriverException) InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) 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