Tuesday, 15 March 2016

Difference between assert & verify in selenium webriver?

1. Difference between assert & verify in selenium webriver? 
When an “assert” fails, the test will be aborted.
Where if a “verify” fails, the test will continue executing and logging the failure.
So when Assertion fails all test steps after that line of code are skipped :(
To resolve this we write this command in try catch block :)
Example 1 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package testWordpress;
 
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class AssertionsTest {
 
 @Test
 public void testMultiply() {
 System.out.println("Before Error ");
 Assert.assertEquals(21, multiply(10, 5));
 System.out.println("After Error ");
 }
 
 public int multiply(int x, int y) {
 return x / y;
 }
 
}
System.out.println(“After Error “); will never executed. :(
Please check eclipse console to conform.
We can resolve this problem by using Try-Catch block.
How?
Lets check out following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package testWordpress;
 
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class AssertionsTest {
 
 @Test
 public void testMultiply() {
 System.out.println("Before Error ");
 
 try{
 Assert.assertEquals(21, multiply(10, 5));
 }catch(Throwable t){
 // recovered
 // java code to fail the test
 System.out.println("After Error ");
 }
 
 }
 
 public int multiply(int x, int y) {
 return x / y;
 }
}