SoftAssertions - selenide/selenide GitHub Wiki
Selenide provide point to perform built in verification softly - this mean that Selenide will skip failed verification and collect all of it and throw error only at the end of test.
- TestNG listener -
com.codeborne.selenide.testng.SoftAsserts
- JUnit4 rule -
com.codeborne.selenide.junit.SoftAsserts
- JUnit5 extension -
com.codeborne.selenide.junit5.SoftAssertsExtension
Page with 2 elements, where 2 at invisible
...
<div id='first' style='display:none;'>First</div>
<div id='second' style='display:none;'>Second</div>
...
And use code like this:
public class Tests {
@Test
public void test() {
$("#first").should(visible).click();
$("#second").should(visible).click();
}
}
In normal way test fail on a first row because first item is not visible and second item will never be touched and verified.
But if you want to verify both items you can use Selenide soft assets. In this case Selenide will perform condition verification and then click on both of the items and it some item unavailable for some reason it will generate errors for each fail after test end.
@Listeners({ SoftAsserts.class})
public class Tests {
@Test
public void test() {
Configuration.assertionMode = SOFT;
open("page.html");
$("#first").should(visible).click();
$("#second").should(visible).click();
}
}
- Don't forget to use dependency
"com.codeborne:selenide-testng"
instead of just"com.codeborne:selenide"
to use TestNG-specific classes.
public class Tests {
@Rule
public SoftAsserts softAsserts = new SoftAsserts();
@Test
public void test() {
Configuration.assertionMode = SOFT;
open("page.html");
$("#first").should(visible).click();
$("#second").should(visible).click();
}
}
- Don't forget to use dependency
"com.codeborne:selenide-junit4"
instead of just"com.codeborne:selenide"
to use JUnit4-specific classes.
@ExtendWith({SoftAssertsExtension.class})
class Tests {
@Test
void test() {
Configuration.assertionMode = SOFT;
open("page.html");
$("#first").should(visible).click();
$("#second").should(visible).click();
}
}
Or register extension inside test class:
class Tests {
@RegisterExtension
static SoftAssertsExtension softAsserts = new SoftAssertsExtension();
@Test
void test() {
Configuration.assertionMode = SOFT;
open("page.html");
$("#first").should(visible).click();
$("#second").should(visible).click();
}
}
- You can use the old good dependency
"com.codeborne:selenide"
because it contains JUnit5-specific classes.
Element not found {#first}
Expected: visible
Screenshot: file://build/reports/tests/1536060081565.0.png
Page source: file://build/reports/tests/1536060081565.0.html
Timeout: 4 s.
Caused by: NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#first"}
FAIL #1: Element not found {#first}
Expected: visible
Screenshot: file://build/reports/tests/1536060329615.0.png
Page source: file://build/reports/tests/1536060329615.0.html
Timeout: 4 s.
Caused by: NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#first"}
FAIL #2: Element not found {#second}
Expected: visible
Screenshot: file://build/reports/tests/1536060334390.1.png
Page source: file://build/reports/tests/1536060334390.1.html
Timeout: 4 s.
Caused by: NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#second"}