Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
268 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Page, ElementHandle, WaitForSelectorOptions } from "puppeteer"; | ||
const testIdAttribute = "data-test-id"; | ||
const valueAttribute = "value"; | ||
|
||
const getTestId = (testId: string) => { | ||
return `[${testIdAttribute}="${testId}"]`; | ||
}; | ||
|
||
const waitForTestId = async ( | ||
page: Page, | ||
testId: string, | ||
options?: WaitForSelectorOptions, | ||
): Promise<ElementHandle<Element>> => { | ||
return await page.waitForSelector(getTestId(testId), options); | ||
}; | ||
|
||
const waitForDOMByValue = async ( | ||
page: Page, | ||
domValue: string, | ||
options?: WaitForSelectorOptions, | ||
): Promise<ElementHandle<Element>> => { | ||
return await page.waitForSelector( | ||
((domValue: string) => { | ||
return `[${valueAttribute}="${domValue}"]`; | ||
})(domValue), | ||
options, | ||
); | ||
}; | ||
|
||
const clickTestId = async (page: Page, testId: string) => { | ||
await page.click(getTestId(testId)); | ||
}; | ||
|
||
const waitAndClickTestId = async (page: Page, testId: string) => { | ||
await waitForTestId(page, testId); | ||
await page.click(getTestId(testId)); | ||
}; | ||
|
||
const getValueOfTestId = async ( | ||
page: Page, | ||
testId: string, | ||
): Promise<string> => { | ||
const element = await page.$(getTestId(testId)); | ||
return await page.evaluate(element => element.textContent, element); | ||
}; | ||
|
||
const assertTestIdContains = async ( | ||
page: Page, | ||
testId: string, | ||
contains: string, | ||
): Promise<void> => { | ||
const value = await getValueOfTestId(page, testId); | ||
expect(value).toContain(contains); | ||
}; | ||
|
||
export { | ||
getTestId, | ||
clickTestId, | ||
waitForTestId, | ||
waitAndClickTestId, | ||
getValueOfTestId, | ||
assertTestIdContains, | ||
waitForDOMByValue, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Puppeteer test-id helpers | ||
|
||
``` | ||
import { | ||
getTestId, | ||
clickTestId, | ||
waitForTestId, | ||
waitAndClickTestId, | ||
getValueOfTestId, | ||
assertTestIdContains, | ||
waitForDOMByValue, | ||
} from `"@kablamo/kerosene-test/src/puppeteer` | ||
``` |
Oops, something went wrong.