Monday 12 September 2011

Using Multiple @Test TestNG Annotations

Below is the script which is using multiple @Test TestNG Annotations..

import static org.testng.Assert.assertEquals;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import org.testng.annotations.*;
public class Second {
  
    public static Selenium selenium;
  
    @Test
      
        public static void sample_1() throws InterruptedException {
      
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
      
        selenium.start();
      
        selenium.open("http://www.google.co.in");
      
        System.out.println(a);
      
        selenium.type("id=lst-ib", "Testing");
          
        selenium.click("name=btnG");
      
        Thread.sleep(1000);

        }

    @Test
        public static void sample_2() throws InterruptedException  {

        assertEquals(selenium.getTitle(), "Testing - Google Search");

        Thread.sleep(1000);

        selenium.click("css=em");

        Thread.sleep(1000);

        assertEquals(selenium.getTitle(), "Software testing - Wikipedia, the free encyclopedia");

        Thread.sleep(3000);

        selenium.click("css=#ca-talk>span>a");      

        }
    }


Sample JUnit Test Suite

Test Suite using JUnit....
 

Test1.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.Selenium;

public class Test1 {

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("URL");
//* Do the Operation

*/

}
}

/***********************
Test2.java
***************/

public class Test2 {

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("URL");
//* Do the Operation

*/

}
}


/***********************

Finally The Test Suite

***************/

package com.Testscripts;
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( Test1.class);
nag.addTestSuite( Test2.class);
return nag;
}

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

}
}
------------------------------------------------------------
Run the TestSuite.java script ...it will automatically run the tests(Test1 and Test2)

Selenium RC + JUnit Sample Script

Below is the sample script 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.Selenium;

public class yahoo{

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();
    }
}

Different flavours of browsers

  *pifirefox
  *piiexplore
  *firefox
  *mock
  *firefoxproxy
  *chrome
  *iexploreproxy
  *iexplore
  *firefox3
  *safariproxy
  *googlechrome
  *konqueror
  *firefox2
  *safari
  *piiexplore
  *firefoxchrome
  *opera
  *iehta
  *custom

Export to excel sheets

Do you want to write results/export run-time Data  to excel sheets ?

Below is the sample script ...

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class WriteExcel2 {
public Selenium selenium;

public static void main(String arg[]) throws IOException, BiffException, RowsExceededException, WriteException{

//Create File output stream object

    FileOutputStream fo=new FileOutputStream("C:T2.xls");
    WritableWorkbook wwb=Workbook.createWorkbook(fo);
    WritableSheet ws=wwb.createSheet("Results", 0);

// define label and asign values

    Label l = new Label(1, 1, "hi");
    Label l1 = new Label(2, 1, "Result");

//pass values to respective cell

    ws.addCell(l);
    ws.addCell(l1);   

// Close the objects

    wwb.close();
    fo.close();
}

Thursday 8 September 2011

Selenium-RC components

Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as an HTTP proxy, intercepting and verifying HTTP messages passed between the browser and the AUT.
Client libraries which provide the interface between each programming language and the Selenium-RC Server.
 
  Client libraries communicate with the Server passing each Selenium command for execution. Then the server passes the Selenium command to the browser using Selenium-Core JavaScript commands. The browser, using its JavaScript interpreter, executes the Selenium command. This runs the Selenese action or verification you specified in your test script.

Selenium-IDE Running Test Cases



  • Run a Test Case
  • Run a Test Suite
  • Stop and Start
  • Stop in the Middle
  • Start from the Middle  
  • Run Any Single Command

Selenium -IDE Building Test Cases

  • Recording
  • Adding Verifications and Asserts With the Context Menu
  • Editing
  •  Insert Command
  •  Insert Comment
  • Opening and Saving a Test Case /Test Suite

Selenium-IDE

The Selenium-IDE (Integrated Development Environment) is the tool you use to develop your Selenium test cases. It’s an easy-to-use Firefox plug-in and is generally the most efficient way to develop test cases.
  Selenium IDE benefits
Easy record and playback
Easy to modify recorded test cases
Easily extensible
Pause and step-through capability during playback
Tests are saved in standard HTML format by default but can be saved in any format (java, c#, xml, ruby, etc.)
Requires minimal training

Selenium Components

 
Selenium-IDE:  is a Firefox add-on that makes it easy to record and playback tests in Firefox. You can even use it generate code to run the tests with Selenium Remote Control.
Selenium Core: is the original JavaScript-based testing system. It's now used primarily as a component of Selenium Remote Control, but it can also be used as a pure testing system for different browsers.
Selenium-RC (Remote Control):  is a client/server system that allows you to control web browsers locally or on other computers, using almost any programming language and testing framework.

Tuesday 6 September 2011

Framework Overview

Automation testing requires a well-defined approach, based on a comprehensive framework, in order to reap maximum benefits.
A framework is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library functions,Object Repository,Global Variable and reference documentation in a single source folder.
There are various frameworks available for automation, such as:
• Test Script Modularity Framework
• Test Library Architecture Framework
• Data-Driven Automation Framework
• Hybrid Automation Framework
• Keyword-Driven Automation Framework
Hybrid test design and test automation Framework is the most commonly implemented framework is a combination of all of the above techniques, pulling from their strengths and trying to mitigate their weaknesses.

Configuring TestNG With Eclipse

    Follow below steps to configure Selenium-RC With Eclipse :
  1. Open your desired Java IDE (Eclipse)
  2. Select the 'Install New Software' from the 'Help' Menu the following window will display

    3.Click on the 'Add' button in 'Install' window then following window will display
    4.Enter the Name as 'TestNG' and Location as 'http://beust.com/eclipse1' and click 'Ok'
    5.Select the TestNG relevant check box and click on 'Next'.
    6.It automatically download and Install the TestNG
    7.Click on 'Finish' and Restart the Java IDE (Eclipse)

Configuring Selenium-RC With Eclipse

Follow below steps to configure Selenium-RC With Eclipse :
  1. Download Selenium RC java client driver from the Selenium-HQ downloads page.
  2. Extract selenium-java-<version-number>.jar file
  3. Open your desired Java IDE (Eclipse)
  4. Create a java project.
  5. Configure Build Path with downloaded jar files to java project as references.
  6. Run Selenium server from the console to execute your tests from the Java IDE (Eclipse)

Selenium RC Server

  Installing Selenium Server
  The Selenium RC server is simply a Java jar file (selenium-server-standalone-<version-number>.jar), which doesn’t require any special installation. Just downloading the zip file and extracting the server in the desired directory is sufficient
Running Selenium Server 
Go to the directory where Selenium RC’s server is located and run the following from a command-line console.
 java -jar selenium-server-standalone-<version-number>.jar
This can be simplified by creating a batch or shell executable file (.bat on Windows and .sh on Linux) containing the command above. Then make a shortcut to that executable on your desktop and simply double-click the icon to start the server.
Note : For the server to run need Java installed and the PATH environment variable correctly configured to run it from the console. You can check that you have Java correctly installed by running the following on a console:
 java -version
If promt version number (which needs to be 1.5 or later), ready to start using Selenium RC.

Monday 5 September 2011

Key Features


In addition to standard features such as performing operations and verifications on the objects, the framework includes other sophisticated features, such as:

  1. Use of variables:

    Variables can be defined and used across the generated test script. This can be used to capture run-time values, which can be reused as input elsewhere during test execution.

  2. Conditional checking:

    Conditional constructs such as ifcan be implemented using keywords to handle different

  3. Data-driven testing:

    This framework supports data-driven testing by importing data from an external resources.

  4. Re-usability:

    The Open Source Test Automation Framework is an application independent framework that deals with all possible actions and verifications that can be performed on an object. Therefore, the code for the same object can be used across different applications. Duplication of work is minimized at every level.

  5. Increased quality:

    The scripts will be of uniform quality since they make use of the same code

  6. Greater productivity:

    The Open Source Test Automation Framework provides both qualitative and quantitative benefits for automation and it is highly productive compared to any other framework. This framework also addresses the ongoing maintenance of the test scripts in a cost-effective manner

  7. Maintenance:

    Simple modifications to the application can be easily handled in the code. The changes will be done only in the source file containing the code and the scripts need not be changed. Hence it is easy to maintain the scripts and provide cost-effective solutions for the test automation.

  8. Return on investment is high:

    Although the initial effort for building the framework is high, in the long run, the return on investment will be high because of the re-usability and optimum utilization of the tool.

Results & Reporting

Actually Selenium RC does not have its own mechanism for reporting results. Rather, it allows you to build your reporting customized to your needs using features of your chosen programming language.
In Our Framework ,We generate two report they are
  • TestNG Report In HTML Format
  • Results In Excel File
    We have different log files like:
  • client side log
  • browser side log
  • Results Eclipse console

Generic Test Reports in Java :

  • JUnit Report
  • TestNG Report
  • ReportNG Report
As our Framework built on java integrated with TestNG and Selenium Test cases are developed using TestNG Annotations then no external task is required to generate test reports. The TestNG framework generates an HTML report it support extends with ReportNG.
And also we are providing results is Spreadsheets of different flows based on various conditions by using conditional constructs such as 'if'

Sample script using TestNG

Below is the sample script which designed based on the TestNG Annotations
public class TestNG {
public static Selenium selenium;
@BeforeClass
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.open("http://www.google.co.in");
Thread.sleep(1000);
}
@Test
public static void sample_1() throws InterruptedException {
assertEquals(selenium.getTitle(), "Google");
selenium.type("id=lst-ib", "Testing");
selenium.click("name=btnG");
Thread.sleep(1000);
}
@Test
public static void sample_2() throws InterruptedException {
assertEquals(selenium.getTitle(), "Testing - Google Search");
Thread.sleep(1000);
selenium.click("css=em");
Thread.sleep(1000);
}
@Test
public static void sample_3() throws InterruptedException {
assertEquals(selenium.getTitle(), "Software testing - Wikipedia, the free encyclopedia");
Thread.sleep(3000);
selenium.click("css=#ca-talk>span>a");
}
@AfterClass
public void tearDown() throws Exception {
selenium.stop();
}
}

Data Driven Testing using Spreadsheets


Consider spreadsheet holds multiple combinations of inputs to be fed to test the application. The best practice here is to keep the data sheet in a common place, preferably in the source of current directory.
Following script designed using Jxl.jar file
public class DDT {

public Selenium selenium;

@Test
public void DataDrivenTesting()throws Exception {

FileInputStream fi=new FileInputStream("F:\PATH.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); }
}
}

Note:Below are should import to run above script
import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import com.thoughtworks.selenium.*;
import org.testng.annotations.Test;

Create connection with database and fetch the values

Following script will explain how to create connection with database and fetch the values
public class DB_Connection_SQL {

public static String url = "jdbc:sqlserver://000.000.0.000:0000;DatabaseName=TEST_DB" ;
public static void main(String [] args){

Connection con = DriverManager.getConnection(url,"UN","PWD");

// Create statement object which should use SQL statement.
Statement stmt = con.createStatement();

// Send SQL SELECT statements to the database which returns the //requested information as rows of data in a Result Set object.
ResultSet res = stmt.executeQuery( "select top 1 name from Emp" );

// Fetch value of "name" from "result" object.
String Ename = res.getString( "name" );

// Use the email Address value to login to application.
selenium.type( "userID" , Ename);
}
}
Note:Below are should import to run above script
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

UI Mapping or Object Repository

A UI map is a mechanism that stores all the locators for a AUT in one place for easy modification when identifiers or paths to UI elements change in the AUT. The test script then uses the UI Map for locating the elements to be tested. Basically, a UI map is a repository of test script objects that correspond to UI elements of the application being tested.
What makes a UI map helpful?
Its primary purpose for making test script management much easier. When a locator needs to be edited, there is a central location for easily finding that object, rather than having to search through test script code. Also, it allows changing the Identifier in a single place, rather than having to make the change in multiple places within a test script, or for that matter, in multiple test scripts.
To summarize, a UI map has two significant advantages.
  • Using a centralized location for UI objects instead of having them scattered throughout the script. This makes script maintenance more efficient.
  • Cryptic HTML Identifiers and names can be given more human-readable names improving the readability of test scripts.

There are various ways a UI Map can be implemented. Following are the few famous ways :
  • Properties File In Java
  • XML File
  • Excel Sheet
  • Java Class File(stores public String variables each variable storing a locator value)

    FRAMEWORK ARCHITECTURE