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

Friday, February 4, 2011

Suite creation in Junit4

Suite creation in Junit4

First Test


package com;


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

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

public class FirstOne {
    
    private static SeleniumServer seleniumServer;
    private static Selenium selenium = new DefaultSelenium( "localhost",4444, "*chrome","http://www.google.com");
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        seleniumServer = new SeleniumServer();
        seleniumServer.start();
        selenium.start();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
          selenium.stop();
          seleniumServer.stop();
    }

    @Test
    public void Testing() throws Exception{
        selenium.open("/");
        selenium.waitForPageToLoad("6000");
        selenium.type("q", "search");
        selenium.click("btnG");
 }

}


Second Test



package com;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

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

public class SecondOne {

 private static SeleniumServer seleniumServer;
 private static Selenium selenium = new DefaultSelenium( "localhost",4444, "*chrome","http://www.google.com");
 
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
  seleniumServer = new SeleniumServer();
        seleniumServer.start();
        selenium.start();
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
    selenium.stop();
       seleniumServer.stop();
 }

 @Test
 public void Testing() throws Exception{
  selenium.open("/");
  selenium.waitForPageToLoad("6000");
  selenium.type("q", "selenium");
  selenium.click("btnG");
 }
}

Suite Class 


package com;



import org.junit.runner.RunWith;

import org.junit.runners.Suite;



@RunWith(Suite.class)

@Suite.SuiteClasses({FirstOne.class, SecondOne.class})

public class TestSuite {

//nothing

}

Tuesday, December 28, 2010

Creating test suites with Junit4

Test Suite can be created in JUnit 4 as

import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class TestSuite {
//nothing
}


The @RunWith(Suite.class) gives opportunity to combine both JUnit 4 and JUnit 3 tests and test cases together:


@RunWith(Suite.class)
@Suite.SuiteClasses({
ExampleOfJunit3TestSuite.class,
ExampleOfJUnit3TestCase.class,
ExampleOfJUnit4TestSuite.class,
ExampleOfJUnit4TestCase.class})
public class BothJUnit4and3TestSuite {
}


The Both JUnit4 and 3 TestSuite runs all tests and test suites listed in @Suite.SuiteClasses.

Thursday, September 2, 2010

Running your first selenium test case in eclipse

1- In FireFox ,Start Your Selenium-IDE from tools->Selenium IDE
2- Record your script
3- Go to File->Export test case as->Choose the programing language as JAVA and save your script.
4-Save it with .java extension

The next step is to open your eclipse and create a project.

1- Creating your java project in eclipse
The first step in eclipse is to create/add your project. To do this follow the steps below:
-> Launch Eclipse
-> Right click in navigator->Create a java project named TestProject (for example)

2- The next step is to add JUnit Libraries which consists of following steps:
-> Go to Project Properties
-> Go to Java Build Path
-> Click on Add external-jar button in Libraries tab
-> Browse and go to the location where u stored ur junit.jar.

4-Add Selenium Java Client Jar into Project:-
Once you added the Junit jars , the next step is to add selenium jar file. you can find selenium-server.jar under selenium-remote-control\selenium-java-client-driver folder what you have downloaded from

http://selenium-rc.openqa.org/download.html

To add this Jar file follow the below steps:
-> Go to Project Properties
-> Go to Java Build Path
-> Click on Libraries tab
-> Click on 'Add External Jars' button
-> Browse and add the 'selenium-server.jar' from the source where you had saved it.

5- Initiating the Selenium Test Server
To start the selenium test server, first go to the location where your selenium-server.jar is placed.
and then Run the command java -jar selenium-server.jar

This will start you selenium server.

6- Adding your test (Java class ) in our Project
To add you test (which you have saved earlier) as a java class file to the project (TestProject, already saved) follow the steps:
-> In eclipse go to create a new java class
-> Create it with name MyFirstTestCase (for example) and paste the code from the file u had saved it
-> Choose "com.thoughtworks.selenium.SeleneseTestCase" as its super class
-> Finish it

7- Modifying your test script (java class)
Once you are done with ur class file, it will look like,

Package.*
import com.thoughtworks.selenium.SeleneseTestCase ;
public class MyFirstTestCase extends SeleneseTestCases {
public void setUp() throws Exception {
setUp( " http://www.google.com/ " , "*firefox'); }

............ You test script..............
}

In case you want to run this in diff browser then simply you can change your script
setUp( " http://www.google.com/ " , "*firefox');
to setUp( " http://www.google.com/ " , "*chrome'); or whatever browser you need to run with.

8- Running your test
Finally we are ready to tun our first test. To run this in eclipse you click on Run button
select option Run as "JUnit Test'

So finally I am done with your end to end execution of ur java test case using selenium RC in eclipse. For report generation using JUnit in eclipse,check my other post Thanks.



For more info,check blogs of http://www.mpowerglobal.com

how to create report in JUnit

Its very simple to create JUnit reports using eclipse

follow these steps and it worked for me

1) First generate an Ant build file for your project.
In the Export page select "Ant Buildfiles" it is under General. Then select a
project that contains your JUnit tests. The default JUnit output directory is
appropriately named "junit".


2) Next resolve dependencies by making sure that the junit.jar is added to Ant's
"Global Entries."
Window->Preferences->Ant->Runtime->Global Entries and Add External JARs...
Navigate to your "eclipse" directory (where eclipse is installed). It is under
"plugins" directory and presently the junit.jar is in a directory named
"org.junit_3.8.1". If this is missing then you will see error messages since
junit is an optional Ant task.




3) Run then right click on the Ant build file (by default build.xml) and Run
As->Ant Build... This will display a list of targets. All of the launch
configurations you have previously configured will have a corresponding target
in your Ant build file. Select the desired target(s), and also select the
"junitreport" target. Check the "Target execution order" text area to make sure
the junitreport is run last.



Whenever you run a test case it will generate results and will be placed in the
junit output directory. These results are formatted into an HTML report by the
junitreport target and stored in the junit output directory. This is a quick explanation of the process, but hopefully it helps get you off your feet.



For more info,check out http://www.mpowerglobal.com/web/guest/blog