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

[feature] Make api calls allow objects as selectors #1015

Closed
EloB opened this issue Jun 9, 2016 · 6 comments
Closed

[feature] Make api calls allow objects as selectors #1015

EloB opened this issue Jun 9, 2016 · 6 comments

Comments

@EloB
Copy link

EloB commented Jun 9, 2016

I been using nightwatch with page objects and sometimes I need to switch between css selectors and xpath. I want as clean code as possible. Here an example below that explains the feature request.

Current solution

module.exports = {
  elements: {
    something: '.something'
  },
  commands: [{
    doSomething() {
      this.waitForElementPresent('@something');
      this.api.useXpath();
      this.click(`//*[text()='Some custom text']`);
      this.api.useCss();
      return this;
    }
  }]
};

Wanted solution

import {
  xPath // === export const xPath = selector => ({ selector, locateStrategy: 'xpath' })
} from 'nightwatch'; 

module.exports = {
  elements: {
    something: '.something'
  },
  commands: [{
    doSomething(customText) {
      return this
        .waitForElementPresent('@something');
        .click(xPath(`//*[text()='${customText}']`)); // So the selector argument need to support object
    }
  }]
};
@beatfactor
Copy link
Member

This would be fixed by supporting parametrized page object elements. There is an open PR regarding this: #1001. You could also create a custom command, e.g. "clickXpathElement" that would take care of the css/xpath switch.

@Tex-Hex
Copy link

Tex-Hex commented Nov 23, 2016

I also ran into this issue (having to switch between this. and this.api.).
For me, following solution works fine:
Returning "this.api" on custom commands and within each line of testing use "this.api.".

var myCommand = {
  doSomeStuff: function() {
    this.api
        .waitForElementVisible('.backend ul.list')
        .useXpath()
        .click("//a[.='Publish']")
        .useCss()
        .waitForElementVisible(".container")
    return this.api;
  }
};

@EloB
Copy link
Author

EloB commented Nov 24, 2016

What I would like this on all commands not just the .click() and fallback to css selectors.

@Tex-Hex
Copy link

Tex-Hex commented Nov 28, 2016

Ah, ok, got it. Thought the point was chaining commands...
Right, object as selectors would be great when reusing the same command with different selectors.

@senocular
Copy link
Contributor

senocular commented Nov 28, 2016

@EloB It sounds like what you're after is already in master via #1156 (a custom xPath function could easily return one of these formatted objects). But this isn't getting pushed into releases because of #1237.

@Tex-Hex You need to be careful about returning this.api. Once you do that, though you have access to all the commands again, your chaining is no longer in the scope of the page object, so you'll lose the ability to use page commands or @-element selectors.

I remember discussing the possibilities of expanding the page object api to include all commands, but there are some road blocks, notably collisions with url and elements which exists as separate implementations in both page objects and the standard api. Let me see if I can dig up that original discussion... I think this is it: #884

@EloB
Copy link
Author

EloB commented Nov 28, 2016

@senocular Looks promising... I would still want a helper to make it a bit shorter... To avoid lines longer than 120 chars. :)

// My example
import { xPath } from 'nightwatch';
this.api.click(xPath(`//*[text()='${customText}']`));

// vs current implementation
this.api.click(({selector:'//*[text()='${customText}']', locateStrategy:'xpath'});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants