繁体   English   中英

将一组文件路径从命令行传递到“npm run test”?

[英]Passing set of file Paths from the command line to on “npm run test”?

我正在使用 Testcafe 将文件上传到我的应用程序,我需要运行命令“npm run test”。 目前,我已经在我的规范脚本中硬编码了文件路径。

我想要实现的是使用 n 个不同的文件在 n 次循环中运行相同的规范。 到目前为止,对我来说实现这一目标的唯一方法是为具有不同文件名的 n 文件编写 n 规范,但我不想这样做,因为所有规范都相同。

fixture `Getting Started`
    .page `http://mywebsite.org`;

test
    .before( async t => {
     console.log("User Log Tests");
     })
    ('File_Upload', async t => {
    await t
    .click('.home-search-btn')
    .wait(5000)
    .click('.menu.icon.large-visibility>ul>li:last-
    child>ul>li>.checkbox')
    .setFilesToUpload('input[type="file"]', [
        'home/name/fileDirectory/file.json',
     ])
    .click('.icon.base-upload')
    });

我想知道是否可以自动化,以便可以从某个目录中读取文件名并在我的规范中进行替换。

如果您想对多个文件名执行相同的操作,您可以将它们的列表保存在一个文件中,并在夹具内或测试内的循环中使用它。 例如,如果files.js文件中有一组文件路径,则可以使用以下方法:

// files.js
export default files = ['file1.json', 'file2.json'];

// test.js
import files from './files.js';

fixture `Getting Started`
    .page `http://mywebsite.org`;

for(const file of files) {
    test
    .before( async t => {
        console.log("User Log Tests");
    })
    (`File_Upload - ${file}`, async t => {
        await t
            .click('.home-search-btn')
            .wait(5000)
            .click('.menu.icon.large-visibility>ul>li:last-child>ul>li>.checkbox')
            .setFilesToUpload('input[type="file"]', [file])
            .click('.icon.base-upload')
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM