Upload and download file using selenium web driver in c#

During the automation Upload and download file using selenium web driver in c# is a common task during the automation and there are lot’s way to perform this using hard code in C# or java its depends on which technology are you working. So here I am going to show you how to perform upload and download file in selenium using c#.

Upload and download file using selenium

Let’s see step by step how to deal with uploading problem using selenium web driver in c#.

 

Upload/Import a file in selenium using c#

Upload a file from your local machine to server. So first we need the file PATH or destination of the file. Then we will pass this path in the Browser element.

Way to get direct path:

var path = “C:\Users\oops\Downloads\New folder”

We can take path directly like this but its not a right way. So we can use system properties and user a dynamic way. Let see the example.

Create a folder “Data” inside the project at same location and user the below code.

var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
path = path.Substring(6) + @"\Data\"+FileName;

Complete code to upload a file in selenium using c#

publicvoid UploadFile()
{

var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
path = path.Substring(6) + @"\Data\"+FileName;
UploadBox.SendKeys(Path);
StartImport.Click();
Task.Delay(10000).Wait();

}

Download and verify a file using selenium in c#

In the below example, we will see how to download and verify a file using selenium in C#. Download a file during the automation is very easy task. We just need to Click on the download link. The second part is verify the downloaded file is bit tricky part.

Please check the below custom method “CheckFileDownloaded(filename)” which is responsible to verify the file from download folder. It will take Filename as a parameter.

Get all the file list from a folder in c#

string[] filePaths = Directory.GetFiles(Path);

How to Verify downloaded file using selenium in c#

filePaths.Contains(“Filename”)

How to Delete a file from the folder using selenium in c#

File.Delete(“Filename”);

Complete Example to verify the downloaded file using selenium in C#

publicstaticbool VerifyDowloadedFile(string filename)
        {
bool exist = false;
string Path = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\FolderName";
string[] filePaths = Directory.GetFiles(Path);
foreach (string p in filePaths)
{
if (p.Contains(filename))
{
FileInfo thisFile = new FileInfo(p);
//Check the file Which is downloaded in the last 3 minutes
if (thisFile.LastWriteTime.ToShortTimeString() == DateTime.Now.ToShortTimeString() ||
thisFile.LastWriteTime.AddMinutes(1).ToShortTimeString() == DateTime.Now.ToShortTimeString() ||
thisFile.LastWriteTime.AddMinutes(2).ToShortTimeString() == DateTime.Now.ToShortTimeString() ||
thisFile.LastWriteTime.AddMinutes(3).ToShortTimeString() == DateTime.Now.ToShortTimeString())
exist = true;
File.Delete(p);
break;
                }
            }
return exist;
        }

 

Add a Comment

Your email address will not be published. Required fields are marked *

20 − seven =