Junit

Generally the entry point of any JAVA program is main function. and it is inside a class. in selenium the entry point can be any function having annotation @Test. consider below example. where in driver is initialized, then browser is maximized and a url is opened.

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

public class MyTest
{

@Test
public void LoginTest()
{

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://clinic.scriptinglogic.com/auth/login_form.php");
}
}

What is junit 

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development. JUnit is perfect unit test framework for java programming language. Open source Java testing framework used to write and run repeatable automated tests.

/*

* This class prints the given message on console.

*/

public class MessageUtil {



private String message;


//Constructor

//@param message to be printed

public MessageUtil(String message){

this.message = message;

}



// prints the message

public String printMessage(){

System.out.println(message);

return message;

}

}

Create Test Case Class

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TestJunit {

String message = "Hello World";

MessageUtil messageUtil = new MessageUtil(message);

@Test

public void testPrintMessage()

{

assertEquals(message,messageUtil.printMessage());

}

}

 

Annotations in JUnit –

  • @Before –

public void method()

The @Before annotation indicates that the attached method will be run before any test in the class. It is mainly used to setup some objects needed by your tests.

 

  • @BeforeClass –

public static void method()

The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class. That happens when the test methods share computationally expensive setup (e.g. connect to database).

 

  • @After –

public void method()

Method that is marked with @After gets executed after execution of every test.  If we need to reset some variable after execution of every test then this annotation can be used with a method that has the needed code. It is used to cleanup the test environment (e.g., delete temporary data, restore defaults).

 

  • @AfterClass –

public static void method()

In the same way “@AfterClass” annotation can be used to execute a method that needs to be executed after executing all the tests in a JUnit Test Case class. It is used to perform clean-up activities, for example, to disconnect from a database.

 


import org.junit.*;

import static org.junit.Assert.*;

import java.util.*;

public class JunitTest1 {

private Collection collection;

@BeforeClass

public static void oneTimeSetUp() {

// one-time initialization code

System.out.println("@BeforeClass - oneTimeSetUp");

}



@AfterClass

public static void oneTimeTearDown() {

// one-time cleanup code

System.out.println("@AfterClass - oneTimeTearDown");

}



@Before

public void setUp() {

collection = new ArrayList();

System.out.println("@Before - setUp");

}



@After

public void tearDown() {

collection.clear();

System.out.println("@After - tearDown");

}



@Test

public void testEmptyCollection() {

assertTrue(collection.isEmpty());

System.out.println("@Test - testEmptyCollection");

}


@Test

public void testOneItemCollection() {

collection.add("itemA");

assertEquals(1, collection.size());

System.out.println("@Test - testOneItemCollection");

}

}

OUTPUT –

@BeforeClass – oneTimeSetUp

@Before – setUp

@Test – testEmptyCollection

@After – tearDown

@Before – setUp

@Test – testOneItemCollection

@After – tearDown

@AfterClass – oneTimeTearDown

 

What is Assert?

JUnit provides static methods in the Assert class to test for certain conditions. These assert statements typically start with assert and allow you to specify the error message, the expected and the actual result. An assertion method compares the actual value returned by a test to the expected value, and throws an AssertionException if the comparison test fails.

EXAMPLE –

 


import com.sun.webkit.Disposer;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import java.util.concurrent.TimeUnit;

public class MyJunit {
 @Test
 public void loginTest()
 {

 System.setProperty("webdriver.ie.driver", "IEDriver/IEDriverServer.exe");
 WebDriver driver = new InternetExplorerDriver();
 driver.manage().window().maximize();

 driver.get("http://localhost/openclinic/auth/login_form.php");
 driver.findElement(By.xpath("//input[@id='login_session']")).sendKeys("admin");
 driver.findElement(By.xpath("//input[@id='pwd_session']")).sendKeys("admin1");
 driver.findElement(By.xpath("//input[@id='login']")).click();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

 boolean result=false;
 try {
 result = driver.findElement(By.xpath("//a[@href='../auth/logout.php']")).isDisplayed();

 }

 catch(Throwable t)
 {
 result=false;
 }

 Assert.assertEquals("Test failed",true,result);

 }

}

 

 

OUTPUT –

java.lang.AssertionError: Test failed

Expected :true

Actual   :false

 


Search

Subscribe to YouTube

Training on Selenium

Like us on facebook

Jobs

Jobs

Cooking with Amol