Creating and running webdriver test with junit and eclipse step by step

We have already discussed about how to download and install junit in eclipse to run webdriver automation software test in my previous post. Please note that you need to add junit jar file only if it is not available in webdriver software testing tool's jar file folder. If it is already there with webdriver jar file folder then you not need to do anything to create and run junit test. Now let me describe you how to create webdriver test case using junit.

Webdriver automation software test case creation using junit and eclipse
You are already aware about how to create new project and package in eclipse and if you are not aware then please visit this post page. To create JUnit Test Case, you need to add JUnit Test Case at place of Class inside your package. Look at bellow given image to know how to add JUnit Test Case.


As shown in above image, Select JUnit Test Case from Select a wizard dialog and then click on Next button. On Next page, Enter your software  test case name and click on Finish button.


When you add JUnit Test Case, your eclipse project explorer window will looks like bellow given image.


Now replace bellow given code with your original code.

package Testjunit;

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

public class Mytest {

 WebDriver driver = null;

 @Test
 public void test() {
  // set geckodriver path.
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  //initialize firefox driver.
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  System.out.print("Window maximise");
  driver.get("http://only-testing-blog.blogspot.com/");
  System.out.print("Site Open");
  driver.quit();
  System.out.print("End of Test");
 }
}

Running webdriver test case using junit in eclipse
After replacing above code with your original code, click on Run button in eclipse. It will show you Run As dialog as shown bellow.

Video describing Running webdriver test using JUnit

Select JUnit Test from Run As dialog and click on Ok button. Eclipse will run your software test case using JUnit and on completion of execution, you can see your result in JUnit pane as bellow.


VIEW THIS POST to know how to create and run junit test suite in eclipse.

18 comments:

  1. lot of thanks for your samples, you help me so much

    ReplyDelete
  2. What is the use of running a JUnit teat case? How is it different from running a test case directly?

    ReplyDelete
    Replies
    1. Can anyone answer this question? I have exactly the same doubt.

      Delete
    2. JUnit will help you on creating more than one test case in the same class. Besides, you may also use different URL for each test case.

      Delete
  3. Super....You helped me a lot.. all the post are very useful for me.. :-)

    ReplyDelete
  4. Realy it is very useful and easy to implement...ty

    ReplyDelete
  5. really nice ,i like a lot this one

    ReplyDelete
  6. like your tutorial, lean things within an hour. Thanks a lot!

    F Wang

    ReplyDelete
  7. I followed instructions exactly and got this error:
    An internal error occurred during: "Launching Mytest".
    java.lang.NullPointerException

    ReplyDelete
  8. Awesome Blogs ... Really Helpful :)

    ReplyDelete
  9. Can 1 package and class be used for running two different test cases?
    Actually. I am getting the error- Class file collision: A resource exists with a different case: '/testproject/bin/mytestpack/Mytestclass.class'.

    ReplyDelete
  10. Hi, I face a problem here. I do following the step by step, but there is no "run as" window appearing after click run.
    and there are lot "
    WARNING: Process refused to die after 10 seconds, and couldn't taskkill it

    would you kind to tell me, what I have done wrong?

    Mak

    ReplyDelete
  11. org.openqa.selenium.remote.ProtocolHandshake createSession
    INFOS: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
    org.openqa.selenium.remote.ProtocolHandshake createSession
    INFOS: Falling back to straight W3C remote end connection
    org.openqa.selenium.remote.ProtocolHandshake createSession
    INFOS: Falling back to original OSS JSON Wire Protocol.

    ReplyDelete
  12. when I try to run the class I got this error
    (the path to the driver executable must be set by the webdriver.gecko.driver system property)
    any help please, how to set property of gecko.driver with Junit class

    ReplyDelete
  13. java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases

    Hello All,
    When i run the above test case i get the above error.
    Can somebody help me.Please.Is the above given Script is correct?

    ReplyDelete