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;
//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


//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(""); //Create an Instance of FileDownloadHandler by passing open zip file name as constructor

FileDownloadHandler fileDownloadHandler = new FileDownloadHandler("");

//Add this instance of FileDownloadHandler to DialogHander


//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
//Remove Watcher


Please leave your comments and suggestion.Thanks!!!