Skip to content

Instantly share code, notes, and snippets.

@RomkeVdMeulen
Last active March 21, 2018 13:08
Show Gist options
  • Save RomkeVdMeulen/1451cbb3866d4eaf501115cc2f9e6065 to your computer and use it in GitHub Desktop.
Save RomkeVdMeulen/1451cbb3866d4eaf501115cc2f9e6065 to your computer and use it in GitHub Desktop.
Add a `by.marker` locator to Protractor to match elements marked with an "e2e" attribute
import {Locator} from "protractor/built/locators";
declare module "protractor/built/locators" {
export interface ProtractorBy {
marker(marker: string, parentElement?: Node): Locator;
}
}
by.addLocator("marker", (marker: string, parentElement?: Element) => {
const scope = parentElement || document;
const matches = scope.querySelectorAll(`[e2e="${marker}"]`);
if (matches.length === 0) {
return null;
} else if (matches.length === 1) {
return matches[0];
}
return matches;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment