kro91
kro91

Reputation: 51

Using elements within API functions in Nightwatch JS

I have a page object and am creating a command for use in my test file. When I use a WebDriver API command like .elements(), the elements that I created are not passed through and cannot be used in the callback function.

Example code:

var commands = {
  command1: function () {
    var element1 = "div.some-class"; //I end up doing this
    this.api
      .elements("css selector", "@element1", function (result) {
        return this
          .click("@element2");
      })  
  }
}

module.exports = {
  url: function() {
    return this.launchUrl;
  },
  elements: {
    element1: "div.some-class",
    element2: "h2[id=some-id]"
  },
  commands: [commands]
}

I have noticed that calling .api makes it so you cannot use elements, but is there any way around this? I have been making variables for each of my commands, but I feel like that defeats the purpose of having elements.

Upvotes: 0

Views: 821

Answers (1)

Hikaryu
Hikaryu

Reputation: 317

to make it more generic inside a custom function u can use:

var objectSelector = this.page.pageobject.elements[elementName]

it should return element1 css: div.some-class

if i gona think about better solution will post it here in a comment

Upvotes: 1

Related Questions