Академический Документы
Профессиональный Документы
Культура Документы
If this kind of junit jar file is already there then you do not need to install junit
jar with eclipse and you are ready to use junit with eclipse. But if junit jar file
is not there then you need to add it.
Downloading junit jar file
Go to http://junit.org/ website -> Click on download and install guide link will
redirect the page at junit jar file download and install page. Here you will find
the link like "junit.jar". Click on it will redirect to junit jar file downloading
page. From this page download the latest version of junit jar file.
Installing junit jar file with eclipse
Go to java build path window as shown in above figure. Now click on Add
External JARs button to add your downloaded junit jar file with eclipse. After
adding junit jar file, click on OK button to close java build path window.
Now you are ready to use junit with eclipse for your webdriver test of
software application. Click here to view all articles on webdriver.
--5.1 Creating and running webdriver test with junit
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 test in my previous post. Please note that you need to add
junit jar file only if it is not available in webdriver 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 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
anf then click on Next button. On Next page, Enter your 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.
Select JUnit Test from Run As dialog and click on Ok button. Eclipse will run
your 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.
--5.2 Creating and running junit test suite with webdriver
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.
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.
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
import
import
import
org.junit.Test;
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.firefox.FirefoxDriver;
import
import
import
import
import
import
import
org.junit.After;
org.junit.Before;
org.junit.Test;
org.junit.Ignore;
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.firefox.FirefoxDriver;
-> 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.
Now your junit test suite is created and project structure will looks like
bellow.
is
is
executed
executed
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.
--6.1 Using JUnit Annotations in webdriver
How to Use JUnit Annotations in webdriver test case with example
Unit testing framework JUnit has many annotations to control the flow and
activity of code execution. You must need to insert JUnit annotation inside
the java code to execute your test case as junit test case. You can look in my
previous post where i have used JUnit @Test annotation before test method.
Let
me
describe
you mostly used 3 JUnit Annotations with example. You can view more details
on JUnit at http://junit.org/.
1. @Before
2. @Test
3. @After
2 other frequently used annotations are @BeforeClass and @AfterClass.
Depending on annotation names, JUnit framework will decide the code
execution flow. i.e. First JUnit framework will execute @Before method,
Second it will execute @Test method and at last it will execute @After
method.
1. @Before Annotation
As name suggest. method written under @Before annotation will be
executed before the method written under @Test annotation. Based on
@Before annotation, JUnit framework will execute that before method first.
Generally method under @Before annotation is used to initializing website
and other environment related setup. @Before annotation method will be
executed before each @Test annotation method means if there are two
@Test methods in your class then @Before method will be executed two
times.
2. @After
Method under @After annotation is known as after method and execution
of @After method will start as soon as completion of @Test method execution
completion. Same as @Before , @After annotation method will be executed
two times if there are two @Test methods in your class.
3. @Test
Method under @Test annotation is known as test method. Execution of @Test
method will start as soon as @Before method execution completed.
Generally we are writing all testing related activity under @Test. We can use
multiple @Test methods in single class too.
4. @BeforeClass
Methods under @BeforeClass annotation will be executed before the any test
method starts execution. It will be executed only once even if there are
multiple @Test
methods
in
your
class.
5. @AfterClass
@AfterClass method will be run on completion of all the test method's
execution from that class. Same as @BeforeClass, @AfterClass method will
be executed once only.
VIEW EXAMPLE OF DIFFERENCE BETWEEN @Before vs @BeforeClass
AND @After VS @AfterClass ANNOTATIONS
Execute bellow given example in your eclipse as a debugging mode to verify
its execution.
package Testjunit;
import java.util.concurrent.TimeUnit;
import
import
import
import
import
org.junit.After;
org.junit.Before;
org.junit.Test;
org.openqa.selenium.*;
org.openqa.selenium.firefox.FirefoxDriver;
@Before
public void beforetest() {
driver.manage().window().maximize();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
}
@After
public void aftertest() {
driver.quit();
}
@Test
public void test() {
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@value='Bike']")).click();
boolean str1 =
driver.findElement(By.xpath("//input[@value='Bike']")).isSelected();
if(str1 = true) {
System.out.print("Checkbox is checked");
}
else
{
System.out.print("Checkbox is not checked");
}
}
}
Here @After method is written before @Test method but JUnit Framework will
tells eclipse to execute @Test method first and then @After method
--6.2 @Before/@After VS @BeforeClass/@AfterClass Difference
Example Of Difference Between @Before/@After VS
@BeforeClass/@AfterClass In JUnit With WebDriver
Many
readers
are
asking
me
the
difference
between
JUnit
annotations @Before VS @BeforeClass and @After VS @AfterClass in
webdriver test. If you have read my JUNIT ANNOTATIONS POST, I have clearly
described difference between @Before and @BeforeClass annotations and
@After and
@AfterClass annotations in bold text. Now let me describe once more and
then we will look at practical example for both of them.
Same
as @Before
annotation, Test
method
marked
@After annotation will be executed after the each @Test method.
with
org.junit.After;
org.junit.AfterClass;
org.junit.Before;
org.junit.BeforeClass;
org.junit.Test;
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.firefox.FirefoxDriver;
}
@After
public void closebrowser() {
System.out.print("\nBrowser close");
driver.quit();
}
@Test
public void test1() throws InterruptedException{
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest
2 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']")).clear();
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest
2 class-test2");
Thread.sleep(2000);
System.out.print("\njunittest2 class-test2 method is executed");
}
}
When you execute above example in eclipse, Bellow given result will be
displayed in console.
Console Output :
Browser open
junittest2 class-test1 method is executed
Browser close
Browser open
junittest2 class-test2 method is executed
Browser close
Based on above given console output, we can say each @Before method is
executed before each @Test method and each @After method is executed
after each @Test method. Now let we replace @Before annotation
with @BeforeClass and @After annotation with @AfterClass in same example
and then observe result. Replace bellow given @BeforeClass and @AfterClass
part with @Before and @After part in above example as bellow.
@BeforeClass
@Ignore
@Test
public void test1() throws InterruptedException{
}
Here, @Ignore annotation will exclude test1() method from execution.
Copy bellow given Ignoring @Test method(test1()) part with @Ignore
annotation and replace it will @Test method part given on Step 3 (Create 2nd
test case) of THIS EXAMPLE (Note : @Test method part which needs to
replace is marked with pink color in that example post).
//To ignore this @Test method from execution
@Ignore
@Test
public void test1() throws InterruptedException{
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest
2 class-test1");
System.out.print("\njunittest2 class-test1 method is executed");
Thread.sleep(2000);
}
Now when you run full test suite(junittestsuite.java) then on completion of
junit test suite execution, you will get bellow given result in console.
junittest1 class is executed
junittest2 class-test2 method is executed
As per console result, ignored test(test1()) is not executed.
JUnit test execution report will looks like bellow.
This way, Junit's @Ignore annotation will help us to ignore specific test from
execution.
--6.4 Junit Timeout And Expected Exception Test
Example Of Junit Timeout And Expected Exception Test For WebDriver
Sometimes you need to set time out for your webdriver test or you need to
set expected exception condition for your test. Supposing you have written
test for one module and you wants to set timeout for your test. Here timeout
means allowed maximum time to complete full test. Same way, You are
expecting some
exception during your webdriver test execution and that exception is
acceptable.If you are using junit framework for your webdriver test then you
can do it very easily. We have seen example of - how to Ignore specific
webdriver test using junit's @Ignore annotation in THIS POST. Now let me
describe how to write timeout test and exception test in junit with examples.
Timeout Test In JUnit For WebDriver
We can specify timeout time with junit's @Test annotation as shown bellow. It
will allow maximum 2000 milliseconds to complete the execution of test1()
method. After 2000 miliseconds, it will skip remaining execution and test1()
will be marked with error.
@Test(timeout=2000)
public void test1(){
}
Expected Exception Test In JUnit For WebDriver
We can specify expected exception condition with @Test annotation as
bellow. Here my expected exception is null pointer exception so my expected
condition is NullPointerException.class. You can write your expected
exception like IndexOutOfBoundsException.class, ArithmeticException.class,
ect.
@Test(expected = NullPointerException.class)
public void exceptiontest2() {
}
Junit Example Of Timeout And Expected Exception Test
-Go to THIS PAGE
-Copy example given on Step 3 - Create 2nd test case and paste it in your
eclipse.
-Remove all @Test methods from that example and paste bellow given @Test
methods and then run your test class (junittest2.java).
@Test(timeout=2000)
public void test1() throws InterruptedException{
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest
2 class-test1");
System.out.print("\njunittest2 class-test1 executed before sleep");
Thread.sleep(5000);
System.out.print("\njunittest2 class-test1 executed after sleep");
}
@Test
public void exceptiontest1() {
throw new NullPointerException();
}
@Test(expected = NullPointerException.class)
public void exceptiontest2() {
throw new NullPointerException();
}
On completion of execution, console will show bellow given out put.
Console Output :
Browser open
junittest2 class-test1 executed before sleep
Browser close
JUnit test result looks like bellow.
test1() method is display with error because test was time out after
2000 milisecond as per our condition.
because
there
was
not
Generate webdriver test report in HTML by running build xml file : Part
-3
In Main tab, you need to verify that your current project's build.xml is
selected or not.My current project name is "JUnitReport" so it is correct
for me. If it is of any other project then you need to change it by
clicking on Browse Workspace button.
you can change it by unchecking and checking and the check box or by
clicking on order button. So now my target execution order will looks
like bellow.
If JDK is not display in Separate JRE list then you can add it by
- Click on Installed JREs button. It will open Preferences dialog.
- Click on Add button from Preferences dialog. It will open Add JRE
dialog.
- Select Standard VM from Add JRE dialog and click on next button as
shown in bellow image.
Now build file configuration is finished and finally you are ready to run
your build.xml file and generate your test report.
Select Ant Buildfiles from export dialog and clicking on Next button will
show you all your projects list. On Next dialog, select your current
project and click on Finish button as shown bellow.
Look in above image. When you will run build.xml file, your test reports
will be saved in JUnit output directory = 'junit' folder. It will be created
automatically in your project folder when you run build.xml file. We will
learn
how
to
run
build.xml
file
in
my
next
post.
When you click on Finish button, build.xml file will be generated inside
your project folder as shown bellow.
View my Next Post to see how to configure and run build.xml file to
generate your test case report in HTML view.