OWASP O2 Platform Blog

Automating the Download of a GitHub ZipFile (using IE’s WatiN)

Here is a script that uses WatiN’s FileDownloadHandler to automate the download of a web page in IE (which popups to save it)

var ie = "ie_hLZRz".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(true); 

//using WatiN.Core.DialogHandlers
Func<string,string, string> open_and_HandleFileDownload =
    (url, fileName)=>{
            var tmpFile = fileName.tempFile();
            var waitUntilHandled = 20;
            var waitUntilDownload = 300;
           
            var fileDownloadHandler = ie.dialogHandler<FileDownloadHandler>();
            if (fileDownloadHandler.notNull())
            {
                ie.IE.RemoveDialogHandler(fileDownloadHandler);
            }
           
            fileDownloadHandler = new FileDownloadHandler(tmpFile);
            ie.IE.AddDialogHandler(fileDownloadHandler);
           
            
            fileDownloadHandler.field("saveAsFilename",tmpFile);
            fileDownloadHandler.field("hasHandledFileDownloadDialog",false);
           
            ie.open_ASync(url);
            try
            {
                fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(waitUntilHandled);           
                "after: {0}".info("WaitUntilFileDownloadDialogIsHandled");
                fileDownloadHandler.WaitUntilDownloadCompleted(waitUntilDownload);
                "after: {0}".info("WaitUntilDownloadCompleted");
            }
            catch(Exception ex)
            {
                "[WatiN_IE][open_and_HandleFileDownload] {0}".error(ex.Message);
            }
               
            if (fileDownloadHandler.SaveAsFilename.fileExists())
            {
                "[WatiN_IE] downloaded ok '{0}' into '{1}'".info(url, fileDownloadHandler.SaveAsFilename);
                return fileDownloadHandler.SaveAsFilename;
            }
            "[WatiN_IE] failed to download '{0}' ".info(url);
            return null;
    };
return open_and_HandleFileDownload("https://github.com/SecurityInnovation/YASAT/zipball/master_", "download.zip");

//O2File:WatiN_IE_ExtensionMethods.cs
//O2Ref:WatiN.Core.1x.dll
//_O2Tag_DontAddExtraO2Files;           
//O2File:_Extra_methods_Collections.cs

The example above did a download from a puplic repository, if you need to access a private one (which required GitHub login), here is how to do it:

if(ie.url() != "https://github.com/DinisCruz/TeamMentor-v3.0")
    ie.open("https://github.com/DinisCruz/TeamMentor-v3.0");
var githubAccount = @"C:\O2\_USERDATA\accounts.xml".credential("github");  

Action loginToGitHub =
    ()=>{
            if(ie.hasLink("Login"))
            {
                ie.link("Login").click();
                ie.field("login_field").value(githubAccount.UserName);
                ie.field("password").value(githubAccount.Password);
                ie.button("Log in").click();
            }
        };

loginToGitHub();
return open_and_HandleFileDownload("https://github.com/DinisCruz/TeamMentor-v3.0/zipball/master", "download.zip");

January 11, 2012 - Posted by | IE Automation, TeamMentor, WatiN

2 Comments »

  1. In the line 10 from the first code, you missed a tag

    Comment by fabriciobraz | May 18, 2012 | Reply

    • I think you meant it had an extra tag (which I just removed)

      Thanks

      Comment by Dinis Cruz | May 25, 2012 | Reply


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: