Assertions are very useful to check your expected result and skip execution if assertion fails on run time. If you are selenium IDE user, you can read all these selenium IDE ASSERTION COMMANDS examples posts to use them in your test cases for your web application. Before describing about assertNull assertion of testng, I recommend you all to READ OTHER WEBDRIVER ASSERTION METHOD examples for your reference.
assertNull(object) WebDriver Assertion
assertNull(object) assertion will check and verify that object is null. It will pass if object found null. If object found not null then it will return error message like "java.lang.AssertionError: expected [null] but found [true]". Whenever your assertion fails, it will mark that specific test method as fail in TestNG result.
Let me present here one simple example of assertNull assertion. We have two text boxes. 1st text box is enabled and 2nd is disabled. Now let me use assertNull assertion in my test script to assert that textbox1 is enabled and textbox2 is disabled. Here we use textbox's disabled attribute for this assertion.
Copy and replace bellow given test methods with example given on THIS PAGE and then run it with testng.xml file
WebElement txt1, txt2;
@BeforeClass
public void load_url(){
driver.get("http://only-testing-blog.blogspot.com/2014/02/attributes.html");
txt1 = driver.findElement(By.xpath("//input[@id='text1']"));
txt2 = driver.findElement(By.xpath("//input[@id='text2']"));
}
//Example Of Assertion Method - will Pass
@Test
public void null1() {
System.out.print("\n"+txt1.getAttribute("disabled"));
Assert.assertNull(txt1.getAttribute("disabled"));
}
//Example Assertion Method - will Fail
@Test
public void null2() {
System.out.print("\n"+txt2.getAttribute("disabled"));
Assert.assertNull(txt2.getAttribute("disabled"));
}
In above given example, txt1.getAttribute("disabled") object will return "null" because there is not any disabled attribute with 1st textbox. So that assertion will pass. And method null1() will pass too. txt2.getAttribute("disabled") object will return "true" because disabled attribute is available with textbox2. So in this case that assertion and method null2() will fail as shown in bellow given image.
No comments:
Post a Comment