Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced CSS selectors eg. :contains('John') #60

Closed
petrvojta opened this issue Feb 24, 2014 · 34 comments
Closed

Advanced CSS selectors eg. :contains('John') #60

petrvojta opened this issue Feb 24, 2014 · 34 comments
Labels

Comments

@petrvojta
Copy link

@beatfactor - I would like to suggest to add support for extended CSS selector for selecting elements with specific text. Similar to JQuery http://api.jquery.com/contains-selector/. It would be usefull for testing worflow. Eg. for testing of creating / updating / deleting records.

browser.click("ul li a:contains('Record')");

or

browser.click("ul li a[text='Record']");

Maybe XPath selectors could handle it?
Thank you!
Btw. Nightwatch.js is cool :-)

@beatfactor
Copy link
Member

Thanks, the :contains pseudo-selector is not in the w3c spec so it won't work, but the other example should work.

@petrvojta
Copy link
Author

Thank you for your quick answer. Unfortunately it doesn't work. I have to wait for XPATH implementation ther #33. Thanks. P.

@beatfactor
Copy link
Member

Just to let you know, this is also discussed on the mailing list and someone had managed to achieve this with xpath: https://groups.google.com/forum/#!topic/nightwatchjs/d0p72Ub9Idg

@petrvojta
Copy link
Author

Perfect 👍 I vote for :contains() (it will keeps a simplicity and beauty of Nightwatch.js). later you can add a XPATH support.

@haggyj
Copy link
Contributor

haggyj commented Apr 7, 2014

+1 for :contains

@neilrees
Copy link

neilrees commented Apr 7, 2014

+1 : Finding elements in collections by nth-child is too brittle, :contains() would be 👍

@davidlinse
Copy link
Contributor

👎 i would vote agains :contains() as @beatfactor already mentioned

the :contains pseudo-selector is not in the w3c spec

and go for real XPath support instead..

Just my two cents
~david

@beatfactor
Copy link
Member

Without native browser css support, this it won't probably be implemented. I could look into delegating to jQuery (if available) via execute or converting into xpath.

@VanSteve
Copy link

+1 for :contains - as this is a very widely used selector that makes for infinitely simpler to read code than the mess of troubleshooting that appears to be required to get the equivalent messier xpath queries working in nightwatch on even a basic level

@janpaul123
Copy link

+1 for :contains. Widely used, and CSS is a much more familiar to many web developers than XPath.

@tslater
Copy link

tslater commented Dec 16, 2014

+1

@maxgalbu
Copy link

If you happen to use jquery in your site, you can use jqueryClick() from my custom commands:

https://github.com/maxgalbu/nightwatch-custom-commands-assertions

@mdarif
Copy link

mdarif commented Feb 19, 2015

+1

1 similar comment
@yidea
Copy link

yidea commented Mar 2, 2015

+1

@damassi
Copy link

damassi commented Apr 10, 2015

@maxgalbu -- Awesome! Thanks for all of these; very useful.

@jurgob
Copy link

jurgob commented Jun 17, 2015

+1

8 similar comments
@genintho
Copy link

👍

@adamwardecki
Copy link

+1

@dbuarque
Copy link

+1

@panfu
Copy link

panfu commented Jul 28, 2015

+1

@xioguru
Copy link

xioguru commented Nov 4, 2015

+1

@Richard-Bady
Copy link

+1

@mrjumpy
Copy link

mrjumpy commented Jan 3, 2016

+1

@idMolotov
Copy link

+1

@montogeek
Copy link

montogeek commented Apr 29, 2016

@beatfactor Does Nightwatch support :after and :before?

@yumikohey
Copy link

@montogeek I don't think so. They are pseudo in CSS. jQuery doesn't even support to query them.

@montogeek
Copy link

@yumikohey Yep, I ended testing the element in a different way

@joaomilho
Copy link

+1

1 similar comment
@ecuan
Copy link

ecuan commented Jan 27, 2017

+1

@martinp999
Copy link

@beatfactor

+1 :contains

FYI and just to reconfirm - a[text='blah'] does not work in jQuery or Nightwatch.

Looks like I also have to resort to the clunkiness of XPath. :-(

@kristinaluxgroup
Copy link

+1

@rchipka
Copy link

rchipka commented Nov 10, 2017

An alternative would be to convert your CSS selectors to XPath using css2xpath.

An added benefit is that you can use CSS/XPath selector hybrids:

a > #css.example//with/some::child(xpath) ~ .that:contains('css')

which converts to:

.//a/*[@id="css"][contains(concat(" ",normalize-space(@class)," ")," example ")]//with/some::child(xpath)/following-sibling::*[contains(concat(" ",normalize-space(@class)," ")," that ")][contains(normalize-space(),"css")]

It also supports :icontains for case insensitive matching, reverse combinators (!>, ! , !^, !+, !~), :starts-with, :ends-with, :first, :last, :has, :has-(sibling|child|ancestor), etc.

You can use the live css2xpath demo to test your selectors.

For more information on what it can do, you can check out the source or contribute some documentation.

@rchipka
Copy link

rchipka commented Nov 10, 2017

var c2x = require('css2xpath');
...
browser
  .useXpath()
  .click(c2x(':contains("Log in")'))

@stocarul
Copy link

+1

@nightwatchjs nightwatchjs locked as resolved and limited conversation to collaborators May 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests