protractor-gherkin-cucumberjs-angular icon indicating copy to clipboard operation
protractor-gherkin-cucumberjs-angular copied to clipboard

example of custom locator

Open samvloeberghs opened this issue 8 years ago • 9 comments

samvloeberghs avatar Feb 22 '17 19:02 samvloeberghs

  • 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

DennisJaamann avatar Feb 23 '17 12:02 DennisJaamann

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

samvloeberghs avatar Feb 23 '17 14:02 samvloeberghs

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

sbley avatar Jun 28 '19 13:06 sbley

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

samvloeberghs avatar Jul 04 '19 11:07 samvloeberghs

@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.

sbley avatar Jul 04 '19 11:07 sbley

@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.

samvloeberghs avatar Jul 04 '19 11:07 samvloeberghs

Thank you Sam, I will give it a try 👍

sbley avatar Jul 04 '19 11:07 sbley

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

sbley avatar Jul 05 '19 14:07 sbley

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

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

sbley avatar Jul 06 '19 14:07 sbley