Nightwatch: Support for pseudo selectors

Created on 23 Oct 2015  路  19Comments  路  Source: nightwatchjs/nightwatch

Starting in #696. It would be great to support pseudo selectors (eg. :nth-child(X)), especially together with page objects (eg. @element:nth-child(5)). Currently when I need to find element in the set I have switch to XPath _AND_ on top of that I've to forget about using page objects because they cannot be composed together with dynamic information from within the actual test.

enhancement

Most helpful comment

@beatfactor . Please review this issue. You just closed it, and said that is the duplicate of the same issue :)
Also, I can't find any news about this issue. Is it implemented in Nightwatch.js?
This feature could be crucial for using Nightwatch page object pattern.

All 19 comments

Same here ! :+1:

I also would love to see this feature implemented! :+1:

Please update this feature.

Has this been implemented successfully?

Huge :+1:

Parametrized sections/elements is a must. What is a point of testing static xhtml structure.
:+1:

@beatfactor Um are you sure you got correct number? You are referencing this issue...

@FredyC this is something I've been working on: #1116; does it seem to satisfy your needs?

@senocular Well I don't have any needs as I am not using nightwatch in any of my projects right now, but I am sure many of others here will be happy about it.

Just to add to the voices who really want this feature. I'm actively using Nightwatch in a large project and it is extremely painful to do something as simple as select the n-th element of an array of elements retrieved by a single selector, especially using Page Objects. This would help immensely.

@senocular I believe that the #1116 fixes only small portion of the lack of support for dynamic selectors. The idea would be great if you will also allow a user-parameters.
I would like to use a selector like this: "//*[local-name() ='tspan' and .='${var1}']"
or
"//a[.='${var1}']"

In most cases you could just write a function to build your element selector, but it is going into mess if you have half of the elements in nicely organized structure and than bunch of magic-strings in the functions. Also this will not cover a scenario when you have a dynamic element with a sub-section. In fact in that case you can call a page.model().section.level1.section.level2.expect.element(giveMeDynamic(var1, var2)).to.be....
But it will not do so if you need to have a dynamic section.
I ussually have mix of the structure of xpath and css selectors, because there is things that are supported in only one of them.

@Ftelf #1116 is a step in that direction. An object-based selector allows it to be parameterizable with additional data (such as index). One of the bullet points in the PR description refers to this. So something like...

  elements: {
    searchBar: { 
      selector (var1) {
        return "//*[local-name() ='tspan' and .='${var1}']";
      }
    },
    // ...

then

page.model().section.level1.section.level2.expect.element({selector:'@searchBar', var1:'foo'}).to.be...

That's the general idea, at least. I still haven't fully flushed the best way to call the function with that data.

@beatfactor . Please review this issue. You just closed it, and said that is the duplicate of the same issue :)
Also, I can't find any news about this issue. Is it implemented in Nightwatch.js?
This feature could be crucial for using Nightwatch page object pattern.

Hi, would still like to see this implemented.

Ok, so @senocular's solution is available in v1.0 so you can do the equivalent of @element:nth-child(5). I will look into the possibility of supporting pseudo css selectors as well.

@beatfactor, you've been the MAN the last few months, Nightwatch has improved tremendously the last few years since I first used it.

If we can get :before and :after CSS selectors (even if only available with XClass or etc), that would solve 90% of the pseudo-selector drama.

I know it's up to the browser webdrivers to initially add support, but once Nightwatch has it integrated you will become a testing framework candidate for a ton of people.

(For reference, now that Nightwatch is progressing as a framework, I'm adopting it into my professional environments as well as personal)

@lacymorrow Thanks for your support, although I would say that Nightwatch has been improving lately as a result of a team effort, a strong collaboration between the maintainers and contributors.

We just need to get all the main issues sorted out first and then we'll add more improvements, such as this one.

This has been added in v1.1.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgaticaq picture lgaticaq  路  3Comments

dakebl picture dakebl  路  4Comments

gary5 picture gary5  路  4Comments

t00f picture t00f  路  3Comments

Zechtitus picture Zechtitus  路  4Comments