Tuesday 8 July 2014

Implement Junit Framework in selenium Webdriver


1) Prework: ( Setup JUnit Framework in eclipse)

Generally you do not need to install junit with eclipse if you have already added external jar files of webdriver  with eclipse. Because required jar file for junit will be already there with webdriver jar files while download from this site -> http://docs.seleniumhq.org/download/.

-> Before using Junit framework in our selenium, we need to make sure JUnit external Jar files are added in eclipse in below path.

Right click on your project -> Build Path -> Configure build path

It will open java build path window. In that window, go to Libraries tab. Here you will see all your jar files. In jar file tree view, look for the jar file name which is starting with junit.


-> If above required .jar file is not added in eclipse means then we need to download and add .jar file in this link -> http://junit.org/



2) Create JUnit Test Case.

We need to create JUnit testcase and will add to our package using below steps in eclips.

i) Right click on "src" -> new -> package-> create new package (EX: JuintPackage)
ii) Right click on above created package -> new -> other-> java-> JUnit-> JUnit test case -> create new testcase java file (Ex:JUnit_Test1.java)


And use use below code for JUnit  testcase as JUnit_Test1.java inside the package JuintPackage.


package JuintPackage;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Junit_Test1 {

WebDriver driver1=new FirefoxDriver();

@Before
public void beforetest()
{
driver1.manage().window().maximize();
driver1.get("http://google.com");
}

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


@Test
public void Execute_Process()
{

driver1.findElement(By.linkText("Gmail")).click();
}

}




And save the above test case.


iii) Run above JUnit Test case. 

Eclipse will run your test case using JUnit and on completion of execution, you can see your result in JUnit pane in eclipse.



3) Running JUnit Test suite (Run Multiple JUnit Test case)

When you run junit test suite, eclipse will run all test cases  one by one. When execution completed, you will see output lines in console.


Steps for running JUnit Test Suite

i) Create two Junit Test case by using Step(2). and saved as Junit_Test1 and JUnit_Test2 respectively.

ii) Then use below stesp for create JUnit Test Suite

 Right click on Package -> new -> other-> java-> JUnit-> select JUnit test suite -> give Test Suite name (JUnit_Tests1)-> choose Junit testcases need to be run (Chosse Junit_Test1 and Junit_Test2)-> click Finish


Its automatically create cose as below


package JuintPackage;

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

@RunWith(Suite.class)
@SuiteClasses({ Junit_Test1.class, Junit_Test2.class })
public class Junit_TestSuite1 {

}


if we see above suite class, there are two JUnit annotations added with name @RunWith and @SuiteCLasses

Here,

-> @RunWith annotation will references junit to run test in class

-> @SuiteClasses describes classes included in that test suite.



iii) when you run junit test suite, eclipse will run both test cases (Junit_Test1 and Junit_Test) 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




Ref: http://software-testing-tutorials-automation.blogspot.com/2014/01/learn-selenium-webdriver-online-free.html

No comments:

Post a Comment