Skip to content

Instantly share code, notes, and snippets.

@nchursin
Created July 24, 2021 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nchursin/ee0c1595987a037a10a50bcdb5695988 to your computer and use it in GitHub Desktop.
Save nchursin/ee0c1595987a037a10a50bcdb5695988 to your computer and use it in GitHub Desktop.
locators.ts proposal
import { Question } from '@serenity-js/core';
import type { Browser, Element, ElementArray, Selector } from 'webdriverio';
import { BrowseTheWeb } from '../abilities';
export class Locator {
constructor(
private readonly description: string,
private readonly selector: Selector
) {
}
firstMatching(): Question<Promise<Element<'async'>>> {
return Question.about(this.description, actor =>
BrowseTheWeb.as(actor).browser.$(this.selector)
)
}
allMatching(): Question<Promise<ElementArray>> {
return Question.about(this.description, actor =>
BrowseTheWeb.as(actor).browser.$$(this.selector)
)
}
}
export class Locators {
id(id: string): Locator {
return new Locator(
`by id #${ id }`,
`#${id}`
)
}
css(selector: Selector): Locator {
return new Locator(
`by css ${ selector }`,
selector
)
}
tagName(tagName: string): Locator {
return new Locator(
`by tag name <${ tagName } />`,
`<${ tagName } />`
)
}
linkText(linkText: string): Locator {
return new Locator(
`by link text ${ linkText }`,
`=${ linkText }`
)
}
partialLinkText(partialLinkText: string): Locator {
return new Locator(
`by partial link text ${ partialLinkText }`,
`*=${ partialLinkText }`
)
}
xpath(xpath: string): Locator {
return new Locator(
`by xpath ${ xpath }`,
xpath
)
}
}
export const by = new Locators();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment