Check file download with Selenium

I need to use selenium to verify downloads. I need to click the link to the download file and verify that it is downloadable or not. (File uploading starts or not) For this I need to create a simple HTML script. But since Selenium does not recognize the Save As dialog box for downloading the file, I cannot continue. There is a solution for selenium for this. I cannot use any other third-party tool, as this is part of a centralized user interface testing script. Thanks in advance.

+1
source share
1 answer

My solution (in C #) gets the URL of the download file and any cookie and makes a request using WebClient:

        var testLink = seleniumDriver.FindElement(By.LinkText("Link to file"));
        var pdfHref = testLink.GetAttribute("href");
        var manage = seleniumDriver.Manage();
        var cookies = manage.Cookies.AllCookies;
        using (var wc = new WebClient())
        {
            foreach (var cookie in cookies)
            {
                var cookieText = cookie.Name + "=" + cookie.Value;
                wc.Headers.Add(HttpRequestHeader.Cookie, cookieText);
            }
            var fileResult = wc.DownloadData(new Uri(pdfHref));
            // or use wc.DownloadString or wc.DownloadFile
            // Do any test required
        }
0
source

Source: https://habr.com/ru/post/1703412/


All Articles