Example Of Junit Timeout And Expected Exception Test For WebDriver

Sometimes you lot require to railroad train fourth dimension out for your webdriver test or you lot require to set expected exception status for your test. Supposing you lot receive got written bear witness for 1 module together with you lot wants to set timeout for your test. Here timeout way allowed maximum fourth dimension to consummate total test. Same way, You are expecting some exception during your webdriver bear witness execution together with that exception is acceptable.If you lot are using junit framework for your webdriver bear witness hence you lot tin create it really easily. We receive got seen instance of - how to Ignore specific webdriver bear witness using junit's @Ignore notation inwards THIS POST. Now permit me depict how to write timeout bear witness together with exception bear witness inwards junit amongst examples.

Timeout Test In JUnit For WebDriver
We tin specify timeout fourth dimension amongst junit's @Test notation every bit shown bellow. It volition allow maximum 2000 milliseconds to consummate the execution of test1() method. After 2000 miliseconds, it volition skip remaining execution together with test1() volition travel marked amongst error.
@Test(timeout=2000)  populace void test1(){     }

Expected Exception Test In JUnit For WebDriver
We tin specify expected exception status amongst @Test notation every bit bellow. Here my expected exception is naught pointer exception hence my expected status is NullPointerException.class. You tin write your expected exception like IndexOutOfBoundsException.class, ArithmeticException.class, ect.
@Test(expected = NullPointerException.class)  populace void exceptiontest2() {  }

Junit Example Of Timeout And Expected Exception Test
-Go to THIS PAGE
-Copy instance given on Step iii - Create 2nd bear witness case together with glue it inwards your eclipse.
-Remove all @Test methods from that instance together with glue bellow given @Test methods together with hence run your bear witness degree (junittest2.java).
@Test(timeout=2000)  populace void test1() throws InterruptedException{    driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test1");  System.out.print("\njunittest2 class-test1 executed earlier sleep");  Thread.sleep(5000);  System.out.print("\njunittest2 class-test1 executed later on sleep");  }    @Test  populace void exceptiontest1() {      throw novel NullPointerException();  }    @Test(expected = NullPointerException.class)  populace void exceptiontest2() {   throw novel NullPointerException();  }

On completion of execution, console volition exhibit bellow given out put.
Console Output :
Browser open
junittest2 class-test1 executed earlier sleep
Browser close

JUnit bear witness effect looks similar bellow.


  • test1() method is display amongst mistake because bear witness was fourth dimension out later on 2000 milisecond every bit per our condition.
  • exceptiontest2() top successfully because nosotros receive got placed expected exception status amongst it.
  • exceptiontest1() display amongst mistake because in that location was non any expected exception condition.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post