0

I'm working with selenium/Java /TestNG Maven project.

Main function calls multiple validation from java class. If any one of the verification fails, the remaining are skipped.

From the below code if, autoRenewalChargedPage.selectUseCase(testDataMap); , fails remaining to verifications are skipping.

Note: I tried softassert.assertall.This doesn't work

Main Function:


package com.truist.eb.truistweb.content;

import java.util.List;
import java.util.Map;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import com.truist.base.tests.BaseTest;
import com.truist.base.utility.ExcelUtils;

public class AutoRenewalChargedPageTests extends BaseTest {
    SoftAssert softAssert = new SoftAssert();

    @Test(dataProvider = "autoRenewalChargedtestdata", priority = 1)
    public void autoRenewalChargedTest(String key, Map<String, List<String>> testDataMap) throws InterruptedException {
        AutoRenewalChargedPage autoRenewalChargedPage = new AutoRenewalChargedPage(getDriver());

        autoRenewalChargedPage.selectUseCase(testDataMap);
        autoRenewalChargedPage.validateTeaserUC1(testDataMap);
        autoRenewalChargedPage.validateStoryUC1(testDataMap);
        softAssert.assertAll();
    }

    // Data Providers

    @DataProvider(name = "autoRenewalChargedtestdata")
    public Object[][] autoRenewalChargedTestData() throws Exception {
        Map<String, List<String>> testDataMap = ExcelUtils.getTestDataMap(webContentTestDataFile,
                "AutoRenewalChargedPage");
        Object[][] testData = new Object[1][2];
        testData[0][0] = "testData";
        testData[0][1] = testDataMap;
        return testData;
    }

}

1 Answer 1

0

To continue verifying the remaining validations even if one verification fails, you can modify your code to catch the exceptions thrown during the verifications and handle them appropriately. Here's an example of how you can achieve this:

@Test(dataProvider = "autoRenewalChargedtestdata", priority = 1)
public void autoRenewalChargedTest(String key, Map<String, List<String>> testDataMap) throws InterruptedException {
    AutoRenewalChargedPage autoRenewalChargedPage = new AutoRenewalChargedPage(getDriver());
    boolean overallVerificationStatus = true; // Flag to track overall verification status

    try {
        autoRenewalChargedPage.selectUseCase(testDataMap);
    } catch (Exception e) {
        overallVerificationStatus = false;
        // Log or report the verification failure
    }

    try {
        autoRenewalChargedPage.validateTeaserUC1(testDataMap);
    } catch (Exception e) {
        overallVerificationStatus = false;
        // Log or report the verification failure
    }

    try {
        autoRenewalChargedPage.validateStoryUC1(testDataMap);
    } catch (Exception e) {
        overallVerificationStatus = false;
        // Log or report the verification failure
    }

    // Assert the overall verification status
    softAssert.assertTrue(overallVerificationStatus);

    // Perform other assertions or actions as needed
    // softAssert.assertEquals(...);
    // softAssert.assertTrue(...);

    softAssert.assertAll();
}

In this approach, each verification is enclosed within a separate try-catch block. If any verification fails and throws an exception, the catch block will be executed, and the overallVerificationStatus flag will be set to false. By the end of the test, the softAssert.assertTrue(overallVerificationStatus) assertion will be performed to check the overall verification status. If the flag is false, it means at least one verification failed, and the test will be marked as a failure.

By implementing this solution, you will be able to continue executing the remaining verifications even if one of them fails. Remember to handle exceptions appropriately and log/report the verification failures for further analysis.

4
  • TY, but this solution doesn't work. Still the same issue.
    – Padmaja
    Jun 22, 2023 at 16:00
  • autoRenewalChargedPage.validateTeaserUC1 , when this executes and fails the validation , softassert.assert all is validating only for this .below is the code for 'validateTeaser UC1'
    – Padmaja
    Jun 22, 2023 at 16:03
  • public void validateTeaserUC1(Map<String, List<String>> testDataMap) throws InterruptedException { driver.switchTo().frame(driver.findElement(By.xpath("//*[@class='eb-teaser-container']/iframe"))); softAssert.assertEquals(driver .findElement(By.xpath("//*[@data-perso-block-id='title']//*[@class='pstory-block-content']")).getText(), testDataMap.get("Teaser_Title").get(0), "Teaser Title doesn't match"); softAssert.assertEquals(driver.findElement(By.xpath( "//*[@class='pstory-block-frame pstory-block-text perso-teaser-text']//*[@class= 'pstory-block-content']"))
    – Padmaja
    Jun 22, 2023 at 16:04
  • .getText(), testDataMap.get("Teaser Text").get(0), "Teaser Text doesn't match"); softAssert.assertEquals(driver.findElement(By.xpath("//*[@id=\"staticLink\"]")).getText(), testDataMap.get("Teaser_CTA").get(0), "Teaser CTA name doesn't match"); driver.switchTo().parentFrame(); softAssert.assertAll(); Thread.sleep(2000); }
    – Padmaja
    Jun 22, 2023 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.