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

example of custom locator #51

Open
samvloeberghs opened this issue Feb 22, 2017 · 9 comments
Open

example of custom locator #51

samvloeberghs opened this issue Feb 22, 2017 · 9 comments

Comments

@samvloeberghs
Copy link
Owner

No description provided.

@DennisJaamann
Copy link
Collaborator

DennisJaamann commented Feb 23, 2017

  • Add custom locator
protractor.By.addLocator('e2eId', function (toState, opt_parentElement) {
        var using = opt_parentElement || document;
        var possibleAnchors = using.querySelectorAll('*[ta-e2e-id="' + toState +'"]');
        var result = null;
       if (possibleAnchors.length === 1) {
            result = possibleAnchors[0];
        } else {
            result = possibleAnchors;
        }

        return result;
    });
  • Register in protractor.conf
  • Update htmls to have e2e-id

@samvloeberghs
Copy link
Owner Author

samvloeberghs commented Feb 23, 2017

small suggestion:

protractor.By.addLocator('e2eId', (toState, opt_parentElement) => {
       const using = opt_parentElement || document;
       const possibleAnchors = using.querySelectorAll('*[ta-e2e-id="' + toState +'"]');
       let result = null;
       if (possibleAnchors.length === 1) {
            result = possibleAnchors[0];
        } else  if (possibleAnchors.length > 1) {
            result = possibleAnchors;
        }

        return result;
    });

mind the else if statement

@sbley
Copy link

sbley commented Jun 28, 2019

How do you register the locator in protractor.conf.js?

@samvloeberghs
Copy link
Owner Author

@sbley sorry this project is inactive and you probably already googled the answer.
https://www.protractortest.org/#/api?view=ProtractorBy.prototype.addLocator

@sbley
Copy link

sbley commented Jul 4, 2019

@samvloeberghs Thanks for the pointer. I have seen that before but tbh it leaves my question unanswered. I can't figure out where in protractor.conf.js I am supposed to put by.addLocator(..) to register it.

@samvloeberghs
Copy link
Owner Author

@sbley basically anywhere where you have access to by before you use the locator.
For example in the onPrepare callback. See example here:

https://github.com/angular/protractor/blob/master/spec/withLoginConf.js

There might be better places, but that should work.

@sbley
Copy link

sbley commented Jul 4, 2019

Thank you Sam, I will give it a try 👍

@sbley
Copy link

sbley commented Jul 5, 2019

@samvloeberghs Do you also know a way to add the type definitions of the locator to TypeScript?

@sbley
Copy link

sbley commented Jul 6, 2019

I can now answer this myself: Add it to the includes part of your tsconfig.e2e.json:

{
  "compilerOptions": { },
  "includes": ["./my-custom-locator.ts"]
}

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

No branches or pull requests

3 participants