Search code examples
javascriptcucumbernightwatch.js

Nightwatch + Cucumber How to describe a generic step


I'm writing some e2e tests using nightwatch and cucumber. I'd like to have a generics steps. Now, my step definition is like this:

 When (/^I click on 'Leave a comment'$/, () => {
    return client
    .click('a[id="leave-comment"]')
  });

I'd like some like:

When (/^I click on  "(.*?)"$/, (text) => {
 ...
});

But I can't implement this step using .click. Any idea?


Solution

  • This solution that I found. I needed to define the type of the element and I used xPath

    When (/^I click on  "(.*?)"$/, (text) => {
     return client
        .useXpath()
        .waitForElementVisible('//button[text()="'+text+'"]', 7000)
        .click('//button[text()="'+text+'"]')
        .useCss()
        .pause(1000);
    });