Tuesday, 25 August 2009
Working with Cookies and caches Using WATiN 2.0
1. Clear cookies /caches
2. Get and set cookies
1. Clear cookies/caches
This functionality/method we can call any time during the live instance of an IE.
We need to call this method just after creating new instance of an IE.
Example: below code would clear cookies/caches just after creating new instance of an IE.
using(IE ie = new IE())
{
ie.ClearCache;
ie.ClearCookies;
ie.GoTo(“http://jawedmblogspot.com”);
}
Caution: if we call this method at later moment then the open instance of an IE still might have cached items in memory. To avoid this situation we need to use new IE.ReOpen() method. This method would close the previous open Instance and reopens new IE instances which solve our problem.
2. Get and set cookies
To manipulate Cookies and/or to read cookie information, IE.GetCookie() and IE.SetCookie are added.
using(IE ie = new IE("http://jawedm.blogspot.com"))
{
string setvalue = "test-cookie=abc; expires=Wed, 01-Jan-2020 00:00:00 GMT";
ie.SetCookie("http://1.watin.com/", setvalue);
string getvalue = ie.GetCookie("http://1.watin.com/", "test-cookie");
Assert.AreEqual("test-cookie=abc", getvalue);
}
for more information you can visit below website
http://watin.sourceforge.net/releasenotes-1-2-0-4000.html
Thanks!!!!
Monday, 24 August 2009
Bugs Status Report From Digite Using WATiN
even to get the status we don’t need access of digite :)
All this just a Button click away :)
2. Bugged logged date wise.
We have hosted this web application at http://172.16.200.127/BugStatus/Default.aspx (Access from Aditi LAN only) location to get access from anywhere and by anyone :)
Fig 2. Report sent through Email.
Thanks,
Md.Jawed
Friday, 21 August 2009
Get the snapshots of web page using WATiN
This is very good feature added in WATiN tool. By using this we can get the snapshot of the web page where our test cases failed and that snapshot we can store at specify configurable location. Latter we can see the status of web page on failed test cases through snapshot.
Best part is that while sending automated test cases report we can attach failed test cases snapshots also with email to keep clarity about behavior of web page in failed condition.
Below is method to get snapshots of web page:
//Start of method
public void GetSnapshotUsingWatiN()
{
//Specify the location where we want to save all the snapshots.
string location = "c://ruby/";
//Open new instance of Internet Explorer
IE ie=new IE();
//Go to any specify website
ie.GoTo("http://jawedm.blogspot.com");
//Capture the snapshot of open web page and save it at mention
location.
ie.CaptureWebPageToFile(location +"/SnapshotUsingWatin.jpg");
}//end of method.
Tuesday, 18 August 2009
Automatic Birthday Scrap to Orkut friends Using WATiN
By using this application Orkut users can send birthday wishes scrap to their friends without fail and even without logging into Orkut site. It is just like eliminating human interaction with website while sending birthday scrap to Orkut friends without missing nearest, dearest friend’s birthdays. Just by running this application you can finish this work within seconds. You can set time to run this application on daily basis at predefined time.
If we run this console application, we will see an Internet Explorer browser opening and automating the entire manual process. Isn't that cool? :)
Background
I remember those days when I used to login in to Orkut to check for friends birthdays. if it’s there then I used to scrap them with best wishes. But the most difficult part is to remember friends birthdays (that’s my experience). Suppose we were not able to open Orkut on a particular day(that happen's to be my friends birthday) then it would be like we missed wishing friends. If next day we get a chance to login to orkut,then we need to send wishes with belated word :(
Then one day I thought we need to have any application which can remove our painful situation. The application should be in my control, in my hand and can take care about all this stuff. It’s like computer is working in place of me.
After struggling for few hours with my thinking, idea and knowledge I came up with an idea that why cant I develop an application which can do as per my thinking. That’s great :)
So, I would love to present an application which is replica of my knowledge/idea in c#, .Net and WATIN.
What is WATiN
WATIN, pronounced "What-in", is an acronym standing for "Web Application Testing in .Net". WATiN is a toolkit used to automate browser-based tests during web application development. This automated test tool uses the C# language to drive the Internet Explorer web browser. To know more about WATiN tool kit please refer http://watin.sourceforge.net/ link.
Using the Code
To interact with Orkut web site I have used WATIN tool as API interaction between my code and orkut web site. For programming language I used C# with .net 2.0 and I hope that you guys are familiar with WATiN.
I would prefer to explain the codes line by line so that we will get idea about How We can Use WATiN for Automation purpose as well as for other purpose like Interacting with other web site just like an API.
Flow chart: (see fig 1.)
1. Open new instance of Internet explorer (can be in visible mode)
2. Go to http://www.orkut.com/
3. Login with predefined emailid and password.
4. Move to home page.
5. Check that any friends birthday fall today if yes then go to step 6 else step 10
6. Click at link LEAVE SCARP and go to selected friend’s scarp book.
7. Type predefined BIRTHDAY MESSAGE/WISHES and post that scrap.
8. Move back to home web page.
9. Further check for the next birthday date, if it fall today repeat step 6 to 8.
10. Click on logout link of Orkut web site.
11. On successful logout close the browser.
Now let’s put the above steps in code.
1. Open visual studio and select New project as Console application.(I assume that you are
familiar with Visual Studio2005)
2. Go to ADD reference and add WATiN.core.dll and Nunitframework.dll in your project.
3. ADD below namespace in you code file to access WATiN classes and method.
using Watin.core;
using NUnit.Framework;
4. I m using XML file to store login Emailid, Password and birthday wish. At first I thought
using APPconfig for Data information but I changed my mind to XML because XML is very
easy to maintain and easy to read through the code and best part is that anybody can easily
use this.
5. So first we will write code to read login Emailid, Password and Birthday wish
The xml file would look like this
------------- UserInputData.xml-------------------------
xml version="1.0" encoding="utf-8"?>
<root>
<UserEmailId>
<Value>jawed_ace@yahoo.com<Value>
<UserEmailId>
<UserPassword>
<Value>myPassword<Value>
<UserPassword>
<BirthdayMsg>
<Value>Wish you happy bithday.<Value>
<BirthdayMsg>
<root>
--------------------------------------------------------
//Start Reading Xml for Input data
//String variable to store data from XML file.
string[] UserInput=new string[3];
//Get the Current directory path
string xmlPath = Environment.CurrentDirectory;
//Get the xml path to read output
xmlPath = xmlPath.Replace("bin\\Debug","UserInputData.xml");
XmlTextReader reader = new XmlTextReader(xmlPath);
reader.Read();
int i=0;
/*start reading the XML file for UserEmailid,UserPassword and Birthday wishes.*/
while (reader.Read())
{
switch (reader.NodeType)
{
//Display the text in each element.
case XmlNodeType.Text:
//Console.WriteLine(reader.Value);
//store the value in string
UserInput[i] = reader.Value;
i++;
break;
}
}
//Stop reading from xml file
6. Now our actual work/Code will start. In this section we will derive internet explorer using
WATiN through login in to Orkut.
We will create a new instance of internet explorer in invisible mode. The piece of code would be
look like
//Make Internet Explorer to run in invisible mode.
IE.Settings.MakeNewIeInstanceVisible = false;
//Open an Instance of IE
IE ie = new IE();
http://www.orkut.com/ in address bar and do the action of go.
// Type www.orkut.com in browser.
ie.GoTo("https://www.orkut.com");
ie.WaitForComplete();
//VERIFY THAT USER HAS NOT ALREADY LOGIN INTO ORKUT
/iF YES THEN cLOSE THE PREVIOUS SESSION AND START NEW SESSION.
Assert.IsTrue(ie.Link(Find.ByText("Logout")).Exists);
ie.Frame("orkutFrame").Link(Find.ByText("Logout")).Click();
ie.WaitForComplete();
8. Now on Orkut Home page we will provide Emailid and password to corresponding emailid
field and password field. These values are already we read from XML.
I think rest of the code is self explanatory to understand as I have included comments also
to get the clear picture of the code.
using System.Collections.Generic;
using System.Text;
using WatiN.Core;
using NUnit.Framework;
using WatiN.Core.Interfaces;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;
using System.Data;
using System.Web;
//This Class would send out Birthday Scrap to your friend
//Author: Md.Jawed
namespace BirthDayScrapOrkut
{
class Program
{
//A common use for a cross-process Mutex is to ensure
//that only instance of a program can run at a time.
//Mutex provides the same functionality as C#'s lock statement,
//making Mutex mostly redundant.
static Mutex mutex = new Mutex(false, "http://jawedm.blogspot.com");
[STAThread]
static void Main(string[] args)
{
{
IE.Settings.MakeNewIeInstanceVisible = false;
//Start Reading Xml for Input data
//String variable to store data from XML file.
string[] UserInput=new string[3];
//Get the Current directory path
string xmlPath = Environment.CurrentDirectory;
//Get the xml path to read out put
xmlPath = xmlPath.Replace("bin\\Debug", "UserInputData.xml");
XmlTextReader reader = new XmlTextReader(xmlPath);
reader.Read();
int i=0;
//start reading the XML file for UserEmailid,UserPassword and Birthday wishes.
while (reader.Read())
{
switch (reader.NodeType)
{
// Do some work here on the data.
case XmlNodeType.Text: //Display the text in each element.
UserInput[i] = reader.Value;
i++;
break;
}
}
//Stop reading from xml file
//Open an Instance of IE
IE ie = new IE();
// Type www.orkut.com in browser.
ie.GoTo("https://www.orkut.com");
ie.WaitForComplete();
ie.WaitForComplete(300);
//VERIFY THAT USER HAS NOT ALREADY LOGIN INTO ORKUT
//iF YES THEN cLOSE THE PREVIOUS SESSION AND START NEW SESSION.
try
{
Assert.IsTrue(ie.Link(Find.ByText("Logout")).Exists);
ie.Frame("orkutFrame").Link(Find.ByText("Logout")).Click();
ie.WaitForComplete();
}
catch (AssertionException logAex)
{
//
}
catch (WatiN.Core.Exceptions.ElementNotFoundException logWex)
{
}
//Type Your emailid in to emailid textbox.
ie.TextField("Email").TypeText(UserInput[0]);
//Provide your Password in password textbox.
ie.TextField("Passwd").TypeText(UserInput[1]);
//Click on signIn button to login into orkut.
ie.Button(Find.ByName("signIn")).Click();
ie.WaitForComplete();
/*get the URL of browser after click on sign In button. To verify that Login is successful or not*/
string VerifyUrl = ie.Url;
ie.WaitForComplete();
//verify that login as successful. return true
if (VerifyUrl == "http://www.orkut.co.in/Main#Home.aspx")
{
//Check that Birthday box exist on home page.
bool res = ie.Frame("orkutFrame").Div("mbox").Table(Find.ByIndex(2)).Exists;
if (res)
{
//Remember the number of user exists in Birthday Box.
int numBirthdayFriend = 0;
{
Div birthDayDiv=ie.Frame("orkutFrame").Div("mbox").Table(Find.ByIndex(2)).Div(Find.ByClass("boxgrid")).Div(Find.ByIndex(numBirthdayFriend));
{
Assert.IsTrue(birthDayDiv.Div(Find.ByIndex(1)).Exists);
//Check that anyone have birthday today or not
Assert.AreEqual("leave a scrap", birthDayDiv.Div(Find.ByIndex(1)).Link(Find.ByIndex(1)).Text);
//Click on leave scrap option to leave a scrap to birthday buddy
birthDayDiv.Div(Find.ByIndex(1)).Link(Find.ByIndex(1)).Click();
ie.WaitForComplete()
/ /Very that we have landended at friend’s Scrap book page.
Assert.IsTrue(ie.Frame("orkutFrame").TextField("scrapText").Exists);
//Type a Scrap to Birthday buddy.
ie.Frame("orkutFrame").TextField("scrapText").TypeText(UserInput[2]);
//Post an Scrap.
ie.Frame("orkutFrame").Link(Find.ByText("post scrap")).Click();
//Click on Home link to return back to Orkut home page after leaving scrap into friend’s scrap book.
ie.Frame("orkutFrame").Link(Find.ByText("Home")).Click();
}
catch(AssertionException Aex)
{
Console.WriteLine(Aex.Message);
}
catch(WatiN.Core.Exceptions.ElementNotFoundException Wex)
{
//
}
numBirthdayFriend = numBirthdayFriend + 3;
}
}
else
{
Console.WriteLine("No Birthday Table Exist for the login user");
}
//Loging out from orkut.
ie.Frame("orkutFrame").Link(Find.ByText("Logout")).Click();
ie.WaitForComplete();
//Close the instance of Browser.
ie.Close();
}
else
{
//Sorry Login Unsuccessful.
ie.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}//end of the code
My learning from this article was that WATiN is not just for Automation purpose. We can use this for API interface also. And more important is that now onwards this application is going to take care about our Orkut needs.
Conclusion
After reading this article I hope that we should know how we can use WATiN framework to test web applications and as well as how we can utilize it for API purpose. I think that we can do intensive UI and functional testing with it and of course for API use and best part is by using this application we can avoid login in to Orkut for birthday wishes.
Future plan
I would love to put the same article with setup file on code project.
http://www.codeproject.com/KB/applications/OrkutBirthdayScrap.aspx
You can download Source code from below link.
http://www.4shared.com/file/129162544/9ffcdb85/BirthDayScrapOrkut.html[^]
and for setup file you can visit below link.
http://www.4shared.com/file/129160071/687976a9/OrkutBirthdaySetUp.html[^]
Points of Interest
Successive clicks of EXE open multiple browsers and command windows (Timeout exception occurs later). To avoid this issue: I have used the concept of Mutex. A common use for a cross-process Mutex is to ensure that only instance of a program can run at a time. Mutex provides the same functionality as C#'slock
statement, making Mutex mostly redundant.Upcoming Application On my blog
2. and i have developed an Application for Automation using WATiN. my self called that application as "An Automated functional graphical user interface testing application using WATiN". currently i am using this application for automation in my project.
Md. jawed
(M) +91 9986415006 (D) +91 6610 5476
Testing is an Art to execute your Knowledge, Skill and Desire.
Aditi Technologies – Think Product http://www.aditi.com/