0

I have a situation where I have to check the download link to make sure when someone clicks the link download is going to kick off and the product will be downloading, Is there anyway that we can do that using Selenium WebDriver?

4
  • Is there a download status somewhere? It might utilize browser specific download or a custom one, but that part is important. Also is there a specific timeframe for the download as in it's always a particular size file?
    – mutt
    Commented Apr 20, 2017 at 5:17
  • what about thinking our of the box- click the link and check that the file exist on the file system ?
    – Rsf
    Commented Apr 20, 2017 at 6:48
  • He needs to verify "download is going to kick off and the product will be downloading". Commented Apr 20, 2017 at 11:03
  • its a fixed size file and there is no changes in UI to show the download status. :(
    – sarath
    Commented Apr 20, 2017 at 13:31

2 Answers 2

2

I had a similar test case that was clicking the "Export to Excel" button on a page and then verify that a file was downloaded. Here is the POM method I wrote (it's in C# but changing it to Java shouldn't be too difficult):

    public string ExportToExcel()
    {
        // Get the default downloads folder for the current user
        string downloadFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
        DirectoryInfo di = new DirectoryInfo(downloadFolderPath);
        // Delete all existing files
        foreach (FileInfo file in di.GetFiles())
        {
            file.Delete();
        }
        // Delete all existing directories
        foreach (DirectoryInfo dir in di.GetDirectories())
        {
            dir.Delete(true);
        }
        // Click "Export to Excel" button
        this.btnExportToExcel.Click();
        // Wait for the download
        System.Threading.Thread.Sleep(1500);
        // Verify file exists in Downloads folder
        FileInfo[] TXTFiles = di.GetFiles("*.xlsx");
        if (TXTFiles.Length == 0)
        {
            return "No files present";
        }
        else
        {
            return "";
        }
    }

Then in the Test method, use it like:

Assert.IsEmpty(MyPomExample.ExportToExcel());

The Assert checks that the string is empty. If not, the test fails and the string is printed when the exception is thrown.

0
0

I think if the download link returns a 200 response code, you can verify that it will download correctly. Try using this method:

private static string GetUrlResponse(string url)
{
    if (url == null)
        throw new ArgumentNullException("url");

    if (url.IndexOf(':') < 0)
        url = "http://" + url.TrimStart('/');

    if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
        return "False";

    var request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "HEAD";

    try
    {
        request.Proxy = null;
        using (var response = request.GetResponse() as HttpWebResponse)
        {

            if (response.ResponseUri != request.RequestUri)
            {

                if (response.ResponseUri.ToString().Contains("error"))
                {
                    return "ERROR (redirect)";

                }
                return "(Redirect)";

            }
            if (response != null && response.StatusCode == HttpStatusCode.OK)
                return "True";

            return "False";
        }
    }
    catch (WebException)
    {
        return "False";
    }
}

This will return a True or False string based on the response code of the download link. I'm not sure if getting the response will kick off the download, but I believe it doesn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.