Reputation: 205
I am not able to understand how to write Java code to implement Verify
. I am always seeing the code to implement Assert
but not for Verify
. I know for Assert
, we need to write the code as below:
Assert.assertTrue()
or
Assert.assertEquals() etc.
But what about Verify
? I want to verify the title of my application once the user is logged into the application by using verify
. How can I do this?
Upvotes: 2
Views: 28778
Reputation: 317
TestNG does not support verify statements, but they can be implemented easily. The simplest way is to use Java StringBuffer in your test method like below,
@Test
public void verifyTest(){
/* buffer to hold your errors */
StringBuffer errorBuffer = new StringBuffer();
/* verification 1 */
try{
Assert.assertEquals("value1", "value!");
}catch(AssertionError e){
errorBuffer.append(e.getMessage() + "\n");
}
/* verification 2 */
try{
Assert.assertEquals("value2", "value!");
}catch(AssertionError e){
errorBuffer.append(e.getMessage());
}
if(errorBuffer.length() > 0){
throw new AssertionError(errorBuffer.toString());
}
}
For more advanced implementation you can use TestNG's IInvokedMethodListener interface where you need to implement two methods from that interface,
public class TestMethodListener implements IInvokedMethodListener{
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
if(method.isTestMethod()){
/* create new error buffer object */
}
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if(method.isTestMethod()){
/* process your test result for verification errors stored in error buffer
* and modify your testResult object accordingly
*/
}
}
}
Most of the time we don't have to go for advanced implementation. Simple StringBuffer should work. But if you want to use verify statements frequently in your test then implementing IInvokedMethodListener would be reasonable. If you are interested in advanced implementation then please check this blog https://muthutechno.wordpress.com/2015/01/26/implementing-verify-statements-for-testng-framework/
Upvotes: 1
Reputation: 136
You must use the TestNG framework which only supports Assert statements. It doesn't support Verify statements. You can visit the following URL for TestNG Javadoc:
When Assertion fails all test steps after that line of code are skipped When a “verify” fails, the test will continue executing and logging the failure.
If you want to use Verify statements then you will have to use the Junit framework.
Upvotes: 1
Reputation: 1636
Even if TestNG does not support verification directly you can create soft assertions which will act like verifications. Kindly check this link: http://seleniumexamples.com/blog/guide/using-soft-assertions-in-testng/
Upvotes: 0