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());
}
Aggregations