javascriptselenium-webdrivernightwatch.js

Nightwatch using same element selector for each call


Is there a way to avoid using element's selector twice or more when executing several actions on the same element?

E.g. ('Some test', function (browser) { browser .waitForElementVisible('selector', 10000) .click('THE SAME selector') }),

Can't it be arranged just like in Cypress?

Cy
  .get('element')
  .should('be.visible')
  .click()    - like 3 actions with only one mentioning of the element's selector

Or to save the element as a variable and use it then?

I have searched throughout the Internet but haven't found any proper topic


Solution

  • The way that you would do this is to move the locator into a page object or into a variable as described in the API Docs

    The reason why NightwatchJS doesn't follow the Cypress pattern is that we allow chaining of all commands. This means that you can create more complex tests.

    const githubButton = 'a[aria-label="Nightwatch on Github"]';
    
      before(browser => browser.navigateTo('https://nightwatchjs.org/'));
    
      after(browser => browser.end());
    
      it('click element', function (browser) {
        browser
          .click(githubButton)
      });    
    
    

    A click can only happen if it is visible in Nightwatch so you don't need to add an assert for it before clicking