最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handle multiple elements with the same selector with nightwatch.js - Stack Overflow

programmeradmin1浏览0评论

I'm using nightwatch-cucumber with the use of PageObjects to automate tests. nightwatch-cucumber based on nightwatch.js. So, I'm pletely new to JavaScript. So far, I prefered using Java as language for Selenium/WebDriver automation.

I want to edit multiple input fields with defined values. The problem is that all these input fields have the same selector. And the number of the input fields is not known at the beginning of a test or is different from test to test. So, I need a solution. In the "holy" java world I can do something like this:

List<WebElement> listOfElements = webdriver.getElements('input.myclass');
for (WebElement el : listOfElements) {
    el.sendKeys("abc");
}

In JavaScript it's a little bit tricky and I don't know to handle with. In my PageObject I tried the following:

module.exports = {
  elements: {},
  mands: [{
    test() {
      this.api.elements('css selector', 'input.myclass',function (result) {
        for (var i = 0; i < result.value.length; i++) {
          console.log(result.value[i].ELEMENT);
          this.api.elementIdValue(result.value[i].ELEMENT, 'abc');
        }
      });

      this.api.pause(3000);
      return this.api;
    }
  }]
};

But this worked not for me. In this case I get an error while test execution (TypeError: Cannot read property 'elementIdValue' of undefined). Also, I don't want to handle everything in asynchronous callback function, because the following test steps maybe require the pleted handling on the multiple input fields.

So, how can I implement such a solution to handle multiple elements in nightwatch-cucumber with and without callback functions? And what do you prefer in that case?

I'm using nightwatch-cucumber with the use of PageObjects to automate tests. nightwatch-cucumber based on nightwatch.js. So, I'm pletely new to JavaScript. So far, I prefered using Java as language for Selenium/WebDriver automation.

I want to edit multiple input fields with defined values. The problem is that all these input fields have the same selector. And the number of the input fields is not known at the beginning of a test or is different from test to test. So, I need a solution. In the "holy" java world I can do something like this:

List<WebElement> listOfElements = webdriver.getElements('input.myclass');
for (WebElement el : listOfElements) {
    el.sendKeys("abc");
}

In JavaScript it's a little bit tricky and I don't know to handle with. In my PageObject I tried the following:

module.exports = {
  elements: {},
  mands: [{
    test() {
      this.api.elements('css selector', 'input.myclass',function (result) {
        for (var i = 0; i < result.value.length; i++) {
          console.log(result.value[i].ELEMENT);
          this.api.elementIdValue(result.value[i].ELEMENT, 'abc');
        }
      });

      this.api.pause(3000);
      return this.api;
    }
  }]
};

But this worked not for me. In this case I get an error while test execution (TypeError: Cannot read property 'elementIdValue' of undefined). Also, I don't want to handle everything in asynchronous callback function, because the following test steps maybe require the pleted handling on the multiple input fields.

So, how can I implement such a solution to handle multiple elements in nightwatch-cucumber with and without callback functions? And what do you prefer in that case?

Share Improve this question asked Jun 12, 2017 at 10:00 GRmeGRme 2,7676 gold badges29 silver badges50 bronze badges 1
  • There's no way to avoid the callback since all the mands are executed asynchronously. As for the error, it is due to the context of this which holds a different reference in the callback provided to this.api.elements. – Florent B. Commented Jun 12, 2017 at 11:36
Add a ment  | 

1 Answer 1

Reset to default 4

thx to @Florent B.

I answered my own question. The following code works for me now:

module.exports = {
  elements: {},
  mands: [{
    test() {
      this.api.elements('css selector', 'input.myclass',function (result) {
        for (var i = 0; i < result.value.length; i++) {
          this.elementIdValue(result.value[i].ELEMENT, 'abc');
        }
      });

      return this.api;
    }
  }]
};

Because the code inside the callback function runs asynchronous I think you have to wait for a special condition (e.g. with waitForElementVisible) to go on with the automated test. Because sometimes you have to for the successful pleted callback function before keep going with next steps in automated tests.

发布评论

评论列表(0)

  1. 暂无评论