-
Notifications
You must be signed in to change notification settings - Fork 9
Add some helpful utilities #42
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54194cf
Add some helpful utilities
NullVoxPopuli c57e7b9
Remove text, add getDescription
NullVoxPopuli c308035
Refactor WithElement
NullVoxPopuli fe85fb4
Update WithElement JSDoc
NullVoxPopuli e5620c3
Update documentation
bendemboski b722d90
Test getDescription
bendemboski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
| import { describe, afterEach, test, expect } from '@jest/globals'; | ||
| import { PageObject, selector, assertExists, getDescription } from '../index'; | ||
| import { resetRoot } from '../-private/root'; | ||
|
|
||
| describe('utils', () => { | ||
| afterEach(() => resetRoot()); | ||
|
|
||
| describe('assertExists', () => { | ||
| test('element exists', () => { | ||
| document.body.innerHTML = '<div>boop</div>'; | ||
|
|
||
| class Page extends PageObject {} | ||
| let page = new Page('div'); | ||
|
|
||
| try { | ||
| assertExists('test', page); | ||
| } catch { | ||
| expect('This should not error').toEqual(false); | ||
| } | ||
|
|
||
| expect(true).toEqual(true); | ||
| }); | ||
|
|
||
| test('element missing', () => { | ||
| document.body.innerHTML = ''; | ||
|
|
||
| class Page extends PageObject {} | ||
| let page = new Page('div'); | ||
|
|
||
| expect(() => { | ||
| assertExists('test', page); | ||
| }).toThrow(/Tried selector `div`/); | ||
| }); | ||
|
|
||
| test('selector shown is deep', () => { | ||
| document.body.innerHTML = ''; | ||
|
|
||
| class Page extends PageObject { | ||
| nested = selector('button'); | ||
| } | ||
| let page = new Page('div'); | ||
|
|
||
| expect(() => { | ||
| assertExists('test', page.nested); | ||
| }).toThrow(/Tried selector `div button`/); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getDescription', () => { | ||
| test('it works', () => { | ||
| class Page extends PageObject { | ||
| thing = selector( | ||
| '.thing', | ||
| class extends PageObject { | ||
| subthing = selector('.subthing'); | ||
| } | ||
| ); | ||
| } | ||
| let page = new Page(); | ||
|
|
||
| expect(getDescription(page.thing)).toEqual('.thing'); | ||
| expect(getDescription(page.thing.subthing)).toEqual('.thing .subthing'); | ||
| expect(getDescription(page.thing[1].subthing[0])).toEqual( | ||
| '.thing[1] .subthing[0]' | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| export { setRoot } from './-private/root'; | ||
| export type { PageObjectConstructor } from './-private/types'; | ||
| export type { PageObjectConstructor, WithElement } from './-private/types'; | ||
|
|
||
| export { default as PageObject } from './page-object'; | ||
|
|
||
| export { default as selector } from './selector'; | ||
| export { default as globalSelector } from './global-selector'; | ||
|
|
||
| export { assertExists, getDescription } from './utils'; |
This file contains hidden or 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,40 @@ | ||
| import { DOM_QUERY, WithElement } from './-private/types'; | ||
|
|
||
| import type { default as PageObject } from './page-object'; | ||
|
|
||
| /** | ||
| * Useful for providing clarity to consumers of page-objects | ||
| * to provide additional context so "can't access property on undefined" | ||
| * errors do not public up to the consumer. | ||
| * | ||
| * In typescript, this is also useful for type-narrowing so that | ||
| * you can pass on the element to other utilities. | ||
| * | ||
| * @example | ||
| * | ||
| * let page = new Page(); | ||
| * | ||
| * assertExists('is the element on the page?', page); | ||
| * | ||
| * await click(page.element); | ||
| * | ||
| * @param {string} msg a descriptor for what it could mean when the element doesn't exist | ||
| * @param {PageObject} pageObject the page object | ||
| */ | ||
| export function assertExists( | ||
| msg: string, | ||
| pageObject: PageObject | ||
| ): asserts pageObject is WithElement<PageObject> { | ||
| if (!pageObject.element) { | ||
| throw new Error( | ||
| `${msg} >> Tried selector \`${getDescription(pageObject)}\`` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Utility to get the fully resolved selector path of a {@link PageObject} | ||
| */ | ||
| export function getDescription(pageObject: PageObject): string { | ||
| return pageObject[DOM_QUERY].selectorArray.toString(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I just express my sincere and emphatic support of
getDescriptionI've run into this many times and every time I come back disappointed that there is not a clean way to get the current selector string. The two main cases I've discovered so far are…In debugging.
Element not foundis not very easy to discover where it is whileselector '… … …' not foundcan really help someone staring at the Browser's console inspector to find where the typo is.When using 3rd party test helpers. For example, ember-power-calendar has a
calendarSelect()helper where the first param must be a string because it concatenates its own nested selectors on the one you passed. But as is now we can't convert a fractal-p-o selector back into the full selector from it's.elementproperty.Having this one escape hatch would make composing Page Objects not only easier to debug but also encourage the use of PageObjects like Martin Fowler originally envisioned by allowing developers to design a fractal-p-o with better methods especially methods that hook into 3rd party test helpers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related: cibernox/ember-power-calendar#291
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback @sukima! Relatedly, if you haven't read emberjs/rfcs#726 yet, I'd love to hear any thoughts/feedback you have on it. I'm hoping to make a push to get it moving again soon...