Hi All,
Today i will be posting about use of Junit test framework and how to configure it with Selenium and run the test cases through it.
This post will be only for the beginners of Selenium/WebDriver/Junit. For the experts, any comments will be appreciated and will help us all in a constructive way.
Ok Lets start. Junit is unit test framework which can be used to run a java program in a particular way. Mainly designed for Developers and can be extended to use for functional testing when configured or used with Selenium like tool. So the bottom line is, Junit can run a java program which is designed as like a junit test case.
Any java program runs by the main method. junit runs by the @Test annotation. Annotation is a mechanism to tell junt runner that this is test case and run it. There are many such annotations present in junit to perform many operations.
You need latest Junit jars to configured in the build path, if you are using selenium then Junit jars are already bundled with the Selenium Server jars.
Lets code now-:
package core.come.TestScripts;
import org.junit.Test;
public class TestJunit {
@Test
public void RunMyJunit(){
System.out.println("Running Junit Test Case");
}
}
When you run this, in the console, "Running Junit Test Case" will be printed. You can there is no Main method as it not a java application file but it is a junit test case program.
Junit runner checks for the @Test annotation and run the method which is associated with it. For those who want to use with Selenium, @Test method should contain the actual test case which you want to execute. You can have multiple number of @Test methods in a junit test case program.
Ok now let us see some other important annotations. See the below code
package core.come.TestScripts;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestJunit {
@Before
public void RunBeforeTest(){
System.out.println("Running this method before Test Method");
}
@Test
public void RunMyJunit(){
System.out.println("Running Junit Test Case");
}
@After
public void RunAfterTest(){
System.out.println("Running this method after Test Method");
}
}
Console Output will be -:
Running this method before Test
Running Junit Test Case
Running this method after Test
Two new annotations added, @Before and @After to the above script. As it is name suggested @Before method will run before each @Test and @After will run after each @Test. That means if @Test method is more than one in the junit test case, then @Before and @After will be called for @Test method.
Consider a situation in Selenium, where you want to automate a web application having a login page, some pages to validate and then logout. For every page validation
You have to do a separated login and logout for every page validation in the application. It is the same as a manual tester do the task.
In that scenario, you need to take the help of @Before and @After. Create two methods login(userName,Pwd) and logout() and have it with @Before and @After annotation.
Now run the junit test case, for each @Test method, the login and logout will be called. It is easy,isnt it?
So this is a very simple and descriptive explanation about using/running Junit test case. There are many such annotations present in junit using those you can perform many operations.
Here is a link to junit annotations, Do a self study, and will post some other complex annotations as well.
Please do not forget to leave a comment/suggestion so that We can all leverage from that.
Be tuned!!
Thanks!!
Today i will be posting about use of Junit test framework and how to configure it with Selenium and run the test cases through it.
This post will be only for the beginners of Selenium/WebDriver/Junit. For the experts, any comments will be appreciated and will help us all in a constructive way.
Ok Lets start. Junit is unit test framework which can be used to run a java program in a particular way. Mainly designed for Developers and can be extended to use for functional testing when configured or used with Selenium like tool. So the bottom line is, Junit can run a java program which is designed as like a junit test case.
Any java program runs by the main method. junit runs by the @Test annotation. Annotation is a mechanism to tell junt runner that this is test case and run it. There are many such annotations present in junit to perform many operations.
You need latest Junit jars to configured in the build path, if you are using selenium then Junit jars are already bundled with the Selenium Server jars.
Lets code now-:
package core.come.TestScripts;
import org.junit.Test;
public class TestJunit {
@Test
public void RunMyJunit(){
System.out.println("Running Junit Test Case");
}
}
When you run this, in the console, "Running Junit Test Case" will be printed. You can there is no Main method as it not a java application file but it is a junit test case program.
Junit runner checks for the @Test annotation and run the method which is associated with it. For those who want to use with Selenium, @Test method should contain the actual test case which you want to execute. You can have multiple number of @Test methods in a junit test case program.
Ok now let us see some other important annotations. See the below code
package core.come.TestScripts;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestJunit {
@Before
public void RunBeforeTest(){
System.out.println("Running this method before Test Method");
}
@Test
public void RunMyJunit(){
System.out.println("Running Junit Test Case");
}
@After
public void RunAfterTest(){
System.out.println("Running this method after Test Method");
}
}
Console Output will be -:
Running this method before Test
Running Junit Test Case
Running this method after Test
Two new annotations added, @Before and @After to the above script. As it is name suggested @Before method will run before each @Test and @After will run after each @Test. That means if @Test method is more than one in the junit test case, then @Before and @After will be called for @Test method.
Consider a situation in Selenium, where you want to automate a web application having a login page, some pages to validate and then logout. For every page validation
You have to do a separated login and logout for every page validation in the application. It is the same as a manual tester do the task.
In that scenario, you need to take the help of @Before and @After. Create two methods login(userName,Pwd) and logout() and have it with @Before and @After annotation.
Now run the junit test case, for each @Test method, the login and logout will be called. It is easy,isnt it?
So this is a very simple and descriptive explanation about using/running Junit test case. There are many such annotations present in junit using those you can perform many operations.
Here is a link to junit annotations, Do a self study, and will post some other complex annotations as well.
Please do not forget to leave a comment/suggestion so that We can all leverage from that.
Be tuned!!
Thanks!!
Hi
ReplyDeleteimport static org.junit.Assert.*;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.internal.selenesedriver.FindElement;
import org.testng.Reporter;
import com.thoughtworks.selenium.Selenium;
public class LoginTesting {
WebDriver driver =new FirefoxDriver();;
WebElement element;
Selenium selenium;
String value1="Personal Leave Information";
String value2="The login or password you have entered is not correct or has expired. Please try again.";
@Rule
public ErrorCollector collector=new ErrorCollector();
@Before
public void setUp() throws Exception
{
driver.get("http://10.30.10.237/ILTS_V1.0/index.php");
System.out.println("entered into the testing page");
}
@After
public void afterClass() {
}
//http://10.30.10.237/ILTS_V1.0/index.php
@Test
public void test() throws InterruptedException
{
element=driver.findElement(By.id("txt_login"));
element.sendKeys(new String[] {"Emp11"});
element=driver.findElement(By.id("txt_password"));
element.sendKeys(new String[] {"asdas"});
driver.findElement(By.xpath("/html/body/div/div[2]/form/center/div/table/tbody/tr/td[2]/input")).click();
}
@After
public void aftertest()
{
System.out.println("test is over");
}
}
i want my selenium webdriver to report test as failed if user fails to login
Thanks
Ashish Juyal
Hello Ashish,
DeleteThanks for the comment!!
You need to verify whether after login , home page is showing up or not. You can use junit assert to do the same. you can do this with the URL of the home page, or any text present in the home page.
eg:
Assert.assertEquals("Actual Page", "Expected Page");
Let me know if you need any further information/doubt on this.
Your information about Selenium scripts is really interesting. Also I want to know the latest new techniques which are implemented in selenium. Can you please update it in your website?
ReplyDeleteSelenium training Chennai