question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Writing To A Global Variable When Tests Are Run In Parallel

See original GitHub issue

Hi There

I’m not sure whether this is the right thread to post this comment in but I was hoping somebody can help with this?

I have the following configuration data snippet

var _Global;

beforeLaunch: function()
{

    var Fsp = require('fs-promise'), mkdirp = require('mkdirp'), ResultsDirectory = './Results';

    //Minimum MetaData That Will Get Wrote To Report...
    global.MetaData =
    {

        OsPlatform: null,

        AngularVersion: null

    };

    _Global = global;

    return Fsp.exists(ResultsDirectory)
    .then(function(exists) { if(exists) { return Fsp.remove(ResultsDirectory); } })
    .then(function()
    {

        return mkdirp(ResultsDirectory, function (err)
        {

            if (err)

                console.error(err);

            else

                console.log('Directory To Store Test Results Created As Expected!!!');

        });

    });

},

onPrepare: function()
{

    var _this = this;

    browser.ignoreSynchronization = false;

    browser.getProcessedConfig().then(function(currentConfig)
    {

        console.log('For This Test We Are Using Browser: ' + currentConfig.capabilities.browserName);

        console.log('Full Details Listed Below:');

        console.log(JSON.stringify(currentConfig.capabilities, null, 4));

    });

    return browser.get(browser.baseUrl)
    .then(function() {return browser.executeScript("return window.angular.version.full;")})
    .then(function(AngularVersion)
    {

        _Global.MetaData.OsPlatform = require('os').platform().toUpperCase();

        //BrowserName & Version...
        browser.getCapabilities().then(function (Caps)
        {

            var BrowserObject = JSON.parse("{\"" + Caps.get('browserName') + "\": \"" +Caps.get('version')+ "\"}");

            return Object.assign(_Global.MetaData, BrowserObject);

        });

        return _Global.MetaData.AngularVersion = AngularVersion;

    });


},

afterLaunch: function(ExitCode)
{

    console.log('Finished With Exit Code ' +ExitCode);

    return Reports.GetAllJsonFilesInResultsDirectory()
    .then(function() {return Reports.MergeJsonFilesToOneObject();})
    .then(function() {return Reports.WriteJsonStringToFileSystem();})
    .then(function() {return Reports.CreateJunitXmlReport();})
    .then(function() {return Reports.CreateHtmlReports();})
    .then(function(Results)
    {

        Results.forEach(function(Result)
        {

            console.log(Result);

        });

    });

}

When I run the tests on a single browser instance this works every time but when I run in parallel this fails on function “onPrepare”, namely it throws an exception. Does “cucumber.js” have a global space that test data could be wrote to or any similar equivalent?

Basically the use here is to write the browser name & version to a global variable “MetaData” which will get wrote to the final report generated. So in parallel tests you would have browser information for ie, firefox & chrome wrote to object “MetaData”. Many thanks in advance as to any guidance anyone provides.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bhreinbcommented, Sep 15, 2017

Hi @danielcb29

This question definitely shouldn’t exist in this space. This is a protractor life cycle question more so than a cucumberjs question. In any case my solution is detailed below

My use was that I wanted to collect metadata across different browsers and merge that to the one object at the end of a test run. Generally the metadata was more or less the same per browser instance the only difference been was the browser name and version. Solving this required understanding of the protractor life cycle

beforeLaunch ~ is executed once only if single instance browser testing or parallel testing
onPrepare ~ is executed on each browser the test is run on 
afterLaunch  ~ is executed once only if single instance browser testing or parallel testing

based on that info I collected all the metadata in the onPrepare block and wrote to a JSON file at a know location (I included the browser name & version when writing this). In the afterlaunch life cycle event i collected all the JSON files using regular expression matching and merged them, thus getting me the one JSON file that .

Unfortunately you can’t write each browser object to the global space because when we launch in parallel it launches the tests under different PIDs hence the inability to write this in the global space. Hope this helps.

0reactions
lock[bot]commented, Oct 24, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest parallelization, globals, mocks, and squawkless tests
#Tests in a single file are NOT run in parallel ... Simple example, the global variable r is included in the test condition,...
Read more >
How to use Global Variables between test suites while ...
Global variables works well between test cases in a test suite. ... But pay attention, if you attempt to run the suites in...
Read more >
How global variable works in parallel programming with Python?
But when the processes are initialized prior to calling test , your source is re-executed and each process, which runs in its own...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Mocha is a feature-rich JavaScript test framework running on Node.js and in the ... global variable leak detection; optionally run tests that match...
Read more >
Paralleism and multithreading
Advanced topics: Parallelism. ... Threads share all global variables; the memory space where global variables are stored ... Testing for thread completion.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found