mtage70
mtage70

Reputation: 21

Nightwatch attributeContains with multiple acceptable values

I have a successful test

browser
      .url(testURL)
      .waitForElementPresent('body', 1000)
      .verify.attributeContains('someElement', 'someAttribute', 'foo')

But for my purposes it is acceptable for 'someAttribute' to contain 'foo' OR 'bar'. I'm wondering how I can write this kind of test so that no test failures are reported by Nightwatch.

Upvotes: 2

Views: 1883

Answers (2)

Stiliyan
Stiliyan

Reputation: 1174

You can test if attribute contains 'foo' OR 'bar' in two steps:

  1. get the attribute value with getAttribute() or attribute()
  2. match a regex against the value

With getAttribute(), use regex.test():

browser.getAttribute('someElement', 'someAttribute', function(result) {
    this.assert.value(/foo|bar/.test(result.value), true);
};

With attribute(), use matches() assertion:

browser.expect.element('someElement').to.have.attribute('someAttribute')
    .which.matches(/foo|bar/);

Upvotes: 3

Ray
Ray

Reputation: 1676

use .elements() and obtain the length of element result to avoid fail message.

.elements('css selector','someElement[yourattribute="foo"]',function(result){
    if(result.value.length>0){ //element exists 
       console.log('somelement is here')  
    }
    else{
      console.log('not here')
    }  
});

Upvotes: 0

Related Questions