如何使用 Selenium/WebdriverIO 探测文件是否被下载 [英] How to probe if a file was download using Selenium/WebdriverIO

查看:27
本文介绍了如何使用 Selenium/WebdriverIO 探测文件是否被下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在单击下载按钮后验证文件是否是使用 Selenium Webdriver 下载的.

I want to know how I can verify if a file was downloaded using Selenium Webdriver after I click the download button.

推荐答案

您的问题并没有说明您是要本地确认还是远程确认(如浏览器堆栈).如果它是远程的,那么我的回答将是NO",因为您可以看到文件正在下载,但您无法访问该文件夹.因此,您将无法断言该文件已下载.

Your question doesn't say whether you want to confirm it locally or remotely(like browserstack) . If it is remotely then my answer will be "NO" as you can see that the file is getting downloaded but you can not access the folder. So you wont be able to assert that the file has been downloaded.

如果您想在本地(在 Chrome 中)实现这一点,那么答案是YES",您可以这样做:

If you want to achieve this locally(in Chrome) then the answer is "YES", you can do it something like this:

在 wdio.conf.js 中(要知道它在哪里下载)

In wdio.conf.js(To know where it is getting downloaded)

var path = require('path');

const pathToDownload = path.resolve('chromeDownloads');

// chromeDownloads above is the name of the folder in the root directory
exports.config = {
capabilities: [{
        maxInstances: 1,     
        browserName: 'chrome',
        os: 'Windows',      
        chromeOptions: {
            args: [
                'user-data-dir=./chrome/user-data',
            ],
            prefs: {
                "download.default_directory": pathToDownload,
            }
        }
    }],

还有你的规范文件(检查文件是否下载?)

And your spec file(To check if the file is downloaded or not ?)

const fsExtra = require('fs-extra');
const pathToChromeDownloads = './chromeDownloads';

describe('User can download and verify a file', () =>{

    before(() => {
        // Clean up the chromeDownloads folder and create a fresh one
        fsExtra.removeSync(pathToChromeDownloads);
        fsExtra.mkdirsSync(pathToChromeDownloads);
    });

    it('Download the file', () =>{
        // Code to download 
    });

    it('Verify the file is downloaded', () =>{
        // Code to verify 
        // Get the name of file and assert it with the expected name
    });
});

关于 fs-extra 的更多信息:https://www.npmjs.com/package/fs-额外

more about fs-extra : https://www.npmjs.com/package/fs-extra

希望这会有所帮助.

这篇关于如何使用 Selenium/WebdriverIO 探测文件是否被下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆