英文:
Testing a file download using Selenium - python
问题
I write automation tests in Selenium and use python and the chrome browser.
我使用Selenium编写自动化测试,使用Python和Chrome浏览器。
I need to write a test that downloads a file (from a link <a>) and checks that the file has been downloaded. And if possible, the test checks the file type.
我需要编写一个测试,用于下载文件(从链接 <a>)并检查文件是否已下载。如果可能的话,测试还会检查文件类型。
How can I write this?
我该如何编写这个测试?
I managed to download the file using click(), but I don't know how to check if the file has been downloaded.
我成功使用click()下载文件,但不知道如何检查文件是否已下载。
英文:
I write automation tests in Selenium and use python and the chrome browser.
I need to write a test that downloads a file (from a link <a>) and checks that the file has been downloaded. And if possible, the test checks the file type.
How can I write this?
I managed to download the file using click(), but I don't know how to check if the file has been downloaded.
答案1
得分: 0
你可以将download.default_directory添加到ChromeOptions,然后使用该选项对象实例化webdriver.Chrome。所有使用此实例下载的文件将存储在download.default_directory中。
为了使测试平台无关,你可以使用pytest提供的tmp_path fixture,它将为下载的文件创建一个临时目录。但请确保将str(tmp_path)指定为ChromeOptions的download.default_directory。
考虑以下示例,你可以使用pytest test_download.py运行它:
# test_download.py
import os
import time
from dataclasses import dataclass
from pathlib import Path
import pytest
from selenium import webdriver
@dataclass
class Chrome:
driver: webdriver.Chrome
downloads: Path
@pytest.fixture(scope="function")
def chrome(tmp_path):
options = webdriver.ChromeOptions()
options.add_experimental_option(
"prefs",
{
"download.default_directory": str(tmp_path),
},
)
driver = webdriver.Chrome(options=options)
yield Chrome(driver=driver, downloads=tmp_path)
driver.quit()
def test_download_file(chrome: Chrome):
filename = "sample4.csv"
chrome.driver.get(f"https://filesamples.com/samples/document/csv/{filename}")
# 等待文件下载完成,如有需要,请调整等待时间
time.sleep(1)
# 检查文件是否已下载
assert filename in os.listdir(chrome.downloads)
请注意,代码部分不需要翻译。
英文:
You can add download.default_directory to the ChromeOptions and instantiate the webdriver.Chrome with the options object. All the downloads made with this instance will be stored in the download.default_directory.
To make the test platform-agnostic you can use the tmp_path fixture provided by pytest, it will create a temporary directory for the downloaded files. But make sure you specify str(tmp_path) as the download.default_directory for the ChromeOptions.
Consider the following example, you can run it with pytest test_download.py:
# test_download.py
import os
import time
from dataclasses import dataclass
from pathlib import Path
import pytest
from selenium import webdriver
@dataclass
class Chrome:
driver: webdriver.Chrome
downloads: Path
@pytest.fixture(scope="function")
def chrome(tmp_path):
options = webdriver.ChromeOptions()
options.add_experimental_option(
"prefs",
{
"download.default_directory": str(tmp_path),
},
)
driver = webdriver.Chrome(options=options)
yield Chrome(driver=driver, downloads=tmp_path)
driver.quit()
def test_download_file(chrome: Chrome):
filename = "sample4.csv"
chrome.driver.get(f"https://filesamples.com/samples/document/csv/{filename}")
# Wait for the file to download, adjust if needed
time.sleep(1)
# Check if the file has been downloaded
assert filename in os.listdir(chrome.downloads)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论