Nightwatch: Assertation by link text

Created on 26 Oct 2016  ·  8Comments  ·  Source: nightwatchjs/nightwatch

It seems like the elements() webdriver functionality has a strategy partial link text. So I assume it might be possible to build a assert function which finds a link with a specific text on the page.
As far as I can see, there is no such function.
Is there a plan to add such a function, or would you be happy if I tried to write such a function and then submit a pull-request? Or is the my desired functionality "check if there is an element with a specific text on the page" achievable in a different way?

Most helpful comment

Thanks, I'll update the documentation.

All 8 comments

OK actually it looks like this is possible:
browser.expect.element('logout', 'link text').present;

But it is not really documented as the documentation states the parameters should be exactly the other way around...

...as the documentation states the parameters should be exactly the other way around...

Don't confuse browser.expect.element() with browser.element(). The expect API is separate from the standard commands API. Whereas browser.element() has a signature putting the locate strategy (or using) first followed by the selector (or value), browser.expect.element() works the other way around with the locate strategy after the selector.

browser.element('link text', 'logout')
// vs.
browser.expect.element('logout', 'link text')

The documentation covers browser.element() but does not include the optional locate strategy for browser.expect.element(). Similarly, element commands that are documented to only take a selector in the "Commands" section of the documentation (e.g. getText()) can also optionally accept a locate strategy argument, though it would be placed before the selector.

browser.getText('#my-div', callback)
// or 
browser.getText('css selector', '#my-div', callback)

Ah I see. I think it would make sense to make the documentation a bit more elaborate on that point. I was searching for quite some time for the possibility of checking if there is a link with a certain text and just stumbled by accident over this feature.

Thanks, I'll update the documentation.

OK one last question around this: Can I use this somehow to click the link I just found?
My problem is, that those methods don't give me a callback and that the click() command just accepts a css selector.

In our previous testframework (Capybara with a headless browser) we could do this:

  @settlement_link = page.find('a', text: 'logout')
  @settlement_link.click

But it seems that is not easily doable in Nightwatch...

The "Commands" section I mentioned in the documentation represents Nightwatch's own commands, most being fairly light wrappers around the more direct selenium (webdriver) protocol commands listed in the section below. These "Commands" commands simplify things by sticking mainly to css or xpath selectors and automatically find elements to perform operations on, such as the getText() example I used. That command is a combination of the protocol commands element() and elementIdText(), wrapped up in a easy to use package that automatically takes the global locate strategy (usually set with useCss() or useXpath()) and gives you that element's text all in one go.

But, as I mentioned in my previous message, using the getText as an example, these commands also accept an undocumented locate strategy that you can set before the selector. So you should be able to do something like this:

browser.click('link text', 'logout')

And if that doesn't work, you can bypass these convenience commands and go directly to the protocol commands. That would include calling browser.element() (analogous to find) and then browser.elementIdClick() (analogous to click though uses an id argument from what was found rather than being called from what was found).

Cool thanks!
As I said, I think everyone would benefit if all this undocumented parameters would be documented :)

Thanks for all your help!

browser.expect.element('logout', 'link text').present; is OK!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MateuszJeziorski picture MateuszJeziorski  ·  3Comments

Pieras2 picture Pieras2  ·  3Comments

Zechtitus picture Zechtitus  ·  4Comments

sgleonardoopitz picture sgleonardoopitz  ·  3Comments

davidlinse picture davidlinse  ·  4Comments