【问题标题】:How to assert that needed file has been downloaded Selenium C# chromedriver如何断言所需文件已下载 Selenium C# chromedriver
【发布时间】:2021-05-01 11:34:27
【问题描述】:

我正在使用 chromedriver 编写基于 Selenium C# 的自动化测试。我正在测试单击按钮后是否下载了某个文件。 每次下载时文件名都会更改,但始终是 .pdf 下载路径预设为 c:\Users\testcase\Downloads 我根本找不到验证/断言文件已下载的有效 C# 代码。 任何形式的帮助将不胜感激。 下面是我的代码

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);
????????????

【问题讨论】:

    标签: c# selenium automation selenium-chromedriver


    【解决方案1】:

    试试这个,应该可以的。但这是另一个问题,您需要在每次断言后删除此文件,因为测试用例将始终通过,如果您下载任何 .pdf 文件甚至一次。让我知道它是否适合你,然后我们可以尝试删除文件。

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

    【讨论】:

      【解决方案2】:

      所以我设法弄清楚如何验证文件是否已下载并在验证后删除文件。超时后代码如下。 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-27
        • 2021-05-13
        • 2018-02-09
        • 1970-01-01
        • 2018-11-10
        • 1970-01-01
        • 1970-01-01
        • 2018-06-04
        相关资源
        最近更新 更多