简体   繁体   中英

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

I'm using Testcafe for uploading files to my application, I need to run the command "npm run test". Currently, I've hard coded the file path in my spec script.

What I want to achieve is to run the same spec in a loop over an n-times with n-different files. As of now the only way to achieve this for me is to write n-specs for n-files with different filenames, but I don't want to do this as all specs are the same.

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')
    });

I wanted to know if it's possible to automate so filenames can be read from some directory and substitute in my specs.

If you would like to perform the same actions for several filenames you can keep a list of them in a file and use it in a loop inside the fixture or inside the test. For example if you have an array of file paths in the files.js file you can use the following approach:

// 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')
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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