“This is the 17th day of my participation in the First Challenge 2022.

preface

In automated testing, verification file download is often encountered. Sometimes, it only verifies that the action of downloading can be clicked normally, and sometimes it verifies whether the downloaded file is correct. Whether the downloaded file is correct includes two aspects: whether the downloaded file exists and whether the content of the downloaded file is correct. The content of the downloaded file, how to compare and contrast the content of the downloaded file and the expected results, if it is CSV file and TXT file, we can read it for comparison, if it is other files, such as compressed files and other more complex types

Step 1: Verify the file download

Simulation manually click on the download link to download the files will be placed the browser’s default download location, if you want to let the download file to the specified directory, need to modify the browser configuration, can be manually modified, but when automation, is to create a new browser instance, manually modify the configuration will not take effect, can be controlled through the code at this moment, look at the code

 from selenium import webdriver
 import os
 Set the browser download directory
 chrome_options = webdriver.ChromeOptions()
 prefs = {"download.default_directory":"D:\\DOWNLOAD\\"}
 chrome_options.add_experimental_option("prefs", prefs)
 driver = webdriver.Chrome(chrome_options=chrome_options)
Copy the code

To verify the existence of the file, delete the existing files in the directory, download the file, and then check whether the file is downloaded successfully

file = "D:\\DOWNLOAD\\test.pdf"
if os.path.exists(file):
    os.remove(file)
Click the action to download the file, the detailed code for this step is omitted
driver.find_element_by_xx(locator).click()
Check whether the download is successful

if os.path.exists(file):
    print("download sucess!!")
Copy the code

Solve the second step, verify the file contents

There is also a premise, each time to verify the file content should be the same, if the real-time generation of different files according to different filtering conditions, then the following method is not applicable. If the authentication result is the same each time, md5 can be used

We first manually execute once to get the target file, go to the Internet or install a tool that can get the MD5 of the file, get the MD5 value of the target file and save it, then use the code to get the MD5 of the downloaded file, and compare the two

importHashlib download_file = "xxxx.rar" m = hashlib.md5()with open(file, "rb") as f:
    for line in f:
        m.update(line)
expect_md5_code = m.hexdiget()

if expect_md5_code==source_md5_code:
    print("sucess!!")

Copy the code

conclusion

If it is per download file is not the same as that kind of situation, particular case is particular analysis, we can advance prepared data, and let it each time, although is not the same as the filter conditions, but can ensure that every time the results can be expected, then we can also use this method, if it is a random create runtime data, the expected results can not forecast, That depends on what type of file you downloaded and whether Python can handle it.