你是一名专业自动化测试工程师用C#语言 selenium3+chrome 校验文件是否下载成功
以下是一份C#语言的代码示例,使用selenium3和chrome浏览器来校验文件是否下载成功:
using System;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace FileDownloadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new ChromeDriver instance
            ChromeOptions options = new ChromeOptions();
            options.AddUserProfilePreference("download.default_directory", @"C:\Downloads");
            IWebDriver driver = new ChromeDriver(options);
            // Navigate to the download page
            driver.Navigate().GoToUrl("https://www.example.com/download");
            // Find the download button and click it
            IWebElement downloadButton = driver.FindElement(By.Id("download-button"));
            downloadButton.Click();
            // Wait for the file to download
            System.Threading.Thread.Sleep(5000);
            // Check if the file was downloaded successfully
            string filePath = @"C:\Downloads\example.pdf";
            if (File.Exists(filePath))
            {
                Console.WriteLine("File downloaded successfully.");
            }
            else
            {
                Console.WriteLine("File download failed.");
            }
            // Close the browser and exit the program
            driver.Quit();
            Environment.Exit(0);
        }
    }
}
在这个示例中,我们首先创建了一个ChromeDriver实例,并设置了下载文件的默认目录为C:\Downloads。然后,我们导航到下载页面,找到下载按钮并点击它。接下来,我们等待5秒钟,以确保文件已经下载完成。最后,我们使用File.Exists()方法检查文件是否存在,以确定文件是否成功下载。如果文件存在,则输出“File downloaded successfully.”,否则输出“File download failed.”。最后,我们关闭浏览器并退出程序。
 
原文地址: https://www.cveoy.top/t/topic/b2h8 著作权归作者所有。请勿转载和采集!