How to Manage Cookies in Selenium WebDriver

In this article we will see the functions to manage cookie of a browser.
In here I am using full c# code, I will provide JAVA code later on.

First step should be declaring the driver
private IWebDriver driver = new FirefoxDriver();

Please follow the your necessary functions from my past posts. Let's start our topic on cookie.

-To create a cookie object in c#
private Cookie myCookie = new Cookie();
There are 4 ways to create a cookie object (to give parameter)
1. (name, value)
2. (name, value, path)
3. (name, value, path,expiry)
4. (name, value, domain, path,expiry)
Parameters:
name: The name of the cookie.(string type)
value: The value of the cookie.(string type)
path:The path of the cookie.(string type)
domain:The domain of the cookie. (string type)
expiry:The expiration date of the cookie.(DateTime structure type)
Note : A cookie is Serializable(can be stored)
I use following way to declear a cookie

private Cookie myCookie = new Cookie("name", "value", "Domain", "Path", expDate);
Note : for expDate, I use static to use it all around 
private static DateTime expDate = new DateTime(2012,12,25);

-To add our own cookie(in here myCookie) 
driver.Manage().Cookies.AddCookie(myCookie);

-To Delete all cookie
driver.Manage().Cookies.DeleteAllCookies();

-To Delete one Specific cookie(in here myCookie)
driver.Manage().Cookies.DeleteCookie(myCookie);

-To Delete one Specific cookie whose name(string) is "shantonu"            driver.Manage().Cookies.DeleteCookieNamed("shantonu");

-To Get the cookie whose name(string) is "shantonu"             driver.Manage().Cookies.GetCookieNamed("Shantonu");
Note : This will give us an Cookie type object as output

-To know how many cookie is present
driver.Manage().Cookies.AllCookies.Count;
Note : This will give us an integer(the number of cookie) as output

-To get the Index value of one Specific cookie(in here myCookie)            driver.Manage().Cookies.AllCookies.IndexOf(myCookie);
Note : This will give us an integer(Zero Based Index Value) as output.

-To Check one Specific cookie(in here myCookie) is present or not            driver.Manage().Cookies.AllCookies.Contains(myCookie);
Note : This will give bool result ( true or false)

-To Get/set all cookies defined for the current page.
driver.Manage().Cookies.AllCookies;
Note : This will give you(or you have to provide) a collection of cookie objects from current page.

From myCookie object we can get some information through its properties.
-To Get the domain(as string) of the cookie.
myCookie.Domain;

-To Get the expiration date(as DateTime) of the cookie.
myCookie.Expiry.Value;

-To Get the Name(as string) of the cookie.
myCookie.Name;

-To Get the Path(as string) of the cookie
myCookie.Path;

-To Get a value(bool) to know whether the cookie is secure.
myCookie.Secure;

-To Get the Value(as string) of the cookie.
st = myCookie.Value;


...Thanks...:)

How to manage browsers in Selenium WebDriver



In this article we are going to see the functions for controlling a web browser
I am using C# code(JAVA will given later on) with dot net web driver.

-Maximizing Browser :
driver.Manage().Window.Maximize();

-Keep browser to specific position of the screen :
driver.Manage().Window.Position = new System.Drawing.Point(10,5);

In here (x,y)=(10,5)(distance from left upper corner)

-Keep browser in fixed size resolution :  
driver.Manage().Window.Size = new System.Drawing.Size(640,480);

In here 640 is width and 480 is height.

-Open an URL in browser’s current tab:
driver.Navigate().GoToUrl("Site URL");

-Open Previous URL in browser’s current tab from the history (like as back button of the browser) :

driver.Navigate().Back();

-Open Next URL in browser’s current tab from the history (like as Next button of the browser):

driver.Navigate().Forward();
- Refresh current page
driver.Navigate().Refresh();
  
-To start Firefox browser with specific profile( i.e.- Tester creates new profile or use default profile to test for default settings and initiations.) 
private static FirefoxProfile myprofile = new FirefoxProfile("path of the profile directory");
private IWebDriver driver = new FirefoxDriver(myprofile);
In my case the path was @"C:\Users\shantonu\AppData\Local\Mozilla\Firefox\Profiles"

-To get  the source of the page last loaded by the browser
driver.PageSource
Note :  This will give page source string. We can compare with tested html.



These are some mostly use functions. I will add more incrementally

... To be continued...

Thinking in Selenium WebDriver way for testing

In this following article we are going to learn the basic Test Strategy(the way) to test a web site element using selenium web driver.
First , we need to know what selenium web driver does. Selenium web driver fully controls the a browser , get the web elements, get data, get all the actions. Selenium web driver is used for functional test automation (mostly data validation). It also provides consistency among different browsers. Web Driver uses a different underlying framework from Selenium’s javascript Selenium-Core. It also provides an alternative API with functionality not supported in Selenium-RC. WebDriver does not depend on a javascript core embedded within the browser, therefore it is able to avoid some long-running Selenium limitations.
Before starting, we have to determine what to test, what to automate.
A web site may have different type of elements(details in my previous post).
For a UI web element when we will attempt to test we need to think about the scopes by which we can see the element is full functional.
For that if we check
-An element is present in a proper place on the page
-A text is present on the page(with proper style).
OR both -A text is at a specific place on the page

-The element is rendered properly in popular browsers.
-The element is Image(if it has) is proper size as well as position
-The HTML is ok
-The backed functionality is ok.
If a text or an image is present on a page to any location, we can easily check that by verification (when contents are frequently changing). If contents ate fixed, we can use assert for specificity.
For Image specificity, we will use Xpath for all example, as it is most useful and commonly used. In Xpath, we don’t have to have a suitable id or name attribute for the element you wish to locate. We can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.
(Note : XPath is the language used for locating nodes in an XML document, we will post a different post on describing XPath)
For better understanding, lets say, we need to check for a combo UI element. We may think in following way,
-Is the combo is ok to see( people can click & see the items).
-Is all elements are listed properly( element data and order)
-Is any position element is selectable
-After selection, can user gets the data to work with
-Is the CSS is ok for the particular styled combo.
-Is the combo Name is proper
-Is the combo position is correct.
-If the combo items are coming from database, is that the data collection ok.
-if Ajax/JavaScript loads items, are they are ok
-If items comes from external source, the time that needed for loading the items is ok
-If there is any service associated with the elements, are they are coming in proper way.
This is just an example to way of thinking in web driver way. Incrementally I will provide that way for all popular web elements.

What are the major web elements?


In this following article we are going to know about different type of elements may present in any website.
Why we are doing this? We are going to learn selenium web driver for every element in later posts and this post will help us to know about different type of web elements.

Mainly website elements can be distinguished in two types, Front End Elements & Back End elements.


A. Front End Elements: Mostly a website can be described having a front end and back end. Typically a front end is what we can see, back end is what you can’t. Good front-end components include the following:
-The navigation structure: The navigation structure is the order of the pages, the collection of what links to what. Usually it is held together by at least one navigation menu.
-The page layout: This is the way things appear on the page. (i.e.- menu position)
-Logo: A good website has a unifying graphic around which it is built. The graphic represents a company or organization. It often sets up the color scheme and the style elements used throughout.
-Images: Photos, graphics, navigation bars, lines and flourishes, animations can all be placed on a website to bring it to life.
-Content: The internet began as a method of sharing information. As it evolved into the World Wide Web, it became rich with all kind of media. But Text is still the primary to communicate. Well written internet-ready text is a special kind of text. Usually the information is broken into readable chunks. It is formatted to be easily scanned, and it is often optimized for search engines as well as human eyes.
-Graphic Design: Many of the elements of graphic design have been described, such as the logo, the navigation menus, the layout, images, etc. But Graphic Design is more than the sum of these parts. It is the overall look and feel the website will have as a result of proper use and integration of all these elements. A website with bad graphic design is usually obvious to everyone except the person who put it together. But doing graphic design well takes a special combination of talent, skill, and education.

B. Back End elements. Static sites are growing rarer as the internet becomes an ever greater part of people’s lives. Modern sites are searchable, contents may be changeable, may provide more information on request ,may have collaboration among users, may have central system to update contents. A lot of websites are data driven (web pages are actually created on the fly in response to the specific needs of the user). All of these functional elements are called back-end elements. In some cases there are many different ways to do the same thing. These are some back end elements’ description.
-CMS(Content Management System): This is the ability to update your website without having to directly edit the html.(i.e.- Joomla)
-E-Commerce: People are able to purchase items, safely process credit-card transactions over the internet using this system.
-Shopping Cart: This is just a way for users to pick out different items and make a single purchase at the end of the process(upgrade system of E-Commerce, i.e.-open cart).
-Site Search: It is a simple facility to find anything among the content of a website to help users to find necessary things in faster way.
-Blog feature: Blogs can be done independently, or as part of your website specialized for expressing opinion or comments on specific topic.
-Image-rotation: Continuous changing images, Image rotation, Changing Image view style can be a part of a website.
-Bulletin Board: A bulletin board allows people to post up messages on a topic.
-Chat Room: Chat Room allows users to comment back and forth in real time.
-Contact forms: Contact forms are a starting point for interaction among visitors and the authority of any website.
-Referral forms: If someone likes your site, and has an easy, one-push way to notify her friends, you’ve turned your visitors into salespeople (doing publicity for your site).
-Newsletter registration: Newsletters keep you getting potential clients as well as update your current clients about your new products, services, or campaigns(content that is updated periodically).
-Online databases: Databases allow us to store, sort, search through, and display large amounts of information. Online databases bring this technology seamlessly to the Web.
-Protected sections: To avoid vulnerabilities a website may have a section. i.e.- password protected sections.
-Downloadable files: Site Contents may be downloadable from websites. This is an easy way to distribute files all over the world.
- Multi-media. Photo-tours, video-clips, sound-clips, slide presentation, Research papers all can add to the experience if they are well matched to the type of site and profile of the target audience.
-Security. If you are passing information online that is not meant for everyone, then you want to ensure you have the right level of security (for Trade secrets, proprietary programming, client credit card numbers, and every imaginable piece of personal data).
-Online Promotion. Not exactly part of the website, but often part of the design as well as the activity surrounding its launch is the online promotion.
Other components : Some elements that are essential to a website aren’t properly described as either front-end or back-end components. Still, a website doesn’t work right without them.
-Hosting. Hosting is where your website is physically located. On a server, somewhere, are a set of files that are transmitted to user computers when they call your name.
-Domain Name. This is the address. When someone asks to see your website, they put this address into the internet, and your site is served up to them.

...To be continued ...

Basic Functions in Selenium IDE

In this following article, we are going to learn some basic commands for working with web elements.From my previous posts, we know how to have setup and have a basic recording technique in selenium IDE.
In here we will discuss about popular functions of Selenium IDE and Selenium Web driver.
Popular functions are :
open : It is used for opening a URL.
Click : It performs a click operation
clickAndWait : It performs a click operation and waits to load
verifyTitle : verifies an expected page title. We use it for checking that the browser is displaying the expected page
assertTitle : It will check the title by compare.
Note: We may be confusion, why assert is differ from verify. Mainly verify used for running the test cases though they might fail, but assert used mainly for stopping the test case on failed condition.(we will see in the example)
verifyTextPresent : verifies expected text is somewhere on the full page.
verifyElementPresent : verifies an expected UI element(elements are described below), as defined by its HTML tag, is present on the page.
verifyText : verifies expected text and its corresponding HTML tag are present on the page.
verifyTable : verifies a table’s expected contents(with html).
waitForPageToLoad (clickAndWait) :This is not actually using for test purpose. Mostly it is used to keep wait for execution (wait for new page loading).
waitForElementPresent : wait for execution until an expected UI element, (HTML tag), is present on the page.

When we start selenium IDE in Firefox, we get those while right clicking mouse or, In the command selection combo in Selenium IDE.

Testing Web Service(SOAP/XML-RPC) using JMete

 
In this article, we are going to see a very simple web service testing procedure using JMeter.
Scenario : We are going to test a web service where the service provide us data . To be specific, the service link is http://www.webservicex.net/periodictable.asmx, the Data action link http://www.webservicex.net/periodictable.asmx?op=GetAtoms. The functionality of the service is to provide atom names in tabular format. As we are testing the data service, we can not see the HTML table, but if we can get the data, that will solve the purpose of Testing data service. 
So, if we follow those steps, we can see a very simple test for web services.

Step 1. Run JMeter(jmeter.bat) from stored location “apache-jmeter-2.8\bin”

Step 2. Create a Thread Group( right click test plan –>Add –> Thread Group) and rename as SOAPThread(I am using this name).

Step 3 : Put Number of Threads(User) as 20( I am using ) and Ramp-Up Period as 2( I am using)

Step 4. Save the Thread Group. 

Step 5 . Add a Sampler “SOAP/XML-RPC Request” under your thread group
 

Step 6. Add "http://www.webservicex.net/periodictable.asmx" as the URL and "http://www.webserviceX.NET/GetAtoms" as the Send SoapAction
 

Step 7 . Add This following request as “SOAP/XML-RPC Data”(link or Drive)
 

So, there might be a question , where I get the this XML code. It’s simple, from the service link (http://www.webservicex.net/periodictable.asmx) , you will get the GetAtoms function. 
 
If you click GetAtoms , then you will get the function definition of different  XML format(http://www.webservicex.net/periodictable.asmx?op=GetAtoms). I use SOAP 1.1 format
 

Step 8. Add Listener to your thread group(I have added view result tree, view result in table)
 

Step 9 . Now, Run the Thread Group from Menu. 
Its done. We have make and run the service test using jmeter, lets observe the results.
Here is the Request of a sample under tree listener.