Assert is to compare the expected with actual.
Assert
is class exposed from org.testng.Assert;
2 types of Asserts –
- Soft Assert
- hard Assert
Soft Assert
Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and would continue with the next step after assert statement.
This is usually used when our test requires multiple assertions to be executed and the user want all of the assertions/codes to be executed before failing/skipping the tests.
testNG exposes SoftAssert
class to achieve soft asserts
We need to create object of SoftAssert class and then perform the assertions.
Example –
SoftAssert softAssert = new SoftAssert(); softAssert.assertTrue(true, "condition is false, just testing");
assertTrue(), 2nd parameter will print when the condition is false or not met, but it won’t stop the execution rather continue to next line of code.
Let’s see different soft assertions
@Test public void SoftAssertTest(){ SoftAssert softAssert = new SoftAssert(); softAssert.assertTrue(false, "Just testing soft assert true condition not met"); softAssert.assertEquals(true, false, "just for testing - true vs false"); softAssert.assertEquals(1, 1); System.out.println("TestCase finished"); softAssert.assertAll(); }
when you run above code, even the assertions failed [1st & 2nd], it will continue to the 3rd assertion, and at the end the test will fail.
If you want to continue to execute the test even assertion fails and make the test case pass, then remove the softAssert.assertAll();
Hard Assert
A Hard Assertion is type of assertion that throws an exception immediately when an assert statement fails & stops the execution for current test
Hard asserts can be handled by using AssertionError in try catch block
Assert.assertTrue(condition, “fail message”)
Assert.assertTrue: is to evaluate a condition.
Fails, if expected != actual, then test execution stops with exception: java.lang.AssertionError
Else, Program continues normal flow after checking the Assert
Assert.assertEquals(actual, expected, “fail message”)
Assert will pass, if the actual = expected, else execution stops.
To know more available methods, you can type Assert. & will see all available methods to validate.
Examples:
If URL loaded correctly, then proceed with login.
If login successful, then proceed with rest of functionality.
To check if a table exist or not, if not present, then no need to loop through the content..
Let’s see the implementation of Assert.
@Test public void HardAssertTest1() { WebDriver driver; System.setProperty("webdriver.chrome.driver", "/Users/skpatro/sel/chromedriver"); driver = new ChromeDriver(); driver.get("https://qavbox.github.io/demo/signup/"); Assert.assertEquals(driver.getTitle(), "Registration Form", "title is not correct"); System.out.println("TestCase finished"); }
driver.getTitle() is the actual value fetched from the browser url launched.
actual is “Registration Form”, if matched assertion is passed.
In this example this assertion will pass & the output will be
TestCase finished.
Let’s make assertion fail, by matching wrong title [intentionally will do the assertion fail]
@Test public void HardAssertTest1() { WebDriver driver; System.setProperty("webdriver.chrome.driver", "/Users/skpatro/sel/chromedriver"); driver = new ChromeDriver(); driver.get("https://qavbox.github.io/demo/signup/"); Assert.assertEquals(driver.getTitle(), "Registration Form1", "title is not correct"); System.out.println("TestCase finished"); }
This above example will throw error and will not print “TestCase finished”.
OutPut –
java.lang.AssertionError: title is not correct expected [Registration Form1] but found [Registration Form]
Expected :Registration Form1
Actual :Registration Form
To handle this assertion we can use try catch block, but it’s not recomended, instead we can use soft assertion.
try{ Assert.assertEquals(driver.getTitle(), "Registration Form1", "title is not correct"); }catch(AssertionError as){ System.out.println(as.getMessage()); }
This will not stop the execution even the assertion fails.
The same way, we can use any kind of Assert available methods.
1 Comment