简体   繁体   中英

Nightwatch Abort Test on Pass

I'm writing a script in Nightwatch that tests a specific element on a page. It's possible that the script could be testing a URL in which the element is not present on the page, in which case I want the script to end the test without any failures being logged.

I cannot seem to find a way to abort the test early without invoking a failure, however. Is there any way to have a Nightwatch test abort on a pass?

Here's a part of the code I'm working with for reference:

//End test if pagination is not present
'Pagination Present' : function (browser) {
  browser
  .execute(function() {
    return document.querySelectorAll("ul[class='pagination']").length;
  }, 
  function(count){
    if (count.value == 0) {
      browser.assert.equal(count.value, 0, "There is no pagination on this page.");
      browser.end();
    }
  })
},

Invoking browser.end(); closes the browser, but it reopens immediately after and the tests continue. Every single case fails, since the pagination does not exist on the given page. I'd like to end the test immediately after browser.assert.equal passes. Is there any way to do so?

You can use try/catch.

I had the same issue with some tests and i've got it to skip that assertion like this: you try to check something, but if you don't find the element, instead of failing the test, i just display a message in the console and exit the whole test:

'Test product\'s UPSs' : function (browser) {        
    try {
       browser.assert.elementPresent('@someElement');          
    }

    catch(err) {           
       console.log('this product has no Special features! Skipping');
       process.exit();
    }
}

In case you have further tests that you know they wouldn't fail and you'd like to continue with them, just leave out the process.exit() function. While it might not be the safest way to do it, at least it gets the job done.

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