首页 > 解决方案 > 如何断言所需文件已下载 Selenium C# chromedriver

问题描述

我正在使用 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#seleniumautomationselenium-chromedriver

解决方案


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

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

推荐阅读