wironxxx
wironxxx

Reputation: 1

Get attribute or elements from page by script in nightwatch

I need a check that class elements was ui-state-active.

class="ui-state-default ui-state-active" href="#">15</a>

I am trying to using all methods and don't undestand how to write a code:

.perform(function () {
    var arrAttr = [];
    var arrCssProp = [];
    var arrValue= [];
    for (i = 1; i < 8; i++) {
      for (j = 1; j < 7; j++) {            
        browser.useXpath();
        browser.getCssProperty('//*[@id="ui-datepicker-div"]/table/tbody/tr[' + i + ']/td[' + j + ']', '.ui-state-default', function (result) {
          arrCssProp[j, i] = result.value;              
        });
        browser.getAttribute('//*[@id="ui-datepicker-div"]/table/tbody/tr[' + i + ']/td[' + j + ']', '.ui-state-default', function (result) {
          arrAttr[j, i] = result.value;
        });
        browser.getValue('//*[@id="ui-datepicker-div"]/table/tbody/tr[' + i + ']/td[' + j + ']', '.ui-state-default', function (result) {
          arrValue[j, i] = result.value;
        });
        browser.useCss();
        writeLogLine('arrAttr:' + i + "|" + j + ' :' + arrAttr);
        console.log('arrAttr:' + i + "|" + j + ' :' + arrAttr);
        writeLogLine('arrCssProp:' + i + "|" + j + ' :' + arrCssProp);
        console.log('arrCssProp:' + i + "|" + j + ' :' + arrCssProp);
        writeLogLine('arrValue:' + i + "|" + j + ' :' + arrValue);
        console.log('arrValue:' + i + "|" + j + ' :' + arrValue);
      }
    }
  })

Upvotes: 0

Views: 5049

Answers (2)

Like in Rohit's example, if you want to use an attribute value, all the commands that rely on it need to be inside the callback. Doing arrCssProp[j, i] = result.value; won't work, since that value will disappear as soon as you exit the callback.

For more info, check out https://github.com/nightwatchjs/nightwatch/wiki/Understanding-the-Command-Queue

Upvotes: 0

rohit.jaryal
rohit.jaryal

Reputation: 440

To get the attribute of class, you can write similar code

browser.getAttribute(pageObject.getElement('@ui'), "class", function (result) {
if (result.value.indexOf("ui-state-active")!=-1) {
    console.log('class name:' + result.value);
}
else { console.log('class name:' + result.value); }});

You can use any locator strategy. To use CSS strategy based on class name, use .ui-state-default along with tagname.

Hope this helps.

Upvotes: 1

Related Questions