简体   繁体   中英

Nightwatch.js function not 'closing'

I'm trying to perform a function at the beginning of my test, then the rest of the test should be executed.

This is my custom-command (named internalAdviceLinksHtml ):

var solr = require('solr-client')

exports.command = function() {
  this
  var client = solr.createClient('solr.dev.bauerhosting.com', 8080, 'cms', '/www.parkers.co.uk');
  var globalSettingsQuery = client.createQuery()
      .q({TypeName:'Bauer.Parkers.GlobalSettings'})
      .start(0)
      .rows(10);  

  client.search(globalSettingsQuery,function(err,obj) {
    if (err) {
      console.log(err);
    } else {      
      var myresult = (obj.response.docs[0].s_InternalAdviceLinksHtml);

      console.log(myresult.length);
      if (myresult.length === 0) {
        console.log('content block not configured');
      } else {    
        console.log('content block configured');
      }
    }
  });        
  return this;
};

Test-file ( script ):

module.exports = {
  'set up the solr query': function (browser) {
    browser
      .solr_query.global_settings.internalAdviceLinksHtml();
  },

  'links above footer on advice landing page displayed': function (browser) {
    browser
      .url(browser.launch_url + browser.globals.carAdvice)
      .assert.elementPresent('section.seo-internal-links')
  },

  'closing the browser': function (browser)  {
    browser
      .browserEnd();
  },  
}; 

The function works correctly (ie if myresult length is 0 then "content block is not configured" is displayed, etc), but the following test ( "links above footer on advice landing page displayed" ) is never invoked.

It seems like the execution stops after the custom-command . I'm sure this will be something quite obvious to someone, but I just can't seem to see what it is.

Any help would be greatly appreciated.

Regarding your internalAdviceLinksHtml custom-command, everything looks good from my point of view ( I presume that lonely this was a typo ).

Your hunch is correct, it seems that the Nightwatch test-runner fails to go to the next test, which is probably due to some promise not being resolved upstream ( client.search function from internalAdviceLinksHtml ).

I would recommend doing a return this immediately after outputting to console ( content block not configured , or content block configured ) and see if that fixes the problem:

  client.search(globalSettingsQuery,function(err,obj) {
    if (err) {
      console.log(err);
    } else {      
      var myresult = (obj.response.docs[0].s_InternalAdviceLinksHtml);

      console.log(myresult.length);
      if (myresult.length === 0) {
        console.log('content block not configured');
      } else {    
        console.log('content block configured');
      }
    }
    return this
  });

Also, a few extra pointers:

  • make use of the Nightwatch test-hooks to make your tests easier to read/maintain & create a separation of concern ( setup => before / beforeEach hooks | teardown (eg: browser.end() ) => after / afterEach hooks);
  • you need not do an explicit browser.end() at the end of your test case. Check this answer for more information on the matter.

Your test-file would become:

module.exports = {
  // > do your setup here <
  before(browser) {
    browser
      .solr_query.global_settings.internalAdviceLinksHtml();
  },

  'links above footer on advice landing page displayed': function (browser) {
    browser
      .url(browser.launch_url + browser.globals.carAdvice)
      .assert.elementPresent('section.seo-internal-links');
  },
  // > do your cleanup here <
  after(browser)  {
    browser
      .browserEnd();
  },  
}; 

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