We create automated regression test to validate the application is working correctly. We need to add checkpoints/validation at different steps in the workflow to validate the application is correctly behaving or not.
For e.g.: Let us consider login into an e-mail website. We need to go to application URL and assert/validate username and password input box and Login button are displayed correctly. Then next step can be to assert login is successful for a positive test with valid username and password or assert proper error message is displayed for a negative test with invalid username and password.
Testing framework for automation like TestNG and jUnit for Java or nUnit for C# provides feature of assertion to assert/validate the application is working properly and scores 10/10 in the test results.
In this article, we will discuss how to add assertions using TestNG framework for selenium tests.
For example in a selenium test for login functionality, When we provide valid username and password and login into application, we expect the title to be displayed correctly in the page. So in this scenario, we should add an assertion for comparing the expected title of the page with the actual title displayed in the page. In case, the two values does not match, the assertion fails and test execution fails
For e.g.: Let us consider login into an e-mail website. We need to go to application URL and assert/validate username and password input box and Login button are displayed correctly. Then next step can be to assert login is successful for a positive test with valid username and password or assert proper error message is displayed for a negative test with invalid username and password.
Testing framework for automation like TestNG and jUnit for Java or nUnit for C# provides feature of assertion to assert/validate the application is working properly and scores 10/10 in the test results.
In this article, we will discuss how to add assertions using TestNG framework for selenium tests.
Problem statements of this article:
- What is meant by Assertions in TestNG?
- What are different types of assertion in TestNG?
- What is the difference between assert and softassertions?
- Code explaining how to implement assert and softassert in TestNG?
What is meant by Assertions?
With TestNG framework, We can add assertions within tests to validate whether the expected condition is true or false. In most of the assertions, the expected condition is compared with the actual results and test fails in case an assertion fails. The failure is reported in test results.For example in a selenium test for login functionality, When we provide valid username and password and login into application, we expect the title to be displayed correctly in the page. So in this scenario, we should add an assertion for comparing the expected title of the page with the actual title displayed in the page. In case, the two values does not match, the assertion fails and test execution fails
What are different types of assertions?
Some of the assertions that can be implemented in TestNG are as follows:
Types of assertion
|
Description
|
Assert.AssertEquals(Expected condition, Actual condition)
|
This compares the expected condition with
actual condition and fails the test in case the assertion fails.Different
object types can be compared using Assert.AssertEquals
|
Assert.AssertEquals(Expected condition, Actual condition,
Message)
|
This compares the expected condition with
actual condition and fails the test in case the assertion fails displaying
the message as defined while calling the assertion.
|
Assert.assertFalse(Boolean condition)
|
Asserts that a condition is false
|
Assert.assertFalse(Boolean condition, String Message )
|
Asserts that a condition is false. If it
isn't, an Assertion Error, with the given message, is thrown.
|
Assert.assertTrue(Boolean condition, String Message )
|
Asserts that a condition is true. Assertion
fails if condition is not true and string message is displayed.
|
Assert.AssertNull(object)
|
Validates if a assertion is null.
|
Assert.Fail(String strMessage)
|
Fails an assertion without validating any
conditions. This is very useful for outcome of conditional statements. e.g: if title is incorrect, fail the test.
|
Sample Code to explain how to create assertion in TestNG
package testNG; import java.io.File; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class assertionVal { WebDriver driver; @BeforeTest public void setup() { File file = new File("D:\\selenium\\IEDriverServer.exe"); System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); driver = new InternetExplorerDriver(); } @Test public void assertVal() { driver.navigate().to("http://qaautomationqtp.blogspot.com"); Assert.assertEquals("Learning Automation Concepts in QTP", driver.getTitle()); //Assert to compare two values and reporting a message in case validation fails Assert.assertEquals("aaa", "aaa", "this is the first test and value does not match"); //Assert to validate a boolean condition as false Assert.assertFalse(driver.getTitle().contentEquals("Learning Automation Concepts in QTP")); //Assert to validate a boolean condition as true Assert.assertFalse(driver.getTitle().contentEquals("Learning Automation Concepts in QTP")); //Assertion to validate an object is not null. Assert.assertNotNull("driver"); } }
What is the difference between assert and soft assertions (Verify statements)?
The issue with assertion is, test execution stops in case an assertion fails. So if we do not want to stop a test on failure and continue with test execution, using assert statement will not work,
In such sccnarios, we will use soft assertions, using import org.testng.asserts.SoftAssert; class.
In this case, verification step will fail but test execution will continue.
Sample Code to explain how to implement soft assertion in TestNG
package testNG; import org.testng.Assert; import org.testng.annotations.Test; //soft assertion will use the below softAssert class import org.testng.asserts.SoftAssert; public class assertionVal { // Create an soft assertion object softas SoftAssert softas = new SoftAssert(); @Test public void assertVal() { //Create an assertion similar to hard assertion as shown below softas.assertEquals("aaaa", "bb"); softas.assertEquals("aa","aaccc","asserts exists"); // this is an example of hard assertion, test will stop execution //at this step in case error is encountered. Assert.assertEquals("aa", "aa"); System.out.println("nitin"); //This step is very important as without this step, //no soft assertion failure will be reported in test result. softas.assertAll(); } }
It was quite helpful
ReplyDeletehttps://bayanlarsitesi.com/
ReplyDeleteYenibosna
Anadolu Kavağı
İçerenköy
Yeşilköy
E43
görüntülü show
ReplyDeleteücretlishow
6QM23
https://titandijital.com.tr/
ReplyDeletesakarya parça eşya taşıma
aksaray parça eşya taşıma
urfa parça eşya taşıma
kocaeli parça eşya taşıma
JJES
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
0DD3
3CE89
ReplyDeleteSivas Lojistik
Amasya Lojistik
Van Lojistik
Yalova Lojistik
Ağrı Parça Eşya Taşıma
DCD00
ReplyDeleteYozgat Lojistik
Mersin Lojistik
Mardin Parça Eşya Taşıma
Elazığ Lojistik
Karaman Parça Eşya Taşıma
BA71D
ReplyDeleteKırıkkale Evden Eve Nakliyat
Rize Evden Eve Nakliyat
Bingöl Evden Eve Nakliyat
Bayburt Lojistik
Van Lojistik
635E8
ReplyDeleteKastamonu Evden Eve Nakliyat
Ağrı Parça Eşya Taşıma
Van Evden Eve Nakliyat
Aksaray Evden Eve Nakliyat
Bolu Evden Eve Nakliyat
745D4
ReplyDeleteEskişehir Evden Eve Nakliyat
Sinop Parça Eşya Taşıma
Manisa Parça Eşya Taşıma
Kayseri Parça Eşya Taşıma
Tokat Evden Eve Nakliyat
43DA1
ReplyDeleteİzmir Şehir İçi Nakliyat
Giresun Parça Eşya Taşıma
Hakkari Lojistik
Osmaniye Parça Eşya Taşıma
Elazığ Şehirler Arası Nakliyat
Iğdır Evden Eve Nakliyat
Altındağ Boya Ustası
Kırıkkale Evden Eve Nakliyat
Silivri Boya Ustası
50AC1
ReplyDeleteCoin Nasıl Oynanır
Gate io Borsası Güvenilir mi
Binance Komisyon Ne Kadar
Btcturk Borsası Güvenilir mi
Kripto Para Üretme Siteleri
Kripto Para Madenciliği Siteleri
Kripto Para Madenciliği Nedir
Binance Kaldıraçlı İşlem Nasıl Yapılır
Kripto Para Madenciliği Nedir
FF97F
ReplyDeletecanlı görüntülü sohbet uygulamaları
sesli sohbet mobil
kadınlarla sohbet
ardahan rastgele görüntülü sohbet
kütahya canlı görüntülü sohbet
chat sohbet
amasya canlı sohbet ücretsiz
ardahan rastgele sohbet uygulaması
kırşehir mobil sohbet
EB7EB
ReplyDeleteArdeşen
Harmancık
Türbanlı İfşa
Kuşadası
Üzümlü
Eskil
Avcılar
Adana
Edremit