Showing posts with label JUnit. Show all posts
Showing posts with label JUnit. Show all posts

March 25, 2011

How to Create Reports using JUnit

March 25, 2011 34
Create Reports using JUnit:


After running test scripts in eclipse it would be good if we generate reports. Junit is one the most popular unit testing framework around. 


Below are the steps to generate reports:


1. Right click on project and click on "Export" option




   


2. Expand "General" node and select "Ant Buildfiles", click on "Next" button




3. Select the project (let all default options), 
Junit output Directory ... F:\Automation\First\junit  
click on "Finish" button




4. build.xml file should be created under project folder




5.  Now we need to add junit jar and Tools.jar file in Ant's "Global Entires"




6. Select "Window" menu, click on "Preferences"





7. In "Preferences" window, expand "Ant" root node, click on "Runtime"





8. Click on "Classpath" tab, expand "Global Entires" 





9. Click on "Add External JARs" button



10. Navigate to your "eclipse" directory (where eclipse is installed). Select "plugins ->org.junit_3.8.2.v3_8_2_v20100427-1100" directory. Select junit.jar file. and add Tools.jar which is located under java/JDK/Lib

11. Now right click on build.xml file and select Run As -> Ant Build.."





12. In "Edit Configuration and launch" window, select "build", "tootltip (the script we want to run)" and "junitreprot" checkboxes and click on "Run" button









13. All your test scripts will run and finally report will be generated under the same project directory under "junit" folder. To view JUint folder please refresh.




14. Click on "index.html", you will see the report 



Hope you all enjoy this post.





December 15, 2010

Test Suite using JUnit....

December 15, 2010 5
Test Suite using JUnit....
Most testing frameworks have the concept of grouping a set of text fixtures into a ’suite’ so that you can execute a number of related tests together.

Creating test suites with Selenium doesn’t appear to be obvious at first. Here’s a simple guide on how to create and execute suites of functional tests:

Here i am using Junit scripts and Test suite()


First create two scripts....Ex:Google1 and Google2

Google1.java

------------------------------------------------------------
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.*;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class Google1 extends SeleneseTestCase{

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}

@Test
public void testText1() throws Exception {
selenium.open("http://www.yahoo.com");
selenium.windowMaximize();
}
}

------------------------------------------------------------


Google2.java

------------------------------------------------------------
package defaultpackage;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class Google2 extends SeleneseTestCase{

private Selenium selenium;
private SeleniumServer seleniumServer;


public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}


public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}


public void testText2() throws Exception {
selenium.open("http://www.google.com");
selenium.windowMaximize();
}
}

------------------------------------------------------------


Here is the test suite....TestSuite1.java
------------------------------------------------------------

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

public class TestSuite1 extends TestCase {

public static Test suite()
{
TestSuite nag = new TestSuite();

nag.addTestSuite( Google1.class);
nag.addTestSuite( Google2.class);
return nag;
}

public static void main(String arg[])
{
TestRunner.run(suite());

}
}
------------------------------------------------------------



Run the TestSuite1.java script ...it will automatically run the Google1 and Google2....

Even though first test case is failed it will run the second one....we can change the order of the test cases...

December 13, 2010

Sample script using JUnit

December 13, 2010 0
Sample script using JUnit
Here is the sample script which will open a page...

This script was written using JUnit...


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class Google1 extends SeleneseTestCase{

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}



@Test
public void testText1() throws Exception {
selenium.open("http://www.yahoo.com");
selenium.windowMaximize();
}
@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}
}

October 26, 2010

How to do Data Driven Testing using JUnit

October 26, 2010 37
There are many ways to do Data driven tests.I used Excel sheet for reading data and FileInputStream method.

Below script explains you how to Read data from excel sheet and use the data to search google.

I am using Eclipse, selenium RC, Junit and Excel sheet for this script.


--->Create a Java project in eclipse.
--->Create a new class DatadrivenJUnit.
--->Paste the below code in Eclipse.
--->Chnage the path of excel file according to your requirement.







Please note that selenium will support only .xls format plese do not forget to change the excel file to .xls if you are using MS-office2007.

Below is the code


import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;


public class DatadrivenJUnit extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();

}

@Test
public void testDatadrivenJUnit() throws Exception
{
FileInputStream fi=new FileInputStream("F:\\Framework\\testdata\\search.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
selenium.open("http://www.google.com");
selenium.windowMaximize();
for (int i = 1; i < s.getRows(); i++)
 { //Read data from excel sheet
 selenium.type("name=q",s.getCell(0,i).getContents());
 selenium.click("btnG");
 Thread.sleep(1000); } }
@AfterClass
 public void tearDown() throws InterruptedException{
selenium.stop();
 seleniumserver.stop();
}

 }


 Please let me know if you need more information regarding Data driven testing....


An Introduction to Testing Web Applications with twill and Selenium