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

If you are planning to perform regression testing of any software web application using webdriver software testing then obviously there will be multiple test cases or test classes under your webdriver project. Example - There are 2 junit test cases of software web application 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 of software web application then you can create test suite for all those test cases to run all test cases from one place.

Before creating junit test suite, you must have to read my post about "How to download and install junit in eclipse" and "How to create and run junit test in eclipse". Now let me describe you how to create junit test suite in eclipse for your junit test case of software web application.

Step 1 - Create new project and package
Create new project in eclipse with name = junitproject and then add new package = junitpack under your project. VIEW THIS POST to know how to create new project and add package in eclipse. Add required external jar files for selenium webdriver.

Step 2 - Create 1st Test Case
Now create JUnit test case under junitpack package with class name = junittest1 as bellow. VIEW THIS post to know how to create junit test case in eclipse.
package junitpack;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class junittest1 {
 
 WebDriver driver = null;

 @Test
 public void test() throws InterruptedException {
  // set geckodriver path.
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  //initialize firefox driver.
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html");
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest1 executed");
  Thread.sleep(2000);
  System.out.print("junittest1 class is executed");
  driver.quit();
 }
}

Step 3 - Create 2nd test case
Same way, Create 2nd test class with name = junittest2 under package = junitpack as bellow.
package junitpack;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class junittest2 {
 
 WebDriver driver = null;
 
@Before
public void setup () {
 // set geckodriver path.
 System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
 //initialize firefox driver.
 driver = new FirefoxDriver();
 driver.manage().window().maximize();
 driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html");
}

@After
public void aftertest() {
 driver.quit(); 
}

 @Test
 public void test1() throws InterruptedException{  
 driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test1");
 System.out.print("\njunittest2 class-test1 method is executed");
 Thread.sleep(2000);
 }
 
 @Test
 public void test2() throws InterruptedException {
 driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test2");
 Thread.sleep(2000);
 System.out.print("\njunittest2 class-test2 method is executed");
 }
}

Step 4 - Create test suite for both test cases
Now we have 2 test cases(junittest1.java and junittest2.java) under package = junitpack.
To create test suite, Right click on junitpack package folder and Go to -> New -> Other -> Java -> Junit ->  Select 'JUnit Test Suite' as shown in bellow image.



Now click on Next button.On next screen, add junit test suite name = junittestsuite and select both test cases as shown bellow image and then click on Finish button.


It will add new test suite class = junittestsuite.java under your package as bellow.
package junitpack;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ junittest1.class, junittest2.class })
public class junittestsuite {

}

If you see in above suite class, there are 2 junit annotations added with name = @RunWith and @SuiteClasses. @RunWith annotation will references junit to run test in class and @SuiteClasses describes classes included in that test suite.

Video on how to create JUnit test suite for selenium webdriver

Now your junit test suite is created and project structure will looks like bellow.


Step 4 - Running test suite
Select junit test suite class = junittestsuite and run it in eclipse. When you run junit test suite, eclipse will run both test cases (junittest1 and junittest2) one by one. When execution completed, you will see bellow given output lines in console.

junittest1 class is executed
junittest2 class-test1 method is executed
junittest2 class-test2 method is executed

junit test execution report will looks like bellow.


This way we can create junit test suite to run multiple test cases from one place. You can view all Junit tutorial posts for webdriver on THIS LINK.

VIEW MY NEXT POST to know how to ignore test from execution.

21 comments:

  1. Wonderful tutorial, thanks
    But I would like to ask, Can we organize the testsuite or testcases so that the browser will be opened onlly once, then we will execute some testcases, then shut down the browser.
    In your case, the browser is started and shut down in every test case.

    ReplyDelete
    Replies
    1. Yes you can do it. use @BeforeClass at place of @Before and @AfterClass at place of @After in above example. Example at - > http://software-testing-tutorials-automation.blogspot.in/2014/03/example-of-difference-between.html

      Delete
  2. Thanks for the Tutorials
    I have one doubt, I have created test cases, i can execute them, but when I try to create a test suite, I cannot see the two test cases, can you please help sir?

    ReplyDelete
    Replies
    1. During test suite creation, you have to right click on package.

      Delete
    2. Even I can see only 1 test case under it. I did the same as above: right clicked on package.

      Delete
  3. Thanks for the Tutorials.
    The one problem I faced with, was that my test-cases were not available for addition to test-suite even though I had right clicked on package. They became available only when I extended TestCase (in this case it would run as JUnit3), but I wanted to use Junit4. Still don't know how to create from the Wizard..
    The other problem was that usage of @BeforeClass and @AfterClass still led to starting of the browser each time per each Test-Case in Test-Suite (though @BeforeSuite and @AfterSuite notations are available only for TestNG not JUnit4). I've checked it locally and also remotely on BrowserStack machines and got the same problem. Maybe you could suggest another way of solving this problem.
    Any answer will be appreciated and hope helpful :)

    ReplyDelete
    Replies
    1. Please post your email id here. It will be not published. I will communicate with you by email to provide you solution.

      Delete
  4. Thank you so much for such a helpful tutorial material.
    This helped me a lot in understanding WebDriver, JUnit/TestNG and Selenium.
    Keep it rocking!

    ReplyDelete
  5. First I would like to thank you for this great tutorial... It's explained in detail step by step.
    My question is that : Is it really necessary to create a suite in junit ? Because, in my project I have grouped the test cases in packages. So if I want to run a particular package, then I can simply right click on that package and run the whole test cases inside it. This is similar to suite.

    So, I would like to know what is the benefit in creating a junit suite?

    ReplyDelete
  6. While Creating Testsuite, i right clicked and created suite in the package, But i am unable to see the tests.

    Also when i try to copy paste, i get error :Class cannot be resolved to a type

    can you please help..

    ReplyDelete
  7. Hi Aravind,
    Thanks a lot for this quick and detailed tutorial.The post is simply superb and very easy to understand.Thank you so much.Keep posting new articles and anything related to performance testing....

    ReplyDelete
  8. Thank you Aravind, excellent blog.

    ReplyDelete
  9. Very nice posts....nice explaination

    ReplyDelete
  10. Thanks , but what is the difference between the normal class and the Junit test cases ? I mean why we can't create and add a normal class directly to the test suite?

    ReplyDelete
  11. Hi.. Can we add and run Junit functions on two different packages under same Junit class i.e., "junittestsuite.java"

    ReplyDelete
  12. Hi.. Can we add and run Junit test cases on two different packages under same Junit class i.e., "junittestsuite.java" ? Waiting for reply.

    ReplyDelete
    Replies
    1. Not tested yet.. please check and post here if it works for you.

      Delete
  13. Thanks for your answer.
    Now I have another question: suppose you need 2 test suites.
    The first should contain (I am using your examples):
    junittest1
    junittest2.test1

    and the other
    junittest1
    junittest2.test2

    Is that possible?

    ReplyDelete
  14. Hi, thankyou for you very informative blog,
    Can we create a junit test suit taking classes from the different packages?

    ReplyDelete
  15. I have 3 errors, the test is not passed, what happened?

    ReplyDelete
  16. Thanks a lot for this quick and detailed tutorial.The post is simply superb and very easy to understand.

    ReplyDelete