Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

typescript - splunk-logging not working when invoking method from After hook using Cucumber and Protractor

I'm currently working on setting up an environment for automated testing using Protractor, Cucumber, and Typescript. I will need to send some logs into Splunk Cloud for each of the scenarios described in the Cucumber features. For this, I'm using splunk-logging library and I want to use this library in the After hook using CucumberJS.

I created a class SplunkLogClass.ts for handling the Splunk logging. This class setups the logger configuration and has a method that receives the message that wants to be logged:

export class SplunkLogClass {
    
    private logger;

    constructor () {
        var splunkLogger = require('splunk-logging').Logger;
        var config = {
            token: "my-token",
            url: "host",
            port: '443',
            index: "index"
        };
        
        this.logger = new splunkLogger(config);

    }

    public async logIntoSplunk(result): Promise<void>{

        var payload = {
            // Message can be anything; doesn't have to be an object
            message: {
                    testResult: result
            }
        };
        await console.log("Sending payload", payload);
        await this.logger.send(payload);
        await console.log('Sent')
        
    }
}

Then, I have my hooks file hooks.ts where I defined the actions I want to perform after each scenario executes.

const { After,AfterAll } = require("cucumber");
import { SplunkLogClass } from '../support/SplunkLogClass'

var myLogger = new SplunkLogClass();
myLogger.logIntoSplunk('before after').then()
myLogger.logIntoSplunk('before after 2').then()

After({timeout:100*100000},async function(scenario){
    await myLogger.logIntoSplunk(scenario.pickle.name).then()
    await console.log('done');
    await myLogger.logIntoSplunk(scenario.pickle.name).then()
});

AfterAll({timeout: 100 * 1000}, async () => {
    await myLogger.logIntoSplunk('afterall').then()
});

The funny thing is the splunk logging attempts before the After declaration work but the ones that are inside After and AfterAll declaration don't work (they are executed after the scenario). The 'Sent' and 'Done' console log works properly and no exceptions are thrown while debugging.

I'm kinda new to JavaScript and TypeScript and I've tried all kinds of combinations using async, await, then, etc because it is still confusing for me how this works. None of the combinations have worked.

question from:https://stackoverflow.com/questions/65598477/splunk-logging-not-working-when-invoking-method-from-after-hook-using-cucumber-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...