Having issue with page object command: "TypeError: Cannot read property 'pause' of undefined"

1,382 views
Skip to first unread message

Peter Buban

unread,
Jul 13, 2017, 10:03:43 AM7/13/17
to NightwatchJs
I wonder if anyone can help me out. I'm new to Selenium/Nightwatch and just can't figure out what's going on. 

I have configured a npm script to use babel with nightwatch, so please forgive the es6 syntax. 

Here is my page object:

const customCommands = {
  pause: (time) => {
    this.api.pause(time)
    return this
  }
}

module.exports = {
  commands: [customCommands],
  elements: {
    usernameField: 'input[type=text][name=username]',
    passwordField: 'input[type=password][name=password]',
    submitButton: 'button[type=submit]',
  }
}

Here is my test case:

// Login in to the CMS
function Login(client, role) {
  const pageObj = client.page.shared()
  pageObj
    // Login
    .setValue('@usernameField', role.username)
    .setValue('@passwordField', role.password)
    .pause(1000) // Allow animation for submit button to enable interaction
    .click('@submitButton')
    // Home page loading
    .pause(3000)
    // Check customer name is correct
    .assert.containsText('.jbs-breadcrumbs li:first-child a', 'test')
}

module.exports = {
   Login
}



I've tried writing the command function in different ways, returning this.api/this but nothing seems to work. I keep getting this error:

  TypeError: Cannot read property 'pause' of undefined


Can anyone help me out with this? I've hit a brick wall.

Andrew King

unread,
Jul 13, 2017, 12:25:15 PM7/13/17
to NightwatchJs
Oddly, I just opened a bug about that on github.  If you use a this.api call in a page object, this gets turned in to a browser object, and you can call this.pause() directly.  Which seems quite retarded.

Peter Buban

unread,
Jul 13, 2017, 12:34:03 PM7/13/17
to NightwatchJs
I got a little closer to getting it working. My new page object file looks like this. Hopefully this helps you out. It's equally frustrating and interesting but at the moment, it's taking much longer to implement tests than I would have expected. 

module.exports = {
  elements: {
    usernameField: 'input[type=text][name=username]',
    passwordField: 'input[type=password][name=password]',
    submitButton: 'button[type=submit]'
  },
  commands: [{
    cmdPause: (client, duration) => client.pause(duration)
  }]
}

Andrew King

unread,
Jul 13, 2017, 12:37:35 PM7/13/17
to NightwatchJs
I'll see if I can work up a supporting page_object call over the weekend.  But you can wreck variables with a this.api call.  Something goes stupid to be sure.

Eric Mumford

unread,
Aug 18, 2017, 1:22:28 AM8/18/17
to NightwatchJs
Use console.log to view your object. Generally the pause is available in the api object. You are likely suffering from scope issues if its not working.

Bad juju to use pause() anyway.


On Thursday, July 13, 2017 at 10:03:43 AM UTC-4, Peter Buban wrote:
Reply all
Reply to author
Forward
0 new messages