Nightwatchjs - Extracting value from globals file

26 Views Asked by At

I'm using nightwatchjs (v3.x) to run my test scripts, and I've just introduced some before and beforeEach hooks to help with these tests.

So, in my globals.js file I have the following code (which is using solr-client to get info from a solr instance to be used in subsequent test scripts);

var featuresSection = 'features/';
var tipsSection = 'tips-and-tuition/';

// solr reviews query 
var cmsRandomEquipmentReview = require('/nightwatch/custom-commands/solrQuery/randomSolrEquipmentReview');
var cmsEquipmentSettingsPage;
var solr = require('solr-client');

module.exports = {
  before: function(done) {
    var solrClient = browser.globals.solrWpEquipmentReviewsClient;
             equipmentReview.solr_equipment_review_entry(solrClient, function(err,cmsEquipmentSettings) {
  if(err) {
    console.log(err);
    done(err);
  } else {
     cmsEquipmentSettingsPage = cmsEquipmentSettings;
   //  console.log(cmsEquipmentSettingsPage)
  }
});
 done();
},

This solr query is working OK (if I add a console.log into this it correctly displays the solr entry).

However, what I now need to do is reference the value of cmsEquipmentSettingsPage in my test scripts.

To check that the test was getting the globals.js info, in my test script I added this;

module.exports = {
  'Testing the solr query': function (browser) {
    var tipsDisplayed = browser.globals.tipsSection
    var solrQueryDisplayed = browser.globals.cmsEquipmentSettingsPage  
    console.log(tipsDisplayed)
    console.log(solrQueryDisplayed)
},

However, when I run my test, tipsDisplayed correctly displays tips-and-tuition/ but solrQueryDisplayed displays undefined.

This is despite the fact that cmsEquipmentSettingsPage definitely has valid values when I add the console.log within my globals.js file.

Am I doing something obviously wrong with referencing this cmsEquipmentSettingsPage value within my test script (i.e. am I 'calling' cmsEquipmentSettingsPage before it's actually got a value, or is it something more fundamental?

Any help would be greatly appreciated. Thanks

1

There are 1 best solutions below

0
Darren Harley On

This was an issue with the globals values not persisting.

This worked for me;

var featuresSection = 'features/';
var tipsSection = 'tips-and-tuition/';

// solr reviews query 
var cmsRandomEquipmentReview = require('/nightwatch/custom-commands/solrQuery/randomSolrEquipmentReview');
var cmsEquipmentSettingsPage = {};
var solr = require('solr-client');

module.exports = {
  cmsEquipmentSettingsPage:cmsEquipmentSettingsPage,
  before: function(done) {
    var solrClient = browser.globals.solrWpEquipmentReviewsClient;
             equipmentReview.solr_equipment_review_entry(solrClient, function(err,cmsEquipmentSettings) {
  if(err) {
    console.log(err);
    done(err);
  } else {
    cmsEquipmentSettingsPage['setting'] = cmsEquipmentSettings;
   done();
  }
});
},

In my nightwatch.conf.js file I had to set; persist_globals: true,

And in my test script;

it('Globals displayed', async function (browser) {
    console.log(browser.globals.cmsEquipmentSettingsPage)
    })

Although this worked, it must be noted that it will only work in serial working. There is a bug raised for globals persisting in Parallel working.