user2195411
user2195411

Reputation: 237

SoftAssert in Selenium Test Class

I have a test method inside a test class where I want to verify a couple things, only fail after I soft assert in this specific test method.

But, I feel my test method is getting messy with failure handling. I haven't been able to find any best practices on this. Any ideas? If I move the asserts into the page object class, it will be a bit messy there too.

@Test
public void test() {

    // steps here

    // then asserts here
    SoftAssert soft = new SoftAssert();
    String expectedHeaderText = "foo";

    soft.assertTrue(pageObjectClass.isHeaderPresent(), "Unable to find the Header page object.");


    soft.assertTrue(pageObjectClass.getHeader().contains(expectedHeaderText),
            String.format("Expected to find '%s'.  Page actually shows '%s'", expectedHeaderText, pageObjectClass.getHeader()));

    // more asserts

    sa.assertAll();

}

Upvotes: 0

Views: 3851

Answers (3)

Max Daroshchanka
Max Daroshchanka

Reputation: 2978

It's always a dilemma: to have explicit checks and readable error messages, or omit something in order to make the code shorter or more generic to be reused.

Your example is a plain SoftAssert usage which is recommended by many tutorials.

It's a best practice to keep all assertions on Test Level, not in Page Objects.

But how to deal when you see, some assertions are huge and duplicated within several test methods?

I suggest following next rules:

  • Similar(duplicated) assert constructions can be moved to a new helper method within a current Test Class.
  • Try to split the tests into classes in the way, test classes contain only similar test methods.
  • If several test classes contain the same code, you can create a common parent for a group of classes and move some reusable code on this test-class-group level.
  • Do not try to fully avoid duplications on Test Level, tests are something that is changed and outdated rapidly. They should be still readable and easy to understand what happens in the code.

I don't use SoftAsserts, but can suggest some of this as a point for the extension:

import org.testng.asserts.SoftAssert

import static java.lang.String.format

public class ProjectSoftAssert extends SoftAssert {

    public void assertElementVisibleAndContainsText(
            boolean isVisible, String actualText, String expectedText, String elementName
    ) {
        assertTrue(isVisible, format("Unable to find the '%s' page object.", elementName));
        assertTrue(
                actualText.contains(expectedText),
                format(
                        "Wrong '%s' page object text. Expected to find '%s'. Page actually shows '%s'",
                        elementName, expectedText, actualText
                )
        );
    }

}

And in your scenario:


@Test
public void test() {
    // steps here
    // then asserts here
    ProjectSoftAssert soft = new ProjectSoftAssert();
    soft.assertElementVisibleAndContainsText(
            pageObjectClass.isHeaderPresent(), pageObjectClass.getHeader(), "foo", "Header"
    );
    // more asserts
    soft.assertAll();
}

Upvotes: 0

user861594
user861594

Reputation: 5908

You can check QMetry Automation Framework which provided assertion and verification methods. For example:

//verify element present 
firstName.verifyPresent();
firstName.assertPresent();

//verify Text of Element
firstName.verifyText("First User");
firstName.assertText("First User");

//verify Text of element with StringMatchers conditions
firstName.verifyText(StringMatcher.contains("First User"));
firstName.assertText(StringMatcher.contains("First User"),"Username Validation");

In case of assert method, your test will not continue on assert fail.

In case of any verify method, your test will continue even if verification failed and the final status of test will be failed if one or more verification failed.

Upvotes: 0

prithvi394
prithvi394

Reputation: 221

Check below convention

@Test
public void test() {
// steps here

// then asserts here
SoftAssert soft = new SoftAssert();
String expectedHeaderText = "foo";

Boolean checkHeader=pageObjectClass.isHeaderPresent() //Change the method on POM pageObjectClass such that it returns the true or false
soft.assertTrue(checkHeader,true);

String checkHeaderContent=pageObjectClass.getHeader()//change method on POM pageObjectClass to return a string
soft.assertTrue(checkHeaderContent.contains(expectedHeaderText), String.format("Expected to find '%s'.  Page actually shows '%s'", expectedHeaderText, checkHeaderContent));
// more asserts
sa.assertAll();

}

Upvotes: 1

Related Questions