Tuesday, 1 July 2014

How To Create And Run JUnit Test Suit For WebDriver Test - Step By Step

If you are planning to perform regression testing of any application using webdriver then obviously there will be multiple test cases or test classes under your webdriver project. Example - There are 2 junit test cases under your project's package. Now if you wants to run both of them then how will you do it? Simple and 
easy solution is creating JUnit test suite. If your project has more than 2 test cases then you can create test suite for all those test cases to run all test cases from one place.
Step 1 - Create new project and package
Create new project in eclipse with name = junitproject and then add new package =
   com.practice.testsuite under your project. 
Step 2 - Create 1st Test Case
Now create JUnit test case under com.practice.testsuite package with class name =
   Junit1 as bellow.
package com.practice.testsuite;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class junittest1 { WebDriver driver = new FirefoxDriver();
public class Junit1 {
 WebDriver driver;
  @Test
  public void f() {
  driver = new FirefoxDriver(); 
  driver.get("https://www.facebook.com/");
  driver.manage().window().maximize();
  driver.quit();
  }
}
Step 3 - Create 2nd test case
Same way, Create 2nd test class with name = Junit2 under package = junitpack as bellow.
package com.practice.testsuite;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class junittest1 { WebDriver driver = new FirefoxDriver();
public class Junit2 {
 WebDriver driver;
   @Test
  public void f() {
    driver = new FirefoxDriver(); 
   driver.get("https://www.google.com/");
   driver.manage().window().maximize();
   driver.quit();
  }
  @Test
  public void f() {
  driver = new FirefoxDriver(); 
  driver.get("https://www.facebook.com/");
  driver.manage().window().maximize();
  driver.quit();
  }
}

Step 4 - Create test suite for both test cases
Now we have 2 test cases(Junit1.java and jJunit2.java) under package = com.practice.testsuite.
To create test suite, Right click on com.practice.testsuite package folder and Go to -> New -> Other -> Java -> Junit ->  Select 'JUnit Test Suite'
package com.practice.testsuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses

@RunWith(Suite.class)
@SuiteClasses({Junit1.class,Junit2.class})
public class JunitSuite {

}

Step 5- Running test suite
 When you run JunitSuite test suite, eclipse will run both test cases (Junit1 and Junit2) one by one. When execution completed, you will see  output lines in console.



No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...