SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Example Of Junit Timeout And Expected Exception Test For WebDriver

Sometimes you need to set time out for your webdriver test or you need to set expected exception condition for your test. Supposing you have written test for one module and you wants to set timeout for your test. Here timeout means allowed maximum time to complete full test. Same way, You are expecting some exception during your webdriver test execution and that exception is acceptable.If you are using junit framework for your webdriver test then you can do it very easily. We have seen example of - how to Ignore specific webdriver test using junit's @Ignore annotation in THIS POST. Now let me describe how to write timeout test and exception test in junit with examples.

Timeout Test In JUnit For WebDriver
We can specify timeout time with junit's @Test annotation as shown bellow. It will allow maximum 2000 milliseconds to complete the execution of test1() method. After 2000 miliseconds, it will skip remaining execution and test1() will be marked with error.
@Test(timeout=2000)
 public void test1(){   
 }

Expected Exception Test In JUnit For WebDriver
We can specify expected exception condition with @Test annotation as bellow. Here my expected exception is null pointer exception so my expected condition is NullPointerException.class. You can write your expected exception like IndexOutOfBoundsException.class, ArithmeticException.class, ect.
@Test(expected = NullPointerException.class)
 public void exceptiontest2() {
 }

Junit Example Of Timeout And Expected Exception Test
-Go to THIS PAGE
-Copy example given on Step 3 - Create 2nd test case and paste it in your eclipse.
-Remove all @Test methods from that example and paste bellow given @Test methods and then run your test class (junittest2.java).
@Test(timeout=2000)
 public void test1() throws InterruptedException{  
 driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test1");
 System.out.print("\njunittest2 class-test1 executed before sleep");
 Thread.sleep(5000);
 System.out.print("\njunittest2 class-test1 executed after sleep");
 }
 
 @Test
 public void exceptiontest1() {
     throw new NullPointerException();
 }
 
 @Test(expected = NullPointerException.class)
 public void exceptiontest2() {
  throw new NullPointerException();
 }

On completion of execution, console will show bellow given out put.
Console Output :
Browser open
junittest2 class-test1 executed before sleep
Browser close

JUnit test result looks like bellow.


  • test1() method is display with error because test was time out after 2000 milisecond as per our condition.
  • exceptiontest2() pass successfully because we have placed expected exception condition with it.
  • exceptiontest1() display with error because there was not any expected exception condition.

3 comments:

  1. @Test(expected = NullPointerException.class)
    public void exceptiontest2() {
    throw new NullPointerException();
    }

    Can you please explain whats the use of NUllPointerException here.
    What is it exactly doing.

    ReplyDelete
    Replies
    1. It is just example to show the exception test in junit

      Delete
  2. Hi,

    It is a very clear tutorial and I could understand more about expected exceptions. Could you add in the tutorial how to print/log the time execution for each test?

    Thanks

    ReplyDelete