C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
AssertionsAssertion determines the state of the application whether it is the same what we are expecting or not. If the assertion fails, then the test case is failed and stops the execution. To use the Assertion in Web Driver, you need to download the Testng jar file and add it to the eclipse. Download the Testng jar file from the link given below: https://mvnrepository.com/artifact/org.testng/testng/6.7There are two types of Assertion:
Hard AssertionHard Assertion is an Assertion that throws the AssertException when the test case is failed. In the case of Hard Assertion, you can handle the error by using a catch block like a java exception. Suppose we have two test cases in a suite. The first test case in a suite has an assertion that fails, and if we want to run the second case in a suit, then we need to handle the assertion error. A Hard Assertion contains the following methods:
AssertFalse()Assertion verifies the boolean value returned by a condition. If the boolean value is false, then assertion passes the test case, and if the boolean value is true, then assertion aborts the test case by an exception. Syntax of AssertFalse() method is given below: Assert.AssertFalse(condition); Let's understand through an example:
package mypack; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("https://www.spicejet.com/"); Assert.assertFalse(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected()); System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected()); } } In the above code, Assert.assertFalse() contains the condition which is returning false value. Therefore, it passes the test case. Output on the console
package mypack; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("https://www.spicejet.com/"); Assert.assertFalse(true); System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected()); }} In the above code, Assert.assertFalse() method contains the true condition. Therefore, the assertion fails which means that the test case is also failed. Assertion failure will stop the execution by exception. Output on the console AssertTrue()Assertion verifies the boolean value returned by a condition. If the boolean value is true, then assertion passes the test case, and if the boolean value is false, then assertion aborts the test case by an exception. Syntax of AssertTrue() method is given below: Assert.AssertTrue(condition); Let's understand through an example. package mypack; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("https://www.spicejet.com/"); driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click(); Assert.assertTrue(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected()); System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected()); } } In the above code, driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click(); This statement is used to select the 'Senior Citizen' box. In the next statement, we are applying assertion to check whether the test case fails or pass. The parameter inside the Assert.assertTrue() method is returning true value, therefore the test case pass. Output Output on the Console AssertEquals()AssertEquals() is a method used to compare the actual and expected results. If both the actual and expected results are same, then the assertion pass with no exception and the test case is marked as "passed". If both the actual and expected results are not the same, then the assertion fails with an exception and the test case is marked as "failed". Syntax of an AssertEquals() method is given below: Assert.assertEquals(actual,expected); Let's understand through an example.
package mypack; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("https://www.spicejet.com/"); Assert.assertEquals("5Adult",driver.findElement(By.id("divpaxinfo")).getText()); System.out.println(driver.findElement(By.id("divpaxinfo")).getText()); }}
Output on the console AssertNotEquals()It is opposite to the function of AssertNotEquals() method. AssertNotEquals() is a method used to compare the actual and expected results. If both the actual and expected results are not the same, then the assertion pass with no exception and the test case is marked as "passed". If both the actual and expected results are same, then the assertion fails with an exception and the test case is marked as "failed". Syntax of an AssertNotEquals() method is given below: AssertNotEquals(actual,expected,message); Let's understand through an example.
package mypack; import org.junit.Assert; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub Assert.assertNotEquals("Hello", "How are you"); System.out.println("Hello...This is javaTpoint"); } } In the above code, actual string, i.e., Hello is not equal to the expected string, i.e., How are you. Therefore, the assertion passes the test case. This will execute the next statement and the next statement is System.out.println("Hello...This is javaTpoint");. Output
package mypack; import org.junit.Assert; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub Assert.assertNotEquals("Hello", "Hello"); System.out.println("Hello...This is javaTpoint"); }} Output AssertNull()AssertNull() is a method that verifies whether the object is null or not. If an object is null, then assertion passes the test case, and the test case is marked as "passed", and if an object is not null, then assertion aborts the test case and the test case is marked as "failed". Syntax of AssertNull() method is given below: Assert.assertNull(object); Let's understand through an example.
package mypack; import org.junit.Assert; public class Checkbox_test { public static void main(String[] args) { Assert.assertNull(null); System.out.println("Hello...This is javaTpoint"); }} Output
package mypack; import org.junit.Assert; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub Assert.assertNull(10); System.out.println("Hello World"); } } Output AssertNotNull()AssertNotNull() is a method that verifies whether the object is null or not. If an object is not null, then assertion passes the test case and test case is marked as "passed", and if an object is null, then assertion aborts the test case and test case is marked as "failed". Syntax of AssertNotNull() method is given below: Assert.assertNotNull(object); Let's understand through an example.
package mypack; import org.junit.Assert; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub Assert.assertNotNull(10); System.out.println("C Language"); }} Output
package mypack; import org.junit.Assert; public class Checkbox_test { public static void main(String[] args) { // TODO Auto-generated method stub Assert.assertNotNull(null); System.out.println("C Language"); } } Output SoftAssertionTill now, we have learnt about the Hard Assertion in Web Driver using Testng framework. In hard assertion, if an assertion fails then it aborts the test case otherwise it continues the execution. Sometimes we want to execute the whole script even if the assertion fails. This is not possible in Hard Assertion. To overcome this problem, we need to use a soft assertion in testng.
Next TopicSelenium Grid
|