Newman multiple data files

Hi,
Is it possible to pass multiple data files via newman. Example call:
newman run collection.json -e environment.json -d testfile.json -d code.json

Please note collection file needs inputs from both data files- testfile.json and code.json for each run.

Hey @sfreewave

Welcome to the community!

You can only use 1 data file in the Newman run command. What’s your use case for needing 2 files?

Hi Danny,
Thanks for your response. We are using cypress on runtime to generate Auth Code, which store it in file ‘code…json’. And other file ‘testfile.json’ we have to use to provide test data to our collection.

Hence looking for a way how can we run them via newman.

@sfreewave: Welcome to the Community Swetha :wave:

Kindly check this related post:

May be separating the request in different folders can be a workaround for now.

As you mentioned you’re getting an Auth token from some other process (Cypress) and storing it in a file.

You could capture/reference that in a System Environment Variable at runtime and use that in the Newman command using --env-var "token=$TOKEN"

That token variable would resolve a {{token}} variable that you might have in the Collection.

Are you running this in a CI pipeline? If so what does that currently look like?

There a lot of assumptions eing made here so perhaps you could expand on your whole process so that we can advise you on a solution.

Thanks Danny,
currently we are using this code in cypress file:

 cy.log('Started the login flow - success login')
    cy.visit(authorize_url)
    cy.get('[type="text"].auth0-lock-input')
        .type(this.auth0.email)
        .should('have.value', this.auth0.email)

    cy.get('[type="password"].auth0-lock-input')
        .type(this.auth0.password)
        .should('have.value', this.auth0.password)

    cy.get('.auth0-lock-submit[type="submit"]')
        .click()

    cy.url().should('include', '/callback')

    cy.location({timeout: 5000}).then((loc) => {
        let params_strings = loc.search
        let matches = params_strings.match(/code=([^&]*)/)
        //let code = ''
        if (matches.length > 1) {
            code_HF = matches[1]
        }
        cy.writeFile('./newman/data/oauth_code.json', [{
            **'auth_code': code_HF**
        }])

and in our test collection, we need to use the value of ‘auth_code’. Plz let me know if there is any alternative way possible for accessing the value( that is withoiut redirecting it into oauth_code.json). Thanks a lot for your help.

Have you looked at using Newman as a library rather than just running this from the CLI?

You could pass that captured value between Cypress and Newman easier. One you install Newman as a dependency in the project, you can create a script to run the Collection.

const newman = require('newman'); 

newman.run({
    collection: require('collection.json'),
    environment: require('env.json'),
    reporters: 'cli',
    iterationData: 'data.json',
    envVar: [
        { 'key' : 'token', 'value': `${cypress_token}`} 
    ]     
}, function (err) {
	if (err) { throw err; }
    console.log('collection run complete!');
});

This is a little rough but hopefully you get where I’m going with it. I can cycle back later and clear it up a bit.

sure. Will definitely give a try. Thanks again. Much appreciated.

Hi Danny,
Tried using approach mentioned by you. But am stuck in how to export the value of ‘cypress_token’ from cypress tool. Dont think cypress supports this feature of setting env variables which can be accessed outside the scope of current test specification file.

Could not read the value from the created cypress file with fs or something and store that as a variable to use with the Newman script?

Hi @danny-dainton,

I am thinking about this today. My setup is that, all my test cases/data for one endpoint is inside 1 data file. We have multiple endpoints so I also have multiple data files. I am thinking of passing the filenames in the -d option for partial execution.

Thanks and regards,
Allen