Monday, January 31, 2011

Introduction to TestNG

Introduction:

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalists that make it more powerful and easier to use.

TestNG is a testing framework designed to simply a broad range of testing needs, from unit testing (testing a class in isolation of the others) to in integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers)

Installing TestNG

Installing TestNG

Downloading and Installing TestNG is same of that of JUnit. (Refer this link before moving further)

1. Download testng-5.9-jdk15.jar file

2. For the purpose of this tutorial, download testng-<version-number>.jar file into C TestNG folder of C drive.

3. Now you have extracted TestNG, you need to add testng-5.9-jdk15.jar to your classpath. This enables the Java compiler to resolve where and what TestNG is.

Installing TestNG in Eclipse IDE

Installing TestNG in Eclipse IDE

Following steps will guide you to install TestNG in Eclipse IDE.

1. Open Eclipse 

2. Click on "Help" menu, select "Install New Software..." (see the following figure)
Figure: Select "Install New Software" from Help menu
3. In the "Install" wizard, click on "Add" button

4. In the "Add Repository" window, enter Name as "TestNG" and Location as "http://beust.com/eclipse", click on "OK" button

5. TestNG will be added into the "Install" wizard, select "TestNG" checkbox and click on "Next" button.

Figure: Install Wizard
6. Click on again "Next" button from "Install Details" page.

7. In the "Review Licenses" page, select "I accept the terms of the license agreement" radio button and click on "OK" button

Eclipse will start installing the latest version of "TestNG"

8. The wizard will ask you to re-start eclipse.

Now, TestNG has installed sucessfully. You can create your unit test cases by using TestNG.

Friday, January 28, 2011

Connecting SQL Server using Windows Authentication

Connecting SQL Server using Windows Authentication


Step 1: Setup ODBC


To set up ODBC
a. goto Control Panel
b.Double click on "Administrative Tools"
c. Double click on "Data Sources (ODBC)"
d. Under "User DSN" tab, click on "Add" button
e. In "Create New Data Set" window, select "SQL Native Client"
f. In "Create a New Data Source to SQL Server" window, enter data source name, select your db server for which you want to connect from the "Server" drop down list
g. Click on "Next" button
h. In the next window, select the 1st option i.e., "With Integrated Windows authentication", click on "Next" button
i. Click on "Finish" button


This newly added data source should be available for you.


Step 2: Adding "sqljdbc4.jar" file


Add "sqljdbc4.jar file to your project library.

Step 3: Write the following Class to connect DB

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Database{
public Connection conn = null;
private String dbName = null;

public Database(){
}

public Database(String dbName, String dataSourceName){
this.dbName = dbName;
try{
      class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
       this.conn = DriverManager.getConnection(dataSourceName)
      System.out.println("Connected Successfully");
     }

catch(ClassNotFoundException e){
e.printStackTrace();
}

catch(SQLException e){
e.printStackTrace();
}
}

Step 4:  Execute it from Main Method:

import java.sql.*;

public class Main{

public static void main(String[] args)
{
try{
     Database db = Database("SQLEXPRESS", "jdbc:odbc:MySQLDB");
     }
catch(Exception e){
e.prntStackTrace();
}

Output: Connected Successfully

Thursday, January 27, 2011

Running Selenium IDE Test Suite

Running Test Suite

Test suite prepared in the Selenium IDE can be run in a several way. Look at the Following are 3 methods with the help of which you can run the test suite prepared in the selenium IDE

1. From the Selenium IDE:

a. We can run the designed selenium test suite from the selenium IDE itself. Please refer Building Test Suite in Selenium IDE link which will guide you how to prepare selenium test suite.

b. Open the saved selenium test suite. Click on File > Open Test Suite. This open all your test cases.

c. Click on "Play entire test suite" button (First green arrow button). 

Selenium will run all your test and produces the test results in the "Results" pane.

2. From Selenium RC

We can run the same selenese test scripts by using Selenium RC server.
To run the HTML test suite, we use -htmlSuite command.

-htmlSuite requires you to specify:
The syntax of -htmlSuite command is:
-htmlSuite(browserString, startURL, suiteFile, suiteFile, resultFile)

where,
  • browserString is the browser name ex: "*firefox"
  • startURL is the base URL ex: http://demo.actitime.com/
  • suiteFile - Test Suite file lcation ex:  C:\SIDE
  • resultFile - You need to create a blank html file named results.html and specfify the path of thos file. Ex: C:\SIDE\results.html
Follow these steps to run the suite:

1. Create a HTML file that will keep your test results Ex: C:\SIDE\results.html
2. Oprn a command link
3. move to the selenium-server.jar location Ex: cd D:\selenium-remote-control-1.0.3\
4. Now the -htmlSuite command with required arguments.
Ex: java -jar selenium-server.jar -port 4444 -htmlSuite *firefox, "http://www.google.com" "c:\SIDE\ActiTimeTestSuite.html" "C:\SIDE\results.html"

You will see Test Runner will open and start running your tests in firefox browser

After the test script is done, you can check the test results in the HTML file you provided as argument i.e., C:\SIDE\results.html

Your test results should look like this:

Figure: Test Results




Monday, January 24, 2011

Building Test Suite in Selenium IDE

Test Suite:

A test suite is a collection of test cases which is displayed in the leftmost pane in the IDE. The test suite pane can be manually opened or closed via selecting a small dot halfway down the right of the pane (whic is the left edge of the entire Selenium-IDE window if the pane is closed).

The test suite pane will be automatically opened when an existing test suite is opened or when the user selects the New Test Case item from the File menu. In the later case, the new test case will appear immediately below.

Creating Test Cases:

Before creating a test cases, login to following application and click through the links http://demo.actitime.com/

Now create a test case. To create a test case,
  1. Open Selenium IDE
  2. Go to File menu > New Test Case
  3. Click on "Record" button
    • Open http://demo.actitime.com/
    • Enter Username as "user"
    • Enter Password as "user"
    • Click on "Login" button
    • Once you enter into the application, right click on the firefox box (within the application and select "assertTitle actiTIME - Enter Time-Track"
    • Click on "Stop Recording" button 
  4. Now to save a test case, select File > Save Test Case
  5. Save this test case with name "01_Login.html" into C:\SIDE folder (or any folder you want)
  6. You can debug/verify this test case, by running it.
Similarly record some test cases by clicking on "Tasks" and "Reports" link and assert the page. Save those test cases as "02_Tasks.html" and 03_Reports.html"

Creating a Test Suite:

To create a test suite in Selenium IDE
  1. Go to File menu > New Test Suite
  2. To add the test cases: Go to File > Add Test Case
  3. Navigate to the location of your test case (in our case C:\SIDE)
  4. Select 01_Login.html, click on "Add"
  5. Repeat steps 2-4 for more test cases
  6. To save your test suite, select File > Save Test Suite and name it as ActiTimeTestSuite.html
Your test suite should look like this.

Figure: Test Suite in Selenium IDE


Installing AutoIt

Installing AutoIt

1. Goto following site - http://www.autoitscript.com/autoit3/downloads.shtml

2. Click on the highlighted link shown in the figure and save to your desktop.

Figure: AutoIt down loader link

3. You are ready to start installing AutoIt. Double click on the AutoIt installer file you downloaded. This will begin the installation process.

4. The first window that appears is the welcome screen for the AutoIt installer. This provides you with some basic information about version of AutoIt that you are installing. Click the Next > button when you are ready to continue.

5. The next screen is the licensing agreement. Read through it carefully to ensure that you agree with it. When you are finished reading it, click on I Agree to continue.

6. In the third screen, you are provided with a couple of options. The window is asking you what you want to happen when you double click on a file that has a.au3 extension. Files with .au3 extension are typically scripts written in AutoIt. If you select Run the script, the script dile you have just double clicked will run the contents of the script. If you select Edit the script, the file will open up in your default AutoIt editor.

Once you have decided on the option you would like to use, select it and click on the "Next" button.

7. Next screen will ask you to select "Choose Components". Select "Full" and click on "Next" button.

8. The next window prompts you for a location to install "AutoIt". By default, it selects C:\Program Files\AutoIt3. If you would like to install it somewhere else, you can try it. Once you decide the location of AutoIt, click the Install button to begin the installation.

9. While AutoIt is installing you will see a progress bar indicating how far along the AutoIt installation has progressed.

10. Once the installer has made all of the changed, you will see the final window telling you that AutoIt has successfully installed. Click Finish to complete the installation process.

Friday, January 21, 2011

Recording and Running Tests in Selenium IDE

Recording in Selenium IDE

1. Open Firefox
2. Click on Tools menu, select "Selenium IDE"
3. Click on the red record button on the right (as shown in the following figure
Figure: Recording button
4. Browse to google.com and enter "Selenium" in the search box and click on "Google Search" button
5. Click on the first result, which is that of seleniumhq.org
6. Stop the recording by clicking on again the red record button (See the above figure)

You should see something like below once you recording is done.

Figure: Selenium IDE Recorded Scripts

Running Recorded Selenium Test:

To run the above Selenium recorded script you need little modifications. Replace the last command "click" to "clickAndWait" (We will discuss this in later)

To make the above modification, highlight the line, in the "Command" text box, replace the "click" with "clickAndWait" (see the below figure)

Figure: Editing a Selenium Command
Now to run your recorded test, click on "Run current test case" (second Green arrow button)

The IDE should play your recorded test. After the test is complete you should have landed on the selenium page (http://seleniumhq.org/). The IDE after the test run is shown below. In the "Log Section" you can see the various events run by the test. In the table tab you can see that all the rows are green, which means that the test ran successfully.

Also at the bottom left, it will shown number of test cases passed and failed (Highlighted in the below screen shot)


Figure: Selenium Test Result

Monday, January 17, 2011

Selenium IDE Recording Option

Selenium IDE Recording Option

Many firts-time users begin by recording a test case from their interactions with a website. When Selenium-IDE is first opened, the record button is ON by default.

Figure: Selenium "Record" button
 This can be set to OFF as a default with an avilable user extension.

To set OFF follow the below steps.
  1. In Selenium IDE, click on "Options" menu
  2. Select "Options..."
  3. In "General" of "Selenium IDE Options" window, un-check "Start recording immediately on open" check-box
  4. Click on "OK" button.
  5. Close the "Selenium IDE"
  6. Re-start the Firefox
When you open "Selenium-IDE" the record button will be OFF.

Monday, January 10, 2011

JUnit Annotation Type - RunWith

7. Annotation Type RunWith:  

When a class is annotated with @RunWith to extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in the class instead of the runner built into JUnit. For example, suites in the JUnit 4 are built using RunWith, and a custom runner names Suite:

@RunWith(Suite.class)
@SuiteClasses(ATest.class, BTest.class, CTest.class)
public class ABCSuite{
}

Class Parametrized:

The custom runner Parameterized implements parameterized tests. When running a parameterized test class, instances are created for the class-product of the test methods and test data elements.

Junit Parameterized Test is new feature invoked in JUnit4, it means vary parameter value for unit test. In fact some method have many difference parameters value to input, if you write a testcase for per parameter value, this could be quite large numbers of test case you need to code in Junit, which has supported class Parameterized as for Parameterized Test function, use @RunWith annotate Parameterized as this test class runner, use @Parameters annotate method data() and it have to return List[], the parameter will pass into class constructor as argument.

Example:

Write the following JUnitTest in Eclipse.

import java.util.Arrays;
import java.unit.Collection;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

 @RunWith(value = Parameterized.class)
public class JUnitTest extends Assert{

            String name;
            int age;

            public JUnit4Test(String name, int age){
                      this.name = name;
                      this.age = age;
            }

           @Parameters
           public static Collection<Object[]> data(){
                    object[][] data = new Object[]{{"Parameterized test1",       10},{"Parameterized test2", 20}};
                    return Arrays.asList(data);
           }

          @Test
          public void testOut(){
                   System.out.println(name + " " + age);
          }

Output:
Parameterized test1 10
Parameterized test2 20

JUnit Annotation Type - AfterClass

6. Annotation Type AfterClass:

If you allocate expensive external resources in a BeforeClass method you need to release them after all the tests in the class have run. Annotating a public static void method with @AfterClass causes that method to be run after all the tests in the class have been run. All @AfterClass methods are guaranteed to run even if a BeforeClass method throws exception. The @AfterClass method declared in superclasses will be run after those of the current class

Here is a simple example:
public class Example{
        DatabaseConnection database;
        @BeforeClass
         public static void login(){
                database = ...;
     
         @Test
         public void something(){
                ...
         }

        @Test
         public void somethingElse(){
               ...
         }

         @AfterClass
         public static void logout(){
                  database.logout();
         }
}

Example in Eclipse:

import java.util.Date;

public class Person2{

         private Date birthday;
         private String name;

         public Person2(String name, Date birthday){
                 this.name = name;
                 this.birthday = birthday;
          }

          public Person2(String name){
                 this.name = name;
           }

           public Date getBirthday(){
                  return birthday;
           }

           public void setBirthday(Date birthday){
                   this.birthday =  birthday;
           }

           public String getName(){
                    return name;
           }

           public void setName(String name){
                    this.name = name;
            }

           public int getAge(){
                   if(this.birthday == null return 0;
                   Date now = new Date();
                   return now.getYear() - this.birthday.getYear() + 1;
            }
}

Here is the test code which is used to test class Person2.

import org.junit.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class Person2Test extends Assert{
         public static SimpleDateFormat df;
         private Person2 person2;

         @BeforeClass
         public static void BeforeClass(){
                  df = new SimpleDateFormat("yyyy-mm-dd");
                  System.out.println("BeforeClass");
         }

         @Before
         public static void setUp() throws Exception{
                  Date date = df.parse("2010-10-14");
                  Person2 = new Person2("Jimmy", date);
                  System.out.println("setUp");
        }

        @Test
         public void getAge(){
                  assertEquals(person2.getAge(), 6);

                  System.out.println("Test getAge");
        }

       @Test
         public void getName(){
                  assertEquals(person2.getName(), "Jimmy");


                  System.out.println("Test getName");
        }

        @After
         public void tearDown() throws Exception{
                  person2 = null;

                  System.out.println("tearDown");
        }



        @AfterClass
         public static void BeforeClass(){
                  df = null;
                  System.out.println("AfterClass");
         }
}

JUnit Annotation Type - BeforeClass

5. Annotation Type BeforeClass:  

Sometimes several tests need to share computationally expensive setup (like logging into a database), While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @Before causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

For example:

public class Example{
         @BeforeClass public static void onlyOnce(){
            ...
          }

          @Test public void one(){
            ...
          }
     
          @Test public void two(){
            ...
          }
}

JUnit Annotation Type - After

4. Annotation Type After:

If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method. All @After methods are guaranteed to run even if a Before or Test method throws an exception. The @After methods declared in superclasses will be run after those of the current class.

Here is a simple example:

public class Example{
         File output;
         @Before public void createOutputFile(){
                     output = new File(...);
          }

         @Test public void something(){
                   ...
          }

         @After public void deleteOutputFile(){
                 output.delete();
          }
}

Example in Eclipse:

Create the class called Person in eclipse.

package test;

public class Person
{
       private String name;
    
       public Person(String name)
       {
             this.name = name;
       }

      public String getName()
      {
           return name;
      }
}

Here is the test cose which is used to test Person class.

package test;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class PersonTest
{
        private Person person;

       @Before
       public void setUp() throws Exception{
               person = new Person("Jimmy");
               System.out.println("setUp");
       }

      @Test
       public void getName(){
                assertEquals(person.getName(), "Jimmy");
                System.out.println("Test getName");
       }

      @After
      public void tearDown() throws Exception{
               person = null;
               System.out.println("tearDown");
     }
}

Output:
setUp
Test getName
tearDown

JUnit Annotation Type - Before

3. Annotation Type Before: 

When writing tests, it is common to find that several tests need similar object created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class.

Here is a simple example:



public class Example{
       List empty;
       @Before
        public void initialize(){
               empty = new ArrayList();
         }
         @Test public void size(){
             ...
         }
          @Test public void remove(){
             ...
        }
}

JUnit Annotation Type - Ignore

2. Annotation Type Ignore:

Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and number of tests that failed.

For example:

@Ignore @Test public void something(){ ...

@Ignore takes an optional default parameter if you want to record why a test is being ignored.

@Ignore("not ready yet") @Test public void something() { ...

@Ignore can also be applied to the test class.
                 @Ignore public class IgnoreMe{
                             @Test public void test1(){....}
                             @Test public void test2(){....}
                  }

Example: Create a following class in Eclipse and name it as JUnitTest4

import org.junit.Ignore;
import org.junit.Test;

public class JUnitTest4
{
       @Test
       public void test1()
       {
              System.out.println("test1");
       }

       @Ignore("not ready yet")
       @Test
       public void test2()
       {
              System.out.println("test2");
       }

       @Test
       public void test3()
       {
              System.out.println("test3");
       }
}

Right click within the test method area and choose "Run As - JUnit Test"

Figure: Example for JUnit @Ignore annotation

JUnit Annotation Type - Test

1. Annotation Type Test

The Test annotation tells JUnit that the public void methos to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as failure. If no expectations are thrown, the test is assumed to have succeeded.

A simple test looks like this:

public class Example{
        @Test
         public void method(){
                org.junit.Assert.assertTrue(new ArrayList().isEmpty());
          }
}

Example in Eclipse IDE:

1. Open your Eclipse IDE.
2. Add a new class to the test project
3. If needed please import the Junit library to your currect project decency libraries.
4. Write a class named SimpleTest and extend junit.framework.TestCase (actually if you Junit4, you don't need extend junit.framework.TestCase)
5. Write a test method and asserting desired result. You can use annotation "@Test" to declare this method is test method.

Figure: Example for JUnit @Test annotation






Right click within the test method area and choose "Run As - JUnit Test"

Test annotation with optional parameters:

The Test annotation supports two optional parameters:

a. expected
b. timeout

1.1 Test annotation with 'expected' optional parameter:

The first, expected, declared that a test method should throws an exception. If it doesn't throw an exception or it it throws a different exception that the one declared, the test fails. For example, the following test succeeds.

Example:

@Test(expected=IndexOutOFBoundsException.class)
public void outOfBounds(){
        new ArrayList<Object>().get(1);
}

Example in Eclipse:

Crate a following class in Eclipse IDE.

import org.junit.*;

public class JunitTest2
{
         @Test(expected = ArithmeticException.class)
          public void divisionWithException(){
          int i = 1/0;
          }
}

Right click within the test method area and choose "Run As - Junit Test"

Figure: Test annotation with expected parameter.
In the above example, the divisionWithException() method will throw an ArithmeticException exception, since this is an expected excpetion, so the unit test will pass.

1.2 Test annotation with 'timeout' optional parameter:

The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds). The following test fails.

Example:

Create a following class in Eclipse IDE and name it as JUnitTest3

import org.junit.Test;

public class JUnitTest3
{
       @Test(timeout=1000)
       public void infinity()
       {
               while(true)
       }
}

Right click within the test method area and choose "Run As - JUnit Test"

Figure: Test annotation with timeout parameter
In the above example, the infinity() method will throw an time out exception, and the test will fail.

Friday, January 7, 2011

Starting Selenium Server

Starting Selenium Server Method1:

1. Start -> Run. Open command prompt 

Figure: Command Prompt
2. Type java -jar

3. Drag the selenium-server.jar file from the directory it is located. (see the below figures)

Figure: Location of selenium-server.jar file
Figure: After Drag and drop to command prompt
4. Press Enter button

5. Now Selenium RC is ready and you can execute selenium test cases.

6. Your command prompt should look like this.

Figure: Selenium Server