Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

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:UsersestcaseDownloads 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);
????????????
question from:https://stackoverflow.com/questions/65922443/how-to-assert-that-needed-file-has-been-downloaded-selenium-c-sharp-chromedriver

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:UsersestcaseDownloads", "*.pdf)";
Console.WriteLine(File.Exists(pdfFile) ? "File exists." : "File does not exist.");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...