0

Background

I am using Extent Report Cucumber Adapter for my Cucumber based test automation framework built using Java which is running on Junit. I am using AssertJ assertions for the test conditions.

Scenario

One of the test scenarios require to test all the links on a web page. I have written the code for the same and it's working fine. I am using AssertJ assertion for the test condition under a try block and catching the SoftAssertionError so that my test execution doesn't halt because of the exception and continue validating all the remaining links even if it finds any broken link.

The report mentions the links which are found broken. However, ideally this step should get failed as the script found some broken links. But the report marks the overall step as passed and as a result the scenario is also marked as passed. Now I am not able to figure out how can I mark the step as failed in my Extent Report provided there are some links found which are broken. Kindly suggest a way to do this. I am providing a small snippet of my code for a better understanding.

public void ValidateAllLinks(String linkURL) {

    try
    {
        URL url = new URL(linkURL);

        //Creating url connection and getting the response code

        HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
        httpURLConnect.setConnectTimeout(5000);
        httpURLConnect.connect();

        try {
            
            SoftAssertions softly = new SoftAssertions();
            softly.assertThat(httpURLConnect.getResponseCode()).as("This is a broken link: " + linkURL).isGreaterThanOrEqualTo(400);
            softly.assertAll();
            
        }catch (SoftAssertionError e)

        {
            e.printStackTrace();

        }


        if(httpURLConnect.getResponseCode()>=400)
        {
            System.out.println(linkURL+" - "+httpURLConnect.getResponseMessage()+" is a broken link.");
            ExtentCucumberAdapter.addTestStepLog("<b>" + "<font color=" + "red>" + linkURL+" - "+httpURLConnect.getResponseMessage()+" is a broken link." + "</font>" + "</b>");

        }    

        //Fetching and Printing the response code obtained

        else{

            System.out.println(linkURL+" - "+httpURLConnect.getResponseMessage()+" is working as expected.");
        }

    }catch (Exception e) {

    }

}
Arun Gupta
  • 25
  • 3

1 Answers1

0

Your example is not a good fit for soft assertions as you are testing only one thing. Soft assertions are meant to assert a bunch of things and once you have asserted all the things you wanted to, you call assertAll().

I don't understand why you test twice httpURLConnect.getResponseCode() you could do it once, add the test step log and then fail the test with a fail() method call (either from AssertJ or JUnit)

Joel Costigliola
  • 6,308
  • 27
  • 35
  • Hey @Joel, I agree to your points regarding the usage of Soft assertions and the httpURLConnect.getResponseCode() method. I have corrected the code for that. However, my problem is still not resolved. Using fail() method will mark the test as failed but in this case, as soon as I encounter a broken link my test will fail and it will break the loop. Ideal solution would be to continue the execution and mark the test as failed at the end of the execution so that I can extract all the broken links. – Arun Gupta Jul 16 '21 at 09:22
  • the example is confusing because there is only one link. if you have multiple links, then create one soft assertions and reuse it to check all the links and after all links are checked call assertAll(). – Joel Costigliola Jul 16 '21 at 13:45