December 15, 2010

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...

5 comments:

  1. Hi Nag,
    You are really awesome in explaining selenium... I thank you so much for the stuff you are giving out through this blog and like to follow up with you in the future as well... hope you will be there for any doubts !!
    Regards,
    Fayaz

    ReplyDelete
  2. Hi naga,
    can we run the suite using testNG just as junit?

    ReplyDelete
  3. Hello, I have testsuite ready and it is running, but I need to generate reports.. could you please help me

    ReplyDelete
  4. you can also creae Suite as below

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

    @RunWith(Suite.class)
    @SuiteClasses({Login.class,Upload.class})
    public class MyTestSuite {

    }

    ReplyDelete