In this section Let’s Understand what are Assertions in TestNG and why are Assertions Used.
What is Assertion in TestNG?
The end result is compared with the anticipated end result with the assistance of Assertion in TestNG, in TestNG We Can Have Multiple Testcases which is Not Easy to Identify Whether each of the Testcases outputs is As Expected. So Assertion class is Used to verify each Testcase Executed as Per the Requirement.
There are Two Types of Assertions in TestNG
- Hard Assertions
- Soft assertions
Hard Assertion
A Hard Assertion is a type of assertion that throws an exception immediately and if an assert statement fails and it continues with the next Testcase in the test suite.
Soft Assertion
A Soft Assertion is a type of assertion that does not throw an exception when an assertion fails and would continue with the next Testcase step after the assert statement.
TestCase
- Display Use of Different assertion Methods in TestNG.
Code
Assert Testcase to verify exact match(equal) value
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void OpenBrowser() { //Chrome WebDriver Setup System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver String ActualTitle = driverChrome.getTitle();//Capture Title from Page To verify String ExpectedTitle = "Google";//Pass ExpectedTitle to String varible Assert.assertEquals(ExpectedTitle, ActualTitle);//Compare ExpectedTitle and ActualTile Form Page driverChrome.quit();// this would close all the window of web driver } }
Assert.assertEquals(ExpectedTitle, ActualTitle);
is Used For Comparing the String Captured Title from Google.com to Expected Title which is passed into the variable
Output
Assert Test case to verify not equal value
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void OpenBrowser() { //Chrome WebDriver Setup System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver String ActualTitle1 = driverChrome.getTitle();//Capture Title from Page To verify String ExpectedTitle1 = "Google1";//Pass ExpectedTitle to String varible Assert.assertEquals(ActualTitle1 , ExpectedTitle1,"Title Doesn't Matched");//Compare ExpectedTitle and ActualTile Form Page and Print Exeception driverChrome.quit();// this would close all the window of web driver } }
Assert.assertEquals(ActualTitle1 , ExpectedTitle1,"Title Doesn't Matched");
is Used For Comparing the String Captured Title from Google.com to Expected Title which is passed into the variable, if The String Don’t match with the Expected Title it will Print Exception Message which we have define in it.
Output
Verify string contains, a sub-string in Selenium Java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void OpenBrowser() { //Chrome WebDriver Setup System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver String ActualTitle = driverChrome.getTitle();//Capture Title from Page To verify Assert.assertTrue(ActualTitle.contains("gle"));//Match Sub-String Pattern using contains System.out.println("The Given Sub-String is Found In Title\n");//this Print Statement will only execute after the Success Sub-string case driverChrome.quit();// this would close all the window of web driver } }
Assert.assertTrue(ActualTitle.contains("gle"));
is Used to Search the SubString Captured From the Captured Title From Google.com.
Output
Verify string does not contain, a sub-string in Selenium Java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void OpenBrowser() { //Chrome WebDriver Setup System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver String ActualTitle = driverChrome.getTitle();//Capture Title from Page To verify Assert.assertEquals(ActualTitle.contains("hi"), true, "The Given Sub-String is Not Found In Title");//Match Sub-String Pattern using contains and if its not true print the given Statement driverChrome.quit();// this would close all the window of web driver } }
Assert.assertEquals(ActualTitle.contains("hi"), true, "The Given Sub-String is Not Found In Title");
is Used to Search the SubString Captured From the Captured Title From Google.com,if that String Don’t Match ,it will print Exception Error which we have Define.
Output
Assert to verify less than and greater than values
import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void CompareValue() { int value1 = 10; int value2 = 12; Assert.assertTrue (value1 < value2); Assert.assertTrue (value1 > value2,"Value1 is Smaller than Value2"); } }
Assert.assertTrue (value1 < value2);
it verifies the Boolean value returned by a condition if condition is True it continue onto next step
Assert.assertTrue (value1 > value2,"Value1 is Smaller than Value2");
it verifies the Boolean value returned by a condition if condition is False it will print Exception Message.
Output
Assert to verify matched regular expression
import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void RegularExpression() { String ActualExpression ="[a-zA-Z0-9]{6}-"; String foundExpression ="[a-zA-Z0-9]{6}-"; Assert.assertSame(ActualExpression,foundExpression); } }
Assert.assertSame(ActualExpression,foundExpression);
it check the both Regular Expression Provided and if both the Expression are not Same it Throws an Exception Error.
Output
Assert to verify not matched regular expression
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void RegularExpressionNotMatch() { String ActualExpression ="[a-zA-Z0-9]{6}-"; String foundExpression ="[a-zA-Z0-9]{6"; Assert.assertSame(ActualExpression,foundExpression,"Regular Expression Does not Match"); } }
Assert.assertSame(ActualExpression,foundExpression,"Regular Expression Does not Match");
it check both the Regular Expression Provided and if both the Expression are not Same it Throws an Exception Error and Exception will be Printed.
Output
Verify Element is exist or not in Selenium Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Assertion { @Test public void OpenBrowser() { //Chrome WebDriver Setup System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver WebElement Search = driverChrome.findElement(By.name("q")); Assert.assertEquals(true,Search.isDisplayed()); driverChrome.quit();// this would close all the window of web driver } }
Assert.assertEquals(true,Search.isDisplayed());
is Used to check Whether the Element is Displayed on Web Page or Not
Output
We Successfully Demonstrated Assertion in TestNG.