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.
Example Of assertNotEquals In Selenium WebDriver With TestNG
As described in my PREVIOUS POST, assertEquals assertion is useful to compare two string, boolean, byte[], char, double, float, int, etc.. and based on assertion result, Your test method will pass or fail. This pass or fail indication will helps you in your testing process. So this is the very good function in selenium
WebDriver which helps us to find and log issue. Please note one thing once more is - Use hard assertions in selenium webdriver only when you wants to skip remaining test
execution on assertion fail. If you wants to continue execution on assertion failure, you can use TestNg soft assertions. We will discuss about it in my upcoming post.
You can VIEW SELENIUM IDE ASSERTION EXAMPLES for your reference.
Assert.assertNotEquals(actual, expected, message) assertion
Other one assertion in selenium WebDriver is assertNotEquals assertion. It's function is opposite to assertEquals assertion. Means if both sides values will not match then this assertion will pass else it will fail. Here you can write your own message for failure condition. Simplest example of Assert.assertNotEquals in selenium webdriver is as bellow.
Replace bellow given test methods (assertion_method_1() and assertion_method_2()) with example given in my PREVIOUS POST and then run it using testng.xml file.
//Assertion Method @Test
public void assertion_method_1() {
Actualtext = driver.findElement(By.xpath("//h2/span")).getText();
Assert.assertNotEquals(Actualtext, "Tuesday, 28 January 2014", "Expected and actual match in assertion_method_1");
System.out.print("\n assertion_method_1() -> Part executed");
}
//Assertion Method @Test
public void assertion_method_2() {
Assert.assertNotEquals(Actualtext, "Tuesday, 29 January 2014", "Expected and actual match in assertion_method_2");
System.out.print("\n assertion_method_2() -> Part executed");
}
You test execution result will looks like bellow..
Here, test method assertion_method_1() will fail because actual string text and expected string text will match. Assertion message will be printed in test result report as marked with green color in above image.
Selenium WebDriver assertTrue assertion example with TestNG
Previously we have learnt two assertions of selenium webdriver. You can view practical example pages of selenium webdriver assertions - assertEquals and assertNotEquals before learning assertTrue(condition) assertion. There is also one another related assertion is assertFalse(condition) assertion but we will look
about it in my next post.
assertTrue(condition) Assertion
assertTrue assertion is generally used for boolean condition true. It will pass if condition returns "true". If it will return false then it will fail and skip test execution from that specific method. Syntax for assertTrue assertion is as bellow.
Assert.assertTrue(condition);
Supposing you have one check box on page. You wants to check its status like it is checked or not. And if it is not checked then you wants to skip execution from that method and wants to fail that method in testng report. This thing can be done very easily using assertTrue assertion. Let we look at practical example of assertTrue assertion.
Replace bellow given variables and 3 methods with example given on my THIS POST and then run it using testng.xml file.
WebElement chk1, chk2;
@BeforeClass
public void load_url(){
driver.get("http://only-testing-blog.blogspot.in/2014/02/attributes.html");
chk1 = driver.findElement(By.xpath("//input[@name='option1']"));
chk2 = driver.findElement(By.xpath("//input[@name='option2']"));
}
//Assertion Method - will pass @Test
public void asserttrue1() {
System.out.print("\n"+chk1.isSelected());
Assert.assertTrue(chk1.isSelected());
System.out.print("\n asserttrue1 - > Executed - means assertion is pass");
}
//Assertion Method - will fail @Test
public void asserttrue2() {
System.out.print("\n"+chk2.isSelected());
Assert.assertTrue(chk2.isSelected());
System.out.print("\n asserttrue2 - > Executed - means assertion is pass");
}
When you run above example in eclipse and get result, asserttrue1() method will display pass and method asserttrue2() will display fail as shown in bellow given image.
asserttrue1() will pass because 1st check box is checked on page so chk1.isSelected() will return true.
asserttrue2() will fail because 2nd check box is not checked on page so chk2.isSelected() will return false. In assertion failure case, code written after Assert.assertTrue(chk2.isSelected()); will be not executed. Run above example in your eclipse and verify the results then try it for your own project.
This way, Assert.assertTrue(condition) is helps use to assert boolean condition true.
Example Of Assert.assertFalse Assertion In Selenium WebDriver With TestNG
When you are working with selenium webdriver, you must be aware about different kind of assertions which are available. If you have a enough knowledge about webdriver assertions, You can create very effective test cases for your test scenarios. assertEquals, assertNotEquals and assertTrue assertions are already
explained in my previous posts. Now let we try to understand assertFalse assertion for selenium webdriver.
Assert.assertFalse(condition) Assertion
It will check boolean value returned by condition and will pass if returned value is "False". If returned value is pass then this assertion will fail and skip execution from current test method. Syntax for assertFalse assertion is as bellow.
Assert.assertFalse(condition);
In sort, assertFalse and assertTrue assertions are opposite to each other. When you wants to assert "True" value - you can use assertTrue assertion. And when you wants to assert false value - you can use assertFalse assertion in your selenium webdriver test.
Let we try to use assertFalse assertion with simple example. Supposing we have two checkboxs on page one from them is checked and another one is not checked. Let we apply assertFalse assertion on both of these check boxes for its checked status.
Replace bellow given example methods with example of THIS PAGE. and run it with testng.xml as described on that post.
WebElement chk1, chk2;
@BeforeClass
public void load_url(){
driver.get("http://only-testing-blog.blogspot.in/2014/02/attributes.html");
chk1 = driver.findElement(By.xpath("//input[@name='option1']"));
chk2 = driver.findElement(By.xpath("//input[@name='option2']"));
}
//Assertion Method - will Fail @Test
public void assertfalse1() {
System.out.print("\n"+chk1.isSelected());
Assert.assertFalse(chk1.isSelected());
}
//Assertion Method - will Pass @Test
public void assertfalse2() {
System.out.print("\n"+chk1.isSelected());
Assert.assertFalse(chk2.isSelected());
}
In this case, assertfalse1() method will fail because it is already checked so chk1.isSelected() will return true value. assertfalse2() method will pass because 2nd checkbox is not checked so it will return false value. Execution result will looks like bellow.
Selenium WebDriver Assertion assertNull Example With TestNG
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 command,
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
driver.get("http://only-testing-blog.blogspot.in/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.
TestNG Assertion assertNotNull With WebDriver Example
As you know, there are many assertions in TestNG and you will find most of them on THIS PAGE. Each of these TestNG assertions are designed to fulfill different kind of conditions. I have described mostly used all assertions with very clear examples with selenium webdriver test. If you will see on that link, you will find
example of assertNull assertion. assertNotNull assertion works opposite to assertNull assertion means it will assert not null condition.
TestNG Assertion assertNotNull(object) WebDriver Assertion
assertNotNull assertion is designed to check and verify that values returned by object is not null. Means it will be pass if returned value is not null. Else it will fail.
Best example to experiment assertNotNull assertion is assertion of enabled and disabled text fields. Let me give you simple example where we will assert "disabled" attribute of text box to verify is it disabled or not using assertNotNull assertion.
Copy paste bellow given code in example given on THIS PAGE and then run it using testng.xml file.
WebElement txt1, txt2;
@BeforeClass
public void load_url(){
driver.get("http://only-testing-blog.blogspot.in/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 Fail @Test
public void notnull1() {
System.out.print("\n"+txt1.getAttribute("disabled"));
Assert.assertNotNull(txt1.getAttribute("disabled"));
}
//Example Of Assertion Method - will Pass @Test
public void notnull2() {
System.out.print("\n"+txt2.getAttribute("disabled"));
Assert.assertNotNull(txt2.getAttribute("disabled"));
}
In above example, assertNotNull assertion of notnull1() method will fail because expected was not null but textbox text1 is not disabled so returned null. assertNotNull assertion of notnull2() will pass because expected was not null and textbox text2 is disabled so it has return true value means not null.
Sameway, You can use assertNotNull assertion to assert checkbox is checked or not, to assert textbox is visible or hidden, etc..