Transcripted Summary

In the previous chapter, we ran our test script and it showed that the test passed even without a verification step.

Let's create new tests with assertions. Our test requirement is to verify the home page for OrangeHRM.

The code will sign in which will take us to the home page. From there, we'll verify the Welcome hyperlink, Admin tab, and Dashboard.


public class VerifySign_HA_PASS
{	
	@BeforeClass
	public void setUp() {}
	
	@Test
	public void signIn() {}
	
	@Test
	public void testHomePageVerification() {}
	
	@Test
	public void userSearch() {}
	
	@Test
	public void userSignOut() {}
	
	@AfterClass
	public void tearDown() {}
}

Let's add assertions to the testHomePageVerification method. Our first assertion will be assertEquals.

@Test
public void testHomePageVerification() 
{
    Assert.assertEquals(true, true, "The Welcome Link Is Not Correct On The Home Page");
}


The first parameter represents the actual value. The second parameter represents the expected value. Let's set those both to true for now, just to see how it works. The third parameter is a String and it represents the message that will show up only in the event of an assertion failure.

Let's also add assertions assertFalse and assertTrue. Both of these methods accept a boolean condition, and an optional String for the message. assertTrue asserts that the condition is true, and assertFalse asserts that the condition is false.


@Test
public void testHomePageVerification() 
{
    Assert.assertEquals(true, true, "The Welcome Link Is Not Correct On The Home Page");

    Assert.assertFalse(false, "The Admin Tab Is Not Displayed On The Home Page");
    
    Assert.assertTrue(true, "The Dashboard Is Not Correct On The Home Page");
}


After running this, we see all tests passed. Now, let’s see what happens when this test fails. Let's change the condition in the assertEquals and assertTrue methods. I'll also add print statements after each to demonstrate the flow.


@Test
public void testHomePageVerification() 
{
    Assert.assertEquals(true, false, "The Welcome Link Is Not Correct On The Home Page");
    System.out.println("3. Verify Welcome Link");

    Assert.assertFalse(false, "The Admin Tab Is Not Displayed On The Home Page");
    System.out.println("4. Verify Admin Tab");
    
    Assert.assertTrue(false, "The Dashboard Is Not Correct On The Home Page");
    System.out.println("5. Verify Dashboard");		
}

As expected, after running this, testHomePageVerification failed. Let’s look at the output. Steps 3, 4, and 5 are missing. The AssertionError shows our message "The Welcome Link Is Not Correct On The Home Page" because our test failed. However, this is the only AssertionError shown, even though we know that assertTrue should also fail. There is only one AssertionError because we used a hard assert.

A hard assert stops immediately after a failure then continues on to the next annotation. In this case, assertEquals failed so the subsequent assertions within this method were skipped and the script went on to the next @Test method, userSearch.

To have all assertions within a test evalauted, you can use soft asserts. We'll discuss those in the next section.



Resources



Quiz:

Note: Chapter 6 has been divided into multiple sub-chapters. You'll find the Quiz on the last sub-chapter, Chapter 6.3



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR