简体   繁体   中英

How to assert that needed file has been downloaded Selenium C# chromedriver

I'm writing an automated test based on Selenium C#, using chromedriver. I'm testing that a certain file is downloaded after clicking the button. The filename changes everytime you download it, but it's always a.pdf The download path is preset to c:\Users\testcase\Downloads I simply cannot find a working C# code that verifies/asserts that the file has been downloaded. Any kind of help would be greatly appreciated. My code below

class FileIsDownloaded : OpenCloseDriver
{
[TestCase]

public void UserIsAbleToDownloadTheFile()
{
driver.Url = "https://MYWEBPAGE.COM";
IWebElement InitiateDownload = driver.findelement(By.Xpath(my-xpath);
InitiateDownload.Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
????????????

Try this, should work. But here is another problem u need to delete this file every time after assertion, because test case will always pass, if u downloaded any.pdf file even once. Let me know if it works for u, then we can try to delete file.

string pdfFile = @"c:\Users\testcase\Downloads\", "*.pdf)";
Console.WriteLine(File.Exists(pdfFile) ? "File exists." : "File does not exist.");

So I managed to figure it out how to validate that the file has been downloaded & after validation the file is deleted. Code follows after the timeout. Code in C#


            int PDFdownloaded = Directory.GetFiles(@"C:\Users\testcase\Downloads\", "*.pdf", SearchOption.AllDirectories).Length;

            if (PDFdownloaded > 0)
            {

                Assert.IsTrue(PDFdownloaded > 0);
            }
            else
            {

                Assert.IsTrue(PDFdownloaded < 0);
            }
            string[] PDF = Directory.GetFiles(@"C:\Users\testcase\Downloads\", "*.pdf");
            foreach (string file in PDF)
            {
                File.Delete(file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM