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!!!


10 thoughts on “How to handle JavaScript pop-up window, file download handler in WATiN

  1. Hi, I have a question on watiN. I am using a file handler to downlaod the file from a web site. it works well only when somebody is logged in the computer. once computer is locked then process would stop a the windows file save as dialogue. following is code and in

    FileDownloadHandler fileHandler = new FileDownloadHandler(lstrFilePath);


    last 2 lines of code shows up the windows save as dialogue box. where process waits and gets timed out of no one logs in the computer.

    any help is highly appeciated.

  2. Hi,
    Good Morning,
    have a nice day.
    let's try the below snippet of code.hope it would solve your problem.
    if still your are facing problem please feel free to ping me 🙂

    public void DoActivityDialogeHandler_Save()
    //assume that this is pdf file.
    string fullFileName =”c:\\MyFileName.pdf “;
    // filedownload handler is //initialized in file save mode which is default.
    FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(fullFileName);
    // whatever filename we give //fileDownloadHandler will use that as //SaveAsFilename;
    using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler))
    catch(Watin.Exception ex)

    if (File.Exists(fullFileName))
    Assert.That(true, “FileSave Success”);
    // File.Delete(fullFileName);
    Assert.That(false , “FileSave failed”);


  3. Hi,
    Thanks for your post. I have a question on Watin popup window.

    I have a java script as shown in the example field (click below link):

    I am having below Watin code to handle the popup:


    IE ie = new IE(“about:blank”);
    AlertAndConfirmDialogHandler myHandler = new AlertAndConfirmDialogHandler();
    //Declare an object of HTMLDialog
    HtmlDialog myModalHandler;
    ie.Button(Find.ByValue(“Click Me!”)).ClickNoWait();
    myModalHandler = ie.HtmlDialog(Find.ByTitle(“Popup Example”));


    I am not able to handle popup window as it is giving error during execution.

    Can we find popup window by innerHTML?

    Can you please help me in handling popup?

    Thanks for your help,


  4. Hi,
    I am using FileDownloadHandler but my program throws an error of missing directive or refernce. I have WatiN.core and NUnit.framework reference set up front. Do I need to add any other reference ???
    Help me plz …

  5. Hi Jawad !

    I appreciate for sharing WATIN knowledge. I have a Question for you regarding WATIN

    My website consist of a Wizard. It is an enhanced version of modal dialog. The wizard contain update panel( and it has many screens), which has all the controls(and when next button is pressed, other control appear which also exist on that panel) It contains serveral controls(Like textboxes,buttons,checkboxes). I have tried to grab the controls but failed( use several techniques like ie.HtmlDialog,iFrame,WaitUntilExits,mshtml.HTMLDocuemntClass,etc).

    Actually WatiN does not realise that the page has actually refreshed therefore not be able to find the elements.

    Plz Help !!!

    I am stuck here.

  6. i m using watin dialog handler and downloading file 7.6mb and get folling error:-
    The CLR has been unable to transition from COM context 0x1a4588 to COM context 0x1a46f8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operation

    my code is
    IE ie = new IE(“url”);
    string time = ie.Div(Find.ById(“downloadDelayTimeSec”)).Text;
    //Div book=ie.Div(Find.ByClass(“searchlinks”));
    FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(“D:\\Arminder.pdf”);
    using (new UseDialogOnce(ie.DialogWatcher, fileDownloadHandler))
    ie.Link(Find.ByText(“Download file now”)).ClickNoWait();

  7. I have a similar scenario like “I need to click on save dialogue box and the file should be saved. But this will run in virtual machine and no one will be logged in. If no one is logged on, it will get struck at the save dialogue box. And if we unlock the Virtual machine, then it detects the save ,clicks on save and file gets saved.”
    This is the case. But I want it to be done automatically without any manual intervention.

    Please suggest on this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s