Friday, February 25, 2011

Selenium Basics

Selenium-IDE:


Selenium-IDE is the Integrated Development Environment for building Selenium test cases. It operates as a Firefox add-on and provides an easy-to-use interface for developing and running individual test cases or entire test suites. Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back. It also has a context menu (right-click) integrated with the Firefox browser, which allows the user to pick from a list of assertions and verifications for the selected location. Selenium-IDE also offers full editing of test cases for more precision and control. Although Selenium-IDE is a Firefox only add-on, tests created in it can also be run against other browsers by using Selenium-RC and specifying the name of the test suite on the command line.



Features:

  • Easy record and playback

  • Intelligent field selection will use IDs, names, or XPath as needed

  • Auto complete for all common Selenium commands

  • Walk through tests

  • Debug and set breakpoints

  • Save tests as HTML, Ruby scripts, or any other format

  • Support for Selenium user-extensions.js file

  • Option to automatically assert the title of every page





Selenium-RC:
Selenium-RC allows the test automation developer to use a programming language for maximum flexibility and extensibility in developing test logic. For instance, if the application under test returns a result set, and if the automated test program needs to run tests on each element in the result set, the programming language’s iteration support can be used to iterate through the result set, calling Selenium commands to run tests on each item.
Selenium-RC provides an API (Application Programming Interface) and library for each of its supported languages: HTML, Java, C#, Perl, PHP, Python, and Ruby. This ability to use Selenium-RC with a high-level programming language to develop test cases also allows the automated testing to be integrated with a project’s automated build environment.


Selenium TestNG with Eclipse Configuration


In order to configure selenium TestNG with Eclipse we need to follow the below steps, I have also mentioned about configuring selenium server in eclipse with the help of which we will be able to see selenium server in console bar within eclipse, the steps are as follows.



Start Eclipse:


  • Goto path where eclipse application is stored.

  • Launch eclipse by double clicking the Eclipse icon.

  • Browse the workspace directory where you want to store all your project contents.

  • Eclipse is launched now.


Creating New Project:


  • Goto to File menu select new than click on new project.

  • From displayed list select JAVA project and click next.

  • Give the appropriate project name than click next.

  • Now to add external libraries click on libraries than click add external Jars..

  • Browse the external Jars and add it.

  • Click on finish button.





External JARs:


TestNG 5.14.1:

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),functional testing,browser-compatibility testing with Selenium-RC to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).

Learn more about TestNG here:http://testng.org/doc/documentation-main.html

Now lets start implementing it.


First you need to record a script in selenium-ide,just try with a simple script and put some check points(to know how Selenium ide works visit:http://seleniumhq.org/projects/ide/)


Now convert your IDE script to any language as selenium supports multiple platforms like junit,TestNG,Python,C#.


Here i have used TestNG because of it's powerfull features like multi threading and annotaions


so convert the IDE script into TestNG format like this:





selenium-java-client-driver:


Add the selenium-java-client-driver.jar files to your project as references.

Add to your project classpath the file selenium-java-client-driver.jar.


To download refernece libraries follow the link below.


http://seleniumhq.org/download/


Configuring selenium server:

We can configure Selenium server by clicking on Run and click on 'External Tools' and click on 'External Tool Configurations' then you will get a window opened



Now browse the java .exe file and paste the path in the Location Text box


Location: C:\Program Files\Java\jdk1.5.0_06\bin\java.exe


Now browse the Selenium Server file folder and paste the path location in Working Directory Text box


Working Directory: C:\Program Files\Selenium\selenium-remote-control-1.0.1\selenium-server-1.0.1

Pass Arguments as -jar selenium-server.jar

Now click on Apply button and click on Run and you can see server running on the port 4444


Now we have to instantiate selenium server in order to run your script,so we need to create the selenium instances in our scripts. Given below is the code about how we instantiate selenium server on a particular host to launch the scripts


public class TestScript {

private Selenium selenium;

@BeforeClass

@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
public void startSelenium(String host,String port,String browser,String url) {

this.selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);
this.selenium.start();
this.selenium.open(url);
}



Add this before your code,now we have to pass values to the parameters as you can see in the above code to launch selenium server.we define parameters values in xml.here youc an see how the xml structure looks like:













Run As

Now run the Eclipse using Run as and click on Apply and Run


once your script is successfully completed then you can see the reports in your workspace directory in test-output folder.



Wednesday, February 2, 2011

Reusability of One Class in another Class in RC

Reusability

Objective:- To create a reusable script for Login and Logout which can be called in a main function

Procedure:- Here I will explain you step by step to create reusable Login and Logout script.


Step 1:

Create Login script As shown Below.

@Test(alwaysRun=true)
public void testlogin() {

selenium.type("Email", "mailid");
selenium.type("Passwd", "Passwd");
selenium.click("signIn");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("//*[@id=\":r7\"]"));
}


Step 2:

Create a code to invoke(start) selenium-server under “@BeforeTest” annotation as shown below.

@BeforeClass

@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})

public void startSelenium(String seleniumHost, String seleniumPort, String browser, String webSite) {

this.selenium= new DefaultSelenium(seleniumHost, Integer.parseInt(seleniumPort), browser, webSite);

this.selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true,captureNetworkTraffic=true");

this.selenium.open(webSite);
}


Step 3:

Create Logout script in same Login script using “@AfterClass” and “@Test” annotation as shown below.

@AfterClass(alwaysRun=true)
@Test
public void testlogout() throws InterruptedException {
selenium.click("//*[@id=\":r7\"]");
selenium.waitForPageToLoad("30000");
}


Step 4:
Create a script for stopping selenium-server and use it under “@AfterClass” annotation and use “dependsOnMethods” so that selenium-server should not stop before executing Logout method, as shown below.


@AfterClass
(dependsOnMethods="testlogout")
public void stopSelenium() {
this.selenium.stop();
}


The entire Reusable script will appear as shown below.

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

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

public class CopyOfGmail_login extends SeleneseTestBase{
Selenium selenium;

@BeforeClass

@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
public void startSelenium(String seleniumHost, String seleniumPort, String browser, String webSite) {
this.selenium= new DefaultSelenium(seleniumHost, Integer.parseInt(seleniumPort), browser, webSite);
this.selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true,captureNetworkTraffic=true");
this.selenium.open(webSite);
}

@Test(alwaysRun=true)
public void testlogin() {

selenium.type("Email", "");
selenium.type("Passwd", "");
selenium.click("signIn");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("//*[@id=\":r7\"]"));
}








@AfterClass(alwaysRun=true)
@Test

public void testlogout() {
selenium.click("signout");
selenium.waitForPageToLoad("30000");

}
@AfterClass
(dependsOnMethods="testlogout")
public void stopSelenium() {
this.selenium.stop();
}
}



Step 5:

Now Create a main script in a new Class. In Class use “extends” method for calling Login script, also use “dependsOnMethods=LoginMethodName” and”alwaysrun=true”,As shown below

dependsOnMethod: Test will not be executed until dependant Test get executed

alwaysRun=true: So that Test should not stop if particular method fails.


Lets take an example for Gmail application the script will be like this.

import org.testng.annotations.Test;


public class CopyOfGmail_Function extends CopyOfGmail_login{

@Test(dependsOnMethods="testlogin",alwaysRun=true)
public void testinbox() {
selenium.click("link=Starred");
selenium.click("link=Sent Mail");
selenium.click("link=Drafts ");
selenium.click("link=Personal");
verifyTrue(selenium.isTextPresent("There are no conversations with this label."));
selenium.click("//div[@id=':co']/div[1]/div");
selenium.click("link=Inbox");
verifyTrue(selenium.isElementPresent("//div[@id=':co']/div[1]/div"));

}

}








Step 6:

we need to create a xml file to run our scripts, it can be shown below.

suite name="My suite" thread-count="1"

parameter name="seleniumPort" value="4444"
parameter name="seleniumHost" value="localhost"
parameter name="webSite" value="http://www.gmail.com"
parameter name="browser" value="*iexplore"
test name="Simple example"

classes

class name="GmailLogout"

test

suite

These all things are to be written using the < > braces.

PopUp Handling using Selenium RC and Testng

PopUp Handling using Selenium Rc


Objective:- To create a script for handling popup using Selenium TestNG.


Procedure: As follows

Step 1: Create a script for a website which consists of popup like http://www.blazonry.com/javascript/windows.php

Step 2: The main functions we use to handle popup are

selenium.selectWindow(“”);

selenium.Focus();

selenium.selectWindow: This is used for selecting the window or popup which appears by clicking.

selenium.Focus() : This is used for focusing on the window that means the control shifts to the window (popup) from the main page.

selenium.selectWindow(“null”) : This command is used for getting the control from popup window to the neutralize . So that it can focus on other window.


Here is the example for popup handling


@Test
public void popup() throws Exception {

selenium.open("http://www.blazonry.com/javascript/windows.php");

selenium.click("//p[4]/a/b");

selenium.waitForPopUp("Window1", "30000");

selenium.selectWindow("name=Window1");

selenium.click("link=Close this Window");

Thread.sleep(50000);

selenium.selectWindow("null");

selenium.click("//p[10]/a/b");

selenium.waitForPopUp("Window2", "30000");

Thread.sleep(30000);

selenium.selectWindow("name=Window2");

selenium.click("link=Click Here to Change the Focus");

Thread.sleep(50000);

selenium.selectWindow("null");

selenium.click("link=close Window 2 from this link");

Thread.sleep(30000);

}

And the selenium file attached should has the following code:



suite name="My suite" thread-count="1"

parameter name="seleniumPort" value="4444"
parameter name="seleniumHost" value="localhost"
parameter name="webSite" value="http://www.gmail.com"
parameter name="browser" value="*iexplore"
test name="Simple example"

The above all code should be written with in the < > braces.





Smartgwt using Selenium

Handling SmartGWT Control using SeleniumIDE


Below is the link for Sample URL supporting SmartGWT Control.

From the above URL we have taken a scenario and recorded the script in IDE and executed it using firefox

Scenario Steps
1. Click on Demo application.
2. Click Show example button.
3. Expand General office products main node.
4. Click Account Books
5. Click any one of the item in grid displayed.
6. Edit the item under Item details and save it.

Testing using Selenium

Selenium IDE:
IDE stands for Integration Development Environment which is used for record and running the scripts means it allows us to record, edit, and debug tests.

IDE Installation:

1. Install Firefox
2. Using Firefox, first, download the IDE from http://seleniumhq.org/download/
3. When downloading from Firefox, the following window will be presented.
4. Click on [Install Now]

Configuring Selenium IDE for SmartGWT:

  1. For Setting up the Selenium IDE we have to download the Smartgwt 2.4 version which consists of Extension for the Selenium IDE for identifying the smartgwt components.
  1. Smart Client ships with a Selenium user extension Javascript file “user-extensions-ide.js” should be added to the Selenium IDE in Options--> General---> Browse the file and add to Selenium IDE.
  1. In order to create tests we need a selenium user extension file to run the scripts “user-extensions.js”, and we have to add this to the selenium using Options-->General--->Browse the file and add to Selenium IDE.
  1. There is no need to add locator manually , Selenium commands records with the SmartClient locator (scLocator)
Recorded script in Selenium IDE.

Command used:
waitForElementPresent:

This command pauses execution until an expected UI element, as defined by its HTML tag, is present on the page. And the target should be mentioned as the scLocator of the item or element which has to be displayed.

Execution Result:

Run has been completed without any failure which shows a green bar on Selenium IDE.

Sunday, June 27, 2010

SOFTWARE TESTING LIFE CYCLE

Software testing life cycle identifies what test activities to carry out and when (what is the best time) to accomplish those test activities. Even though

testing differs between organizations, there is a testing life cycle.

Software Testing Life Cycle consists of six (generic) phases:

# Test Planning,
# Test Analysis,
# Test Design,
# Construction and verification,
# Testing Cycles,
# Final Testing and Implementation and
# Post Implementation.

Software testing has its own life cycle that intersects with every stage of the SDLC. The basic requirements in software testing life cycle is to

control/deal with software testing – Manual, Automated and Performance.
Test Planning

This is the phase where Project Manager has to decide what things need to be tested, do I have the appropriate budget etc. Naturally proper planning at this

stage would greatly reduce the risk of low quality software. This planning will be an ongoing process with no end point.

Activities at this stage would include preparation of high level test plan-(according to IEEE test plan template The Software Test Plan (STP) is designed to

prescribe the scope, approach, resources, and schedule of all testing activities. The plan must identify the items to be tested, the features to be tested,

the types of testing to be performed, the personnel responsible for testing, the resources and schedule required to complete testing, and the risks

associated with the plan.). Almost all of the activities done during this stage are included in this software test plan and revolve around a test plan.
Test Analysis

Once test plan is made and decided upon, next step is to delve little more into the project and decide what types of testing should be carried out at

different stages of SDLC, do we need or plan to automate, if yes then when the appropriate time to automate is, what type of specific documentation I need

for testing.

Proper and regular meetings should be held between testing teams, project managers, development teams, Business Analysts to check the progress of things

which will give a fair idea of the movement of the project and ensure the completeness of the test plan created in the planning phase, which will further

help in enhancing the right testing strategy created earlier. We will start creating test case formats and test cases itself. In this stage we need to

develop Functional validation matrix based on Business Requirements to ensure that all system requirements are covered by one or more test cases, identify

which test cases to automate, begin review of documentation, i.e. Functional Design, Business Requirements, Product Specifications, Product Externals etc. We

also have to define areas for Stress and Performance testing.
Test Design

Test plans and cases which were developed in the analysis phase are revised. Functional validation matrix is also revised and finalized. In this stage risk

assessment criteria is developed. If you have thought of automation then you have to select which test cases to automate and begin writing scripts for them.

Test data is prepared. Standards for unit testing and pass / fail criteria are defined here. Schedule for testing is revised (if necessary) & finalized and

test environment is prepared.
Construction and Verification

In this phase we have to complete all the test plans, test cases, complete the scripting of the automated test cases, Stress and Performance testing plans

needs to be completed. We have to support the development team in their unit testing phase. And obviously bug reporting would be done as when the bugs are

found. Integration tests are performed and errors (if any) are reported.
Testing Cycles

In this phase we have to complete testing cycles until test cases are executed without errors or a predefined condition is reached. Run test cases –> Report

Bugs –> revise test cases (if needed) –> add new test cases (if needed) –> bug fixing –> retesting (test cycle 2, test cycle 3….).
Final Testing and Implementation

In this we have to execute remaining stress and performance test cases, documentation for testing is completed / updated, provide and complete different

matrices for testing. Acceptance, load and recovery testing will also be conducted and the application needs to be verified under production conditions.
Post Implementation

In this phase, the testing process is evaluated and lessons learnt from that testing process are documented. Line of attack to prevent similar problems in

future projects is identified. Create plans to improve the processes. The recording of new errors and enhancements is an ongoing process. Cleaning up of test

environment is done and test machines are restored to base lines in this stage.
Software Testing Life cycle