Search code examples
node.jsselenium-webdriverwebdriver-io

WebdriverIO update results in browserStack API call does not wait for response


I have setup my tests using webdriverIO V5 and Browserstack. The tests run successfully, however the test results are not being updated in the BrowserStack . The request call that I have made in my code does not wait for it to complete

in my wdio.conf.js , I have the following code:

afterTest: function (test, context, { error, result, duration, passed, retries }) {
        let sessionid = browser.sessionId;
        if (!test.passed) {
            request({
                uri: `https://${this.user}:${this.key}@api.browserstack.com/automate/sessions/${sessionid}.json`,
                method: 'PUT',
                form: { 'status': 'error', 'reason': "" }
            })      
        }
        else {
            request({
                uri: `https://${this.user}:${this.key}@api.browserstack.com/automate/sessions/${sessionid}.json`,
                method: 'PUT',
                form: { 'status': "passed", 'reason': "" }
            })
        }
//browser.pause(3000)

Results are updated only when I give the browser.pause(3000). How can I make this to wait till the request has been completed rather that waiting for 3 seconds after every test


Solution

  • @NaveenThiyagarajan - request() is from node_module. The above code is in wdio.conf.js file. I was able to get it working with async . is there a better way to do this? below is my code:

    var request = require("request");
    
    exports.config = {
        afterTest: async function (test, context, { error, result, duration, passed, retries }) {
            let sessionid = browser.sessionId;
    
            function doRequest(url, testStatus) {
                return new Promise(function (resolve, reject) {
                    request({
                        uri: url,
                        method: 'PUT',
                        form: { 'status': testStatus, 'reason': "" }
                    }, function (error, response, body) {
                        if (!error && response.statusCode === 200) {
                            resolve(body)
                        }
                        else {
                            reject(error)
                        }
                    })
                })
            }
    
            if (test.passed) {
                let res = await doRequest(`https://${this.user}:${this.key}@api.browserstack.com/automate/sessions/${sessionid}.json`, "passed")
                console.log(res)
            }
            else {
                let res = await doRequest(`https://${this.user}:${this.key}@api.browserstack.com/automate/sessions/${sessionid}.json`, "error")
                console.log(res)
            }
        },
    }