简体   繁体   中英

Dynamically determining which Nightwatch.js tests to run from database

I'm working on setting up nightwatch tests for one of our sites and have structured them so that we have testing persona's that we assign tests too. This all worked great, as long as we only run our tests in our dev environment. Unfortunately we want to dynamically set up the persona's properties and what tests they need to perform based off of the environment they are being executed for.

Problem is that nightwatch fires before the test collection has been assembled so it sees no tests to run. I would like to delay nightwatch from starting till after everything is completely generated.

Here is an example persona :

/// <reference path="../../basepersona.ts" />
/// <reference path="../../Utilities.ts" />
namespace Tests {
export class Dealer_Persona extends BasePersona {
    constructor() {
        super("Dealer");

        var setParameters = function (persona, results, testsCallback, getTests) {
            results.forEach((param) => {
                switch (param.ParameterName) {
                    case "UserName":
                        persona.UserName = param.ParameterValue;
                        break;
                    case "Password":
                        persona.Password = param.ParameterValue;
                        break;
                    case "CreditCardNumber":
                        persona.CreditCardNumber = param.ParameterValue;
                        break;
                    case "CreditCardExpireMonth":
                        persona.CreditCardExpireMonth = param.ParameterValue;
                        break;
                    case "CreditCardExpireYear":
                        persona.CreditCardExpireYear = param.ParameterValue;
                        break;
                    case "CreditCardCVV":
                        persona.CreditCardCVV= param.ParameterValue;
                        break;
                }
            });

            getTests(persona, testsCallback);


        }

        this.DAL.GetParametersForPersona(this, setParameters, this.DAL.SetTestsForPersona, this.DAL.GetTestsForPersona);

    }
}
}


var persona = new Tests.Dealer_Persona();

This is a very basic persona, several of them have need to use some very environmentally specific values for their properties. Also, since there are several callbacks that need to occur for the persona to be loaded properly, the GetParametersForPersona method had to accept multiple callback functions and act as the first step in the chain.

Here is the DAL class :

/// <reference path="interfaces.ts" />
/// <reference path="../node_modules/@types/node/index.d.ts" />
/// <reference path="../node_modules/@types/mssql/index.d.ts" />
export class DALMethods {
    constructor() {
        var nextVal = false;
        process.argv.forEach(function (val, index, array) {
            if (nextVal) {
                site = val;
            }

            if (val.indexOf("site") > 0) {
                nextVal = true;                  
            }
        });

        var Setter = new Settings();

        Setter.SetVariables(site);
    }

    GetTestsForPersona(persona: BasePersona, callback): any {
        new sql.Request(connectionString)
            .input('persona', sql.VarChar(100), persona.Name)
            .input('environment', sql.VarChar(10), site)
            .execute('usp_AutomatedTesting_GetPersonaTests').then(function (recordsets) {
                callback(persona, recordsets[0]);
            }).catch(function (err) {
                // ... execute error checks 
            });
    }

    GetParametersForPersona(persona: BasePersona, callback, testsCallback, getTests): any {
        sql.connect(connectionString).then(function () {    
        new sql.Request()
            .input('persona', sql.VarChar(100), persona.Name)
            .input('environment', sql.VarChar(10), site)
            .execute('usp_AutomatedTesting_GetPersonaParameters').then(function (recordsets) {
                callback(persona, recordsets[0], testsCallback, getTests);
            }).catch(function (err) {
                console.log(err);
                });
        }).catch(function (err) {
            console.log(err);
        });
    }

    SetTestsForPersona(persona, results) {
        results.forEach((param) => {
            switch (param.TestName) {
                case "LoginFailure":
                    persona.Tests.push(new LoginFailure_Test(persona));
                    break;
                case "LoginGuestFailure":
                    persona.Tests.push(new LoginGuestFailure_Test(persona));
                    break;
                case "ShippingFailure":
                    persona.Tests.push(new ShippingFailure_Test(persona));
                    break;
                case "PaymentFailure":
                    persona.Tests.push(new PaymentFailure_Test(persona));
                    break;
                default:
                    console.log(param.TestName + " does not exist");
                    break;
            }
        });

        persona.Tests.forEach((x) => {
            Object.defineProperty(persona, x.Name, {
                value: x.Test,
                configurable: true, writable: true, enumerable: true
            });
        });

    }
}

I think my problem stems from the fact that I don't know of a way to load these persona's (there are 23 of them), set up their parameters and assign them tests (n number of tests per persona) in a way that will block nightwatch from executing till they are completely finished so that nightwatch will see them.

Any insight would be greatly appreciated!

We ended up using gulp to compile all the TS first, then call nightwatch. Not what I was hoping for, but it works.

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