In the journey of Automation, you will end up many situations where your test creates a file and you need to check if the file is created or if the content of the file is appropriate and as per expectations. The created file can be created on to local machine or can be on to a remote machine. If it is on local machine, it is easy to verify but if it is on some other machine then it is little tricky and that can be done using IIS Service.
- Verify on Local Machine: Verify the existence of the newly created file on the local machine. (Next Article)
- How to Verify file downloaded successfully through selenium using IIS Service.(Remote Machine)
Verify file using IIS Service on Remote Machine
Create two empty text files namely sample_1 and sample_2 and compress them using any zip software. Store the two files at any location.
Steps to Compress the Files:
Here we are using 7-Zip (free software compression tool) to compress the files.
- Select the two files and right click.
- Hover on 7-Zip.
- Click on Add to sample.zip.
- You will get a zip file.
Text is not required within the files,since we simply want to confirm if we have downloaded our required files in this example.
Note:
- We can also use any other file type and other software compression tool can also be used.
- We have selected zip format for simplicity to implement our example.
Copy and store the zip file in a folder in D drive or any folder of your choice. Let the folder name be “data”. We need to host the zip file on IIS Server (Windows Operating system) for implementing the program. IIS Server is not enabled by default.
Enable IIS Service on Windows Machine
For the simplicity of the example, we will enable the IIS service on the local machine, from where we will trigger the test. But the procedure is same if the files are created on the remote machine.
To enable it follow the steps mentioned below:
- Click Windows + R Key and this is populate run dialog box. Type control panel and click on OK.
- The Control Panel window opens up. Now, for the View By option: select Large icons. You would see the expanded control panel and then Click on Programs and Features.
- Programs and Features dialog box opens up. Click on Turn Windows features on or off.
- Windows Features dialog box opens up. Enable all features under Internet Information Services and click OK.
- Close the Windows Features dialog box and Control Panel.
- Click on Windows + R key and you would see the run dialog box. Type inetmgr and click on OK.
- Internet information Services (IIS) Manager would open. Now click on the Expansion Arrow.
- Right click on Sites and click on Add Website.
- After clicking on Add Website, the following dialog box opens up. Enter the following data.
Note:
- We can enter any site name: Here we have given site name as test.
- he physical path is the location where we have stored our zip file.
- We have entered port number as 81, since default website in IIS uses port 80. We can also use other valid port numbers. Ensure that any other application is not using the port number you have entered.
- Click on test under Sites.
- Double click on Directory Browsing. You would see Enable or Disable button.
- If you see Enable button, click on it and click on Back arrow.
If you see Disable button, click on Back arrow.
Back arrow
- Click on Browse to open the localhost.
- The webpage should open (we are using default browser as Google Chrome).
Code Implementation
Code for implementation of the program (we use Visual Studio as our IDE). We can use Express or Community editions of Visual Studio which are freely available.
We have created a solution and added a class library within it. We have created a folder (creating folder is optional) with in the class library and add a class file to implement our code. Let the class file name be DownloadDemo.cs
Steps to Add Reference:
On the right side under projects click on Reference >> Add Reference
Reference Manager dialog box opens up. Click on Assemblies. Select the references which have been highlighted. Click on OK.
After this, create a class and declare certain variables which will be used later. Google Chrome is used to implement this program. However, other browsers can be used too. Later initialize ChromeDriver. The chromedriver.exe file has been stored in the path: C:\\Program Files (x86)\\chromedriver
Note:
- We need to ensure Chrome browser to configure to download files automatically.
- Always use the latest chromedriver version for the most recent version of Google Chrome.
Steps to configure Google Chrome browser:
- Open your Google Chrome browser.
- At the top right, click .
- Click on Settings.
- At the bottom, click Show advanced settings link.
- Under the “Downloads” section, adjust your download settings:
- To change the default download location, click Change and select where you would like your files to be saved. This location has to be used in the code. To implement our code, we have chosen our location as C:\Users\abc\Downloads
- Uncheck the box “Ask where to save each file before downloading.”
The below snapshot of code provides some details of method Download_Demo which helps to download file and check if the file exists.
This code snapshot provides details of zip file checking and extracting the files within it.
File.Exists method requires System.IO namespace. The File.Exists method accepts string as a parameter which we use here to check if our file exists. It returns Boolean value. (true or false)
Note: There are two ways to write a path for file:
- @” C:\Users\abc\Downloads”
- “C:\\Users\\abc\\Downloads”
This code snapshot provides details of verifying files.
Directory.GetFiles method gets the files in a specific folder. The Directory.GetFilesmethod accepts string as a parameter. We pass the path of file inside it. In the above code, we save the files in a string array (i.e. string[] fileEntries).
Check data in Quick Watch
- You need to add debugger to the line of code.
- When you run your program (you can use NUnit to run your program), the program will halt itself at debugger.
- Press F10, to move to next line of code, this will ensure that the previous line of code is executed and the required data inside (in this case string []fileEntries) will be seen.
- Right click on the element, for which information is required.(we right click on fileEntries) (Refer Figure 26)
- Click on Quick Watch.
Look at the below screenshot that inside the array there are two files at index 0 and 1. However, only filename is needed. Later a list of string is created which will individually store only filename.
A for loop is used to iterate through the length of array and to split the data present in array into individual elements. Iterate till the total length of the array.(Line 79). The total length of the array represents the number of files we expect; in this case we have stored two text files.
Within the For loop an array is created (i.e. string[] split) which will store the last element of the path.(i.e. the filename). The path can be easily split using “\\” character.
Later we add the filenames in the list. This is done by adding the last element to List listItemsName which is done with the following code.
listItemsName.Add(split.Last());
After iterating through the For loop again for the second time, all required elements are added in the List listItemsName, and we can see that our list has the two items.
List<string>originalList= newList<string>{ “sample_1.txt”, “sample_2.txt” };
This is the list of string which holds the elements (filenames) which we expect to be present.
Now, using For loop we compare the two lists:
- listItemsName
- OriginalList
In the For loop, we iterate till the count of originalList, as we need to compare the elements present in it. We want to compare downloaded files in listItemsName with the files expected in the originalList.
result = listItemsName[j].Equals(originalList[j]);
Now the above line of code will compare both the lists and store Boolean result. The. Equals Method compares the two lists here for data within them.
Later, if the result is true, pass the test and call a method to delete the test data. Else fail the test and call the method to delete the test data.
DeleteFilesAndDirectorymethod:In the method we check if the directory exists, if it exists, then we delete the directory and its contents. Later will also check if the zip file which has downloaded initially is present, if it present, it is deleted.
This cleanup of data ensures that when the test is run next time, it will not conflict with the existing items which were created in previous test.
We close our browser in the end.
Author Bio:
Shyam Gawade is a Software Test Engineer with two years of experience in manual and automation testing. His areas of interest include Test designing and Selenium. He enjoys diving deep into his areas of interest and acquire new skills. In his free time, he enjoys playing football and surfing the web for articles to keep him updated on the most current technological trends.
My LinkedIn profile : https://www.linkedin.com/in/shyam-gawade-ba328593/