There are many assertions available in Selenium WebDriver software testing tool with TestNG framework and we will look at all of then one by one. Assertions are important and very useful in any software automation tools to assert something during your test execution. If you know, Selenium IDE software testing tool has many built in assertion commands and if you are selenium IDE user then you can view set of selenium IDE assertion command examples on THIS LINK.
Difference between Assertion and verification
Verification and assertequals selenium java is little different in software testing so don't be confused. Verification will just verify but assertion will first verify and if result is not as per expectation then it will stop execution of that specific test method. Today let we look at Assertequals testng assertion in selenium webdriver.
Assert.assertEquals(actual, expected) assertion
Assertequals testng assertion is useful to compare expected and actual values in selenium webdriver. Assertequals in selenium will check If both values match then its fine and will continue execution. But if fails then immediately assertequals selenium java will mark that specific test method as fail and exit from that test method.
You can use different types of values in actual and expected like Boolean, byte[], char, double, float, int, etc.. but function is same for all. Let we look at simple example to understand assertequals testng assertion better.
Steps :
- First of all, create 3 classes (BaseClassOne.java and ClassTwo.java) as describe in THIS POST if you don't have created.
- Now copy paste bellow given lines in your ClassTwo.java file.
Assertion and Verification Example Of Selenium WebDriver With TestNg
package TestNGOnePack;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ClassTwo extends TestNGOnePack.BaseClassOne{
String Actualtext;
@BeforeClass
public void load_url(){
driver.navigate().to("http://only-testing-blog.blogspot.com/2014/01/textbox.html");
}
//Method Example For Assertion
@Test
public void assertion_method_1() {
Actualtext = driver.findElement(By.xpath("//h2/span")).getText();
Assert.assertEquals(Actualtext, "Tuesday, 28 January 2014");
System.out.print("\n assertion_method_1() -> Part executed");
}
//Method Example For Assertion
@Test
public void assertion_method_2() {
Assert.assertEquals(Actualtext, "Tuesday, 29 January 2014");
System.out.print("\n assertion_method_2() -> Part executed");
}
//Method Example For Verification
@Test
public void verification_method() {
String time = driver.findElement(By.xpath("//div[@id='timeLeft']")).getText();
if (time == "Tuesday, 28 January 2014")
{
System.out.print("\nText Match");
}
else
{
System.out.print("\nText does Match");
}
}
}
testng.xml example to run single class
To Run above example using testng.xml file, you will have to configure it as bellow.
<suite name="Suite One" configfailurepolicy="continue">
<test name="Test One">
<classes>
<class name="TestNGOnePack.ClassTwo" />
</classes>
</test>
</suite>
If you run above example, assertion_method_1() will display pass in result but assertion_method_2() will display fail because here expected and actual values will not match with each other.
If you will look in console, Only "assertion_method_1() -> Part executed" will be printed over there. "assertion_method_2() -> Part executed" will be not there because execution of current method was skipped after assertion failure. Last verification_method() is just simple verification method which verifies text and print message based on result.
Thank you for creating this very useful resource.
ReplyDeleteQuick question:
Why in the code are used this comparing statement
if (time == "Tuesday, 28 January 2014") ? May be better to compare two values to use following - if (time.equals("Tuesday, 28 January 2014")) ?
Thank you
While using if (time == "Tuesday, 28 January 2014") shows some error better use (time.equals("Tuesday, 28 January 2014"))
ReplyDeleteis it OK to get exception as displaying in the image? or is there any work around to not get exceptions but method should mark as 'fail'? As i'm using this method "assertEqual(String actual,String expected, String message)", so it will through the error message string as given in the method.
ReplyDeleteI executed the following code:
ReplyDeleteAssert.assertEquals("ACCEPTED (0)", btnText1, "No task assigned to Accepted Category.");
But the string "No task assigned to Accepted Category." is not getting printed. Please suggest the workarounds if any.
Thanks
thanks was able to implement this successfully. Great to see you have created a post on this small thing
ReplyDelete