Search in sources :

Example 21 with NoAlertPresentException

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

the class SuvianTest method test13_1.

// This test appears to find the button even though it is inside iframe
// without frame switch
@Test(enabled = true)
public void test13_1() {
    // Arrange
    driver.get("http://suvian.in/selenium/2.3frame.html");
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".container .row .intro-message iframe")));
    // Act
    WebElement buttonElement = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("h3 button"))));
    assertThat(buttonElement, notNullValue());
    // Assert
    WebElement currentElement = buttonElement;
    for (int cnt = 0; cnt != 4; cnt++) {
        try {
            WebElement containerElement = currentElement.findElement(By.xpath(".."));
            String elementHTML = containerElement.getAttribute("outerHTML");
            System.err.println("Parent element: " + (elementHTML.length() > 120 ? elementHTML.substring(0, 120) + "..." : elementHTML));
            currentElement = containerElement;
        } catch (InvalidSelectorException e) {
            // ignore - reached top level
            break;
        }
    }
    buttonElement.click();
    // Assert
    try {
        alert = driver.switchTo().alert();
        String alertText = alert.getText();
        assertTrue(alertText.contains("You clicked a button within a frame"));
        // confirm alert
        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 : InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) Point(org.openqa.selenium.Point) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.testng.annotations.Test)

Example 22 with NoAlertPresentException

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

the class GmailTest method loginTest.

// the gmail of showing the identity confirmation page the test is not ready to handle
@Test(priority = 4, enabled = false)
public void loginTest() throws InterruptedException, IOException {
    // Click on Sign in Link
    driver.findElement(signInLink).click();
    // origin:
    // https://github.com/TsvetomirSlavov/JavaScriptForSeleniumMyCollection
    // Wait for page url to change
    ExpectedCondition<Boolean> urlChange = driver -> driver.getCurrentUrl().matches("^https://accounts.google.com/signin.*");
    wait.until(urlChange);
    // TODO: examine it landed on https://accounts.google.com/AccountChooser
    // Enter the email id
    enterData(identifier, "automationnewuser24@gmail.com");
    /*
		File screenshotFile = screenshot.getScreenshotAs(OutputType.FILE);
		// Move image file to new destination
		FileUtils.copyFile(screenshotFile, new File("c:\\temp\\UserID.jpg"));
		*/
    // Click on next button
    clickNextButton(identifierNextButton);
    // Enter the password
    enterData(passwordInput, "automationnewuser2410");
    // Click on next button
    clickNextButton(passwordNextButton);
    // Wait for page url to change
    /*
		urlChange = driver -> {
			String url = driver.getCurrentUrl();
			System.err.println("The url is: " + url);
			return (Boolean) url.matches("^https://mail.google.com/mail.*");
		};
		wait.until(urlChange);
		 */
    wait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver webDriver) {
            System.out.println("Checking if mail page is loaded...");
            // https://github.com/pawnu/SeleniumQA/blob/master/EcommerceProject/RunTest.java
            return checkPage();
        }
    });
    // Wait until form is redndered, old semantics
    wait.until(new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {
            System.err.println("Wait for form to finish rendering");
            JavascriptExecutor js = ((JavascriptExecutor) driver);
            Boolean active = (Boolean) js.executeScript("return document.readyState == 'complete'");
            if (active) {
                System.err.println("Done");
            }
            return active;
        }
    });
    System.err.println("Click on profile image");
    // Click on profile image
    wait.until((WebDriver driver) -> {
        WebElement element = null;
        try {
            element = driver.findElement(profileImage);
        } catch (Exception e) {
            return null;
        }
        return (element.isDisplayed()) ? element : null;
    }).click();
    // Wait until form is redndered, lambda semantics
    wait.until((WebDriver driver) -> {
        System.err.println("Wait for form to finish rendering");
        JavascriptExecutor js = ((JavascriptExecutor) driver);
        Boolean active = (Boolean) js.executeScript("return document.readyState == 'complete'");
        if (active) {
            System.err.println("Done");
        }
        return active;
    });
    // Sign out
    System.err.println("Sign out");
    highlight(driver.findElement(signOutButton), 100);
    driver.findElement(signOutButton).click();
    try {
        alert = driver.switchTo().alert();
        alert.accept();
    } catch (NoAlertPresentException ex) {
        // Alert not present
        System.err.println("NoAlertPresentException (ignored): " + ex.getStackTrace());
        return;
    } catch (WebDriverException ex) {
        System.err.println("Alert was not handled by PhantomJS: " + ex.getStackTrace().toString());
        return;
    }
}
Also used : By(org.openqa.selenium.By) WebDriver(org.openqa.selenium.WebDriver) WebDriverException(org.openqa.selenium.WebDriverException) BeforeClass(org.testng.annotations.BeforeClass) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) BeforeMethod(org.testng.annotations.BeforeMethod) WebElement(org.openqa.selenium.WebElement) IOException(java.io.IOException) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ArrayList(java.util.ArrayList) List(java.util.List) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Assert.assertTrue(org.testng.Assert.assertTrue) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) WebDriver(org.openqa.selenium.WebDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) 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.testng.annotations.Test)

Example 23 with NoAlertPresentException

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

the class AppTest method test13_1.

// NOTE: this test is hanging the jbrowserdriver
// after the test is run orphaned java processess require a taskkill
@Ignore
@Test
public void test13_1() {
    // Arrange
    driver.get("http://suvian.in/selenium/2.3frame.html");
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".container .row .intro-message iframe")));
    // Act
    WebElement buttonElement = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("h3 button"))));
    assertThat(buttonElement, notNullValue());
    buttonElement.click();
    // Assert
    try {
        // confirm alert
        Alert alert = driver.switchTo().alert();
        String alert_text = alert.getText();
        assertThat(alert_text, containsString("You clicked on Green"));
    } 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) Alert(org.openqa.selenium.Alert) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) Ignore(org.junit.Ignore) Test(org.junit.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