Appi
Appi

Reputation: 95

How do i fail my testcase with testNG assertion?

My below code executed 6 times, clicked on 6 options and fetched the value from the dropdown and displaying on console. If a single listitem is missing from dropdown, its displaying 'Missing Listitem' on console. And if one of the item missing, the testcase should get failed but without stopping execution until it displays all the listitems for all taboptions. Here is the code:

public void patientDetails() throws IOException {

        for(int i=0; i<patientDetailsOptions.size();i++)  //Total 6 times loop is going to be execute as 6 tab options are in one row.
        {            
                clickDropdown.get(i).click();  //Clicking on dropdown of each taboptions

        List<WebElement> Patient_details = clickDropdown.get(i).findElements(By.xpath("//div[@id='modal-patient-detail-section']/ul[@class='patient-demographics-dropdown open']/ul/li"));  
        //Fetching list item from that dropdownand storing into Patient_details.


        //Now printing all list item of taboption one after other if, list items are not as per size should display "Missing Listitem".
        for(int k=0; k< Patient_details.size(); k++)
            {
                String toolTips = Patient_details.get(k).getText(); 
                if(!toolTips.isEmpty())
                {
                    toolTips=toolTips.replaceAll("\n", " ");
                    System.out.println(toolTips);

                }else {
                     System.err.println("Missing ListItem");
                         //Assert.assertFalse(toolTips.isEmpty());
                     softAssert.assertFalse(toolTips.isEmpty());

                       }
                    }
                 }
              }

My above code executed successfully with Failures:0 with one 'Missing ListItem'. If listitmes are not as per Patient_details.size(), my code is printing ' Missing ListItem'. And if once found 'Missing ListItems' on console it should fail the test case but should not stop the execution until it displays the listitmes for each tab options.

With "softassert.assertFalse(toolTips.isEmpty)" shouldn't stop further execution but my test case got pass, i wanted it to be fail as one listitem is missing. Any help will be appreciated.

Upvotes: 0

Views: 44

Answers (1)

Muzzamil
Muzzamil

Reputation: 2881

If you want to make it fail if Missing ListItems then simply use fail assertion.

Please use below line

softAssert.fail("Failing as list item is missing.");

OR

softAssert.assertFalse(toolTips.isEmpty);

But Most important:

You have to use this line in end (after patientDetails() OR For loop) to accumulate soft assertion result.

softAssert.assertAll();

Here is good example to understand soft assertion.

Upvotes: 1

Related Questions