Saturday, 12 September 2009

How to handle JavaScript pop-up window, file download handler in WATiN

1. How to handle pop-UP using WATiN.

1.1 Suppose that we have scenario like on click of button/link etc we are calling a JavaScript function and that JavaScript function will open a HTML pop up window. To handle this the WATiN code would be something like :

//Open our main site
IE ie = new IE(“go to some URL”);
//Create an Instance of ConfirmDialogHnadler;
AlertAndConfirmDialogHandler myHandler = new AlertAndConfirmDialogHandler();
//Declare an object of HTMLDialog
HtmlDialog myModalHandler;
ie.AddDialogHandler(myHandler);
//The button that opens the HTML dialog must be clicked with NO WAIT
ie.Link(Find.ByText("Click to see a demo popup window")).ClickNoWait();
//Hook the PopUp by title/name/type etc.
myModalHandler = ie.HtmlDialog(Find.ByTitle(new Regex("MyTestPopUp")));
//Click on button which is on PopUp window.
myModalHandler.Button(new Regex("BtnSubmit")).ClickNoWait();


1.2 In this scenario through JavaScript function if we are opening a POP-Up and that pop up windows is coded in aspx. Then the WATiN code will be something like this :

//go to main window

IE ie = new IE("gotosomeURL");

//Click on link to open popup window

ie.Link(Find.ByText("open popup")).ClickNoWait();

//Hook the new popup by url/type/title etc

string url = "MYpopUp.aspx";

Regex regUrl = new Regex(url);

IE br = IE.AttachToIE(Find.ByUrl(regUrl));

//Type some text in text box which is on popup window

br.TextField("TextBox1").TypeText("MyName");

//Click on button which is on popup window

br.Button(new Regex("Button1")).ClickNoWait();


2. Download a zip file from popup window

//Open an Main website(i m assuming that you r not signout of codeproject we

IE ie = new IE("http://www.codeproject.com/KB/applications/OrkutBirthdayScrap.aspx"); //Create an Instance of FileDownloadHandler by passing open zip file name as constructor

FileDownloadHandler fileDownloadHandler = new FileDownloadHandler("BirthDayScrapOrkut.zip");

//Add this instance of FileDownloadHandler to DialogHander

ie.AddDialogHandler(fileDownloadHandler)

//User DialogWatcher to watch open filehandler windows
using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler))
{
//Click on link Download source code to open zip file
ie.Link(Find.ByText("Download source - 1.9 MB")).ClickNoWait();
//Wait for 30 seconds to handle File Download
fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(30);
fileDownloadHandler.WaitUntilDownloadCompleted(200);
}
//Remove Watcher
ie.DialogWatcher.RemoveAll(fileDownloadHandler);

HAPPY AUTOMATION..

Please leave your comments and suggestion.Thanks!!!

Monday, 7 September 2009

Debug your ASP.NET Application which is hosted on "Remote IIS Server"

A very nice article to read about how to debug an Application hosted on Remote IIS server by dear ABHIJIT JANA.

In this article ABHIJIT has explain all the aspect of debugging application like

1. Visual Studio Internal Debugger
2. Local IIS Debugging
3. Remote IIS Debugging


He has used nice figures and diagrams to explain the debugging skill.

Fig: Block Diagram for Remote debugging of ASP.NET Application with msvsmon

Few words from his Article:

The tool which is used to remote debugging of the ASP.Net application know as "Msvsmon.exe" . The Remote Debugging tool (Msvsmon.exe) is a small windows based application that Visual Studio 2005 uses for remote debugging. It has very simple UI which makes it very simple to setup and configure during remote debugging, Visual Studio 2005 runs on one computer and the Remote Debugging Tool runs on the remote computer along with the application you are debugging.

If we want to debug the application which is hosted on remote IIS, we have to start the msvsmon.exe to the remote server, and our development system is the debugger host where we will debug our code.

OverAll Summary Provided by through diagram as:



Please click here for more details.

Thanks,
Md.Jawed

Saturday, 5 September 2009

Why ApartmentState: [STA] needed at first place in WATiN?

While writing Automation code using WATiN we could observe that we need to mention ApartmentState as STA. In my initial period of time I was thinking why we need to mention STA?

On curiosity I tried to find out the reason behind it. I searched goggle, msdn and loads of blogs to get the exact cause behind it and at last I got the answer of it. Today I thought i should share my learning with others also.

STA: The Thread will create and enter a single-threaded apartment.

Let me put some point over here before moving into deep :)

1. Internet Explorer is not Thread safe we need to use STA.

2. And we are creating any instance of Internet Explorer through COM.

Now move into the deep inside ...

An apartment is a logical container within a process for objects sharing the same thread access requirements. All objects in the same apartment can receive calls from any thread in the apartment. The .NET Framework does not use apartments, and managed objects are responsible for using all shared resources in a thread-safe manner themselves.

Because COM classes use apartments, the common language runtime needs to create and initialize an apartment when calling a COM object in a COM interop situation. A managed thread can create and enter a single-threaded apartment (STA) that allows only one thread, or a multithreaded apartment (MTA) that contains one or more threads. You can control the type of apartment created by setting the ApartmentState property of the thread to one of the values of the ApartmentState enumeration. Because a given thread can only initialize a COM apartment once, you cannot change the apartment type after the first call to the unmanaged code.

Visit MSDN link for more Information.

Differences in ApartmentState between .Net 1.1 and .Net 2.0

I copied below explanation from MSDN.

In .Net 1.1 the default ApartmentState of a Thread is Unknown. A running Thread with this ApartmentState can be set to STA or MTA, but only once. Trying to set the Apartment state of a running Thread from MTA to STA (or vice versa) doesn’t have any effect, it remains MTA (or STA).

In .Net 2.0 the default ApartmentState of a Thread is MTA. Trying to reset the ApartmentState of a running Thread from MTA to STA (or vice versa) doesn’t have any effect, it remains MTA. Setting the ApartmentState to Unknown or STA instead of MTA, can ONLY be done BEFORE the Thread starts.

Happy WATiN Automation.

Please leave your comments.Thanks!!!

Tuesday, 25 August 2009

Working with Cookies and caches Using WATiN 2.0

Release of WATiN 2.0 having work around with cookies and caches like
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

Web Application: Retrieve Bugs Status/Logged Defects from Digite Using WATiN.

Now onwards getting status of bugs from digite based on CR/WR wise and/or Date wise would not be more painful and time consuming .
even to get the status we don’t need access of digite :)
All this just a Button click away :)
For that we have developed a web application to access this application and generate reports based on following parameters:-
1. CR/WR wise logged bugged.
2. Bugged logged date wise.
Just clicked on button in that web application an email with report would be in your e-mail inbox .
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 1. Web Application to generate report
Description and explanation of code is under progress...Hope shortly I will come back with code explanation and more description about above Application :)

Fig 2. Report sent through Email.

Thanks,

Md.Jawed