Search in sources :

Example 1 with SoftAssert

use of org.testng.asserts.SoftAssert in project selenium_java by sergueik.

the class SuvianTest method test0_1.

// Firebug console validation:
// $x("<xpath>")
// $$("<cssSelector>")
@Test(enabled = true)
public void test0_1() {
    // Arrange
    driver.get("http://suvian.in/selenium/1.1link.html");
    // Wait page to load
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector(".container .row .intro-message h3 a"))));
    List<WebElement> elements = driver.findElements(By.xpath("//div[1]/div/div/div/div/h3[2]/a"));
    // assertTrue(elements.size() > 0);
    // http://grepcode.com/file/repo1.maven.org/maven2/org.testng/testng/6.8/org/testng/asserts/SoftAssert.java
    SoftAssert softAssert = new SoftAssert();
    softAssert.assertTrue(elements.size() > 0);
    // System.out.println("softAssert Method Was Executed");
    softAssert.assertAll();
    // Act
    WebElement element = elements.get(0);
    highlight(element);
    assertTrue(element.getText().equalsIgnoreCase("Click Here"), element.getText());
    element.click();
    // Wait page to load
    try {
        wait.until(ExpectedConditions.urlContains("1.1link_validate.html"));
    } catch (UnreachableBrowserException e) {
    }
    // 1. Expected Condition uses enclosing element
    try {
        wait.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver d) {
                String t = d.findElement(By.className("intro-message")).getText();
                Boolean result = t.contains("Link Successfully clicked");
                System.err.println("in apply: Text = " + t + "\nresult = " + result.toString());
                return result;
            }
        });
    } catch (Exception e) {
        System.err.println("Exception: " + e.toString());
    }
    // 3. Alternative wait, functional style, with Optional <WebElement>
    // http://www.nurkiewicz.com/2013/08/optional-in-java-8-cheat-sheet.html
    // and capture-loaded Predicate
    Predicate<WebElement> textCheck = _element -> {
        String _text = _element.getText();
        System.err.println("in stream filter (3): Text = " + _text);
        return (Boolean) (_text.contains("Navigate Back"));
    };
    try {
        WebElement checkElement = wait.until(new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver d) {
                Optional<WebElement> e = d.findElements(By.cssSelector("div.container div.row div.intro-message h3")).stream().filter(textCheck).findFirst();
                return (e.isPresent()) ? e.get() : (WebElement) null;
            }
        });
        System.err.println("element check: " + checkElement.getAttribute("innerHTML"));
    } catch (Exception e) {
        System.err.println("Exception: " + e.toString());
    }
    // http://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds-in-java
    // http://stackoverflow.com/questions/31102351/selenium-java-lambda-implementation-for-explicit-waits
    Predicate<WebElement> textCheck2 = _element -> {
        String _text = _element.getText();
        System.err.println("(in filter) Text: " + _text);
        return (Boolean) (_text.equalsIgnoreCase("Link Successfully clicked"));
    };
    elements = driver.findElements(By.cssSelector(".container .row .intro-message h3"));
    // longer version
    Stream<WebElement> elementsStream = elements.stream();
    elements = elementsStream.filter(textCheck2).collect(Collectors.toList());
    // shorter version
    elements = driver.findElements(By.cssSelector(".container .row .intro-message h3")).stream().filter(_element -> "Link Successfully clicked".equalsIgnoreCase(_element.getText())).collect(Collectors.toList());
    assertThat(elements.size(), equalTo(1));
    elements = driver.findElements(By.cssSelector(".container .row .intro-message h3")).stream().filter(_element -> _element.getText().equalsIgnoreCase("Link Successfully clicked")).collect(Collectors.toList());
    assertThat(elements.size(), equalTo(1));
    element = elements.get(0);
    highlight(element);
    assertTrue(element.getText().equalsIgnoreCase("Link Successfully clicked"), element.getText());
}
Also used : WebDriver(org.openqa.selenium.WebDriver) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) WebElement(org.openqa.selenium.WebElement) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) StringUtils(org.apache.commons.lang3.StringUtils) Locatable(org.openqa.selenium.internal.Locatable) Coordinates(org.openqa.selenium.interactions.internal.Coordinates) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Matcher(java.util.regex.Matcher) Point(org.openqa.selenium.Point) Map(java.util.Map) Method(java.lang.reflect.Method) HasInputDevices(org.openqa.selenium.interactions.HasInputDevices) FindBy(org.openqa.selenium.support.FindBy) SoftAssert(org.testng.asserts.SoftAssert) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) Predicate(java.util.function.Predicate) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) Mouse(org.openqa.selenium.interactions.Mouse) Collectors(java.util.stream.Collectors) FindBys(org.openqa.selenium.support.FindBys) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriver(org.openqa.selenium.WebDriver) WebDriverException(org.openqa.selenium.WebDriverException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) ITestResult(org.testng.ITestResult) ArrayList(java.util.ArrayList) Select(org.openqa.selenium.support.ui.Select) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Iterator(java.util.Iterator) Keys(org.openqa.selenium.Keys) By(org.openqa.selenium.By) Consumer(java.util.function.Consumer) Assert.assertTrue(org.testng.Assert.assertTrue) Comparator(java.util.Comparator) Collections(java.util.Collections) Optional(java.util.Optional) SoftAssert(org.testng.asserts.SoftAssert) 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) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) Test(org.testng.annotations.Test)

Aggregations

Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StringUtils (org.apache.commons.lang3.StringUtils)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1