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.
lot of thanks for your samples, you help me so much
ReplyDeleteWhat is the use of running a JUnit teat case? How is it different from running a test case directly?
ReplyDeleteCan anyone answer this question? I have exactly the same doubt.
DeleteJUnit 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.
DeleteI am liking it
ReplyDeleteCool
ReplyDeleteSuper....You helped me a lot.. all the post are very useful for me.. :-)
ReplyDeletei like it
ReplyDeleteRealy it is very useful and easy to implement...ty
ReplyDeletereally nice ,i like a lot this one
ReplyDeletelike your tutorial, lean things within an hour. Thanks a lot!
ReplyDeleteF Wang
I followed instructions exactly and got this error:
ReplyDeleteAn internal error occurred during: "Launching Mytest".
java.lang.NullPointerException
Awesome Blogs ... Really Helpful :)
ReplyDeleteCan 1 package and class be used for running two different test cases?
ReplyDeleteActually. I am getting the error- Class file collision: A resource exists with a different case: '/testproject/bin/mytestpack/Mytestclass.class'.
Hi, I face a problem here. I do following the step by step, but there is no "run as" window appearing after click run.
ReplyDeleteand 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
org.openqa.selenium.remote.ProtocolHandshake createSession
ReplyDeleteINFOS: 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.
when I try to run the class I got this error
ReplyDelete(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
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
ReplyDeleteHello 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?