繁体   English   中英

如何断言所需文件已下载 Selenium C# chromedriver

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

我正在使用 chromedriver 编写基于 Selenium C# 的自动化测试。 我正在测试单击按钮后是否下载了某个文件。 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. 任何形式的帮助将不胜感激。 我的代码如下

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

试试这个,应该可以的。 但是这里还有一个问题,你需要在每次断言后删除这个文件,因为测试用例总是会通过,如果你下载了 any.pdf 文件甚至一次。 让我知道它是否适合你,然后我们可以尝试删除文件。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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