-
-
Notifications
You must be signed in to change notification settings - Fork 752
feat: fillField supports rich text editors #5527
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
Open
DavertMik
wants to merge
5
commits into
4.x
Choose a base branch
from
feat/fill-field-editor
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe5ce3e
feat: fillField supports rich text editors
DavertMik e14ef4e
fix: use browser.keys for WebDriver typeText/selectAll
DavertMik 9f5f2f4
fix: WebDriver v9 frame switching + newline handling
DavertMik 12cbb5d
fix: drop redundant iframe body click in rich editor fill
DavertMik c55680a
fix: bound rich editor hidden-ancestor walk to avoid cross-form binds
DavertMik 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,115 @@ | ||
| import WebElement from '../../element/WebElement.js' | ||
|
|
||
| const MARKER = 'data-codeceptjs-rte-target' | ||
|
|
||
| const EDITOR = { | ||
| STANDARD: 'standard', | ||
| IFRAME: 'iframe', | ||
| CONTENTEDITABLE: 'contenteditable', | ||
| HIDDEN_TEXTAREA: 'hidden-textarea', | ||
| } | ||
|
|
||
| function detectAndMark(el, opts) { | ||
| const marker = opts.marker | ||
| const kinds = opts.kinds | ||
| const CE = '[contenteditable="true"], [contenteditable=""]' | ||
| const MAX_HIDDEN_ASCENT = 3 | ||
|
|
||
| function mark(kind, target) { | ||
| document.querySelectorAll('[' + marker + ']').forEach(n => n.removeAttribute(marker)) | ||
| if (target && target.nodeType === 1) target.setAttribute(marker, '1') | ||
| return kind | ||
| } | ||
|
|
||
| if (!el || el.nodeType !== 1) return mark(kinds.STANDARD, el) | ||
|
|
||
| const tag = el.tagName | ||
| if (tag === 'IFRAME') return mark(kinds.IFRAME, el) | ||
| if (el.isContentEditable) return mark(kinds.CONTENTEDITABLE, el) | ||
|
|
||
| const canSearchDescendants = tag !== 'INPUT' && tag !== 'TEXTAREA' | ||
| if (canSearchDescendants) { | ||
| const iframe = el.querySelector('iframe') | ||
| if (iframe) return mark(kinds.IFRAME, iframe) | ||
| const ce = el.querySelector(CE) | ||
| if (ce) return mark(kinds.CONTENTEDITABLE, ce) | ||
| const textarea = el.querySelector('textarea') | ||
| if (textarea) return mark(kinds.HIDDEN_TEXTAREA, textarea) | ||
| } | ||
|
|
||
| const style = window.getComputedStyle(el) | ||
| const isHidden = | ||
| el.offsetParent === null || | ||
| (el.offsetWidth === 0 && el.offsetHeight === 0) || | ||
| style.display === 'none' || | ||
| style.visibility === 'hidden' | ||
| if (!isHidden) return mark(kinds.STANDARD, el) | ||
|
|
||
| const isFormHidden = tag === 'INPUT' && el.type === 'hidden' | ||
| if (isFormHidden) return mark(kinds.STANDARD, el) | ||
|
|
||
| let scope = el.parentElement | ||
| for (let depth = 0; scope && depth < MAX_HIDDEN_ASCENT; depth++, scope = scope.parentElement) { | ||
| const iframeNear = scope.querySelector('iframe') | ||
| if (iframeNear) return mark(kinds.IFRAME, iframeNear) | ||
| const ceNear = scope.querySelector(CE) | ||
| if (ceNear) return mark(kinds.CONTENTEDITABLE, ceNear) | ||
| const textareaNear = [...scope.querySelectorAll('textarea')].find(t => t !== el) | ||
| if (textareaNear) return mark(kinds.HIDDEN_TEXTAREA, textareaNear) | ||
| } | ||
|
|
||
| return mark(kinds.STANDARD, el) | ||
| } | ||
|
|
||
| function selectAllInEditable(el) { | ||
| const doc = el.ownerDocument | ||
| const win = doc.defaultView | ||
| el.focus() | ||
| const range = doc.createRange() | ||
| range.selectNodeContents(el) | ||
| const sel = win.getSelection() | ||
| sel.removeAllRanges() | ||
| sel.addRange(range) | ||
| } | ||
|
|
||
| function unmarkAll(marker) { | ||
| document.querySelectorAll('[' + marker + ']').forEach(n => n.removeAttribute(marker)) | ||
| } | ||
|
|
||
| async function findMarked(helper) { | ||
| const root = helper.page || helper.browser | ||
| const raw = await root.$('[' + MARKER + ']') | ||
| return new WebElement(raw, helper) | ||
| } | ||
|
|
||
| async function clearMarker(helper) { | ||
| if (helper.page) return helper.page.evaluate(unmarkAll, MARKER) | ||
| return helper.executeScript(unmarkAll, MARKER) | ||
| } | ||
|
|
||
| export async function fillRichEditor(helper, el, value) { | ||
| const source = el instanceof WebElement ? el : new WebElement(el, helper) | ||
| const kind = await source.evaluate(detectAndMark, { marker: MARKER, kinds: EDITOR }) | ||
| if (kind === EDITOR.STANDARD) return false | ||
|
|
||
| const target = await findMarked(helper) | ||
| const delay = helper.options.pressKeyDelay | ||
|
|
||
| if (kind === EDITOR.IFRAME) { | ||
| await target.inIframe(async body => { | ||
| await body.evaluate(selectAllInEditable) | ||
| await body.typeText(value, { delay }) | ||
| }) | ||
| } else if (kind === EDITOR.HIDDEN_TEXTAREA) { | ||
| await target.focus() | ||
| await target.selectAllAndDelete() | ||
| await target.typeText(value, { delay }) | ||
| } else if (kind === EDITOR.CONTENTEDITABLE) { | ||
| await target.click() | ||
| await target.evaluate(selectAllInEditable) | ||
| await target.typeText(value, { delay }) | ||
| } | ||
|
|
||
| await clearMarker(helper) | ||
| return true | ||
| } | ||
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,25 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Rich Text Editors</title> | ||
| </head> | ||
| <body> | ||
| <h1>Rich Text Editors</h1> | ||
| <p>Test pages for fillField against rich text editors.</p> | ||
| <ul> | ||
| <li><a href="/form/richtext/prosemirror">ProseMirror</a> — contenteditable</li> | ||
| <li><a href="/form/richtext/quill">Quill</a> — contenteditable (.ql-editor)</li> | ||
| <li><a href="/form/richtext/ckeditor5">CKEditor 5</a> — contenteditable (.ck-editor__editable)</li> | ||
| <li><a href="/form/richtext/ckeditor4">CKEditor 4</a> — iframe</li> | ||
| <li><a href="/form/richtext/tinymce-modern">TinyMCE (inline)</a> — contenteditable</li> | ||
| <li><a href="/form/richtext/tinymce-legacy">TinyMCE (iframe)</a> — iframe</li> | ||
| <li><a href="/form/richtext/monaco">Monaco</a> — hidden textarea</li> | ||
| <li><a href="/form/richtext/ace">ACE</a> — hidden textarea</li> | ||
| <li><a href="/form/richtext/codemirror5">CodeMirror 5</a> — hidden textarea</li> | ||
| <li><a href="/form/richtext/codemirror6">CodeMirror 6</a> — contenteditable (.cm-content)</li> | ||
| <li><a href="/form/richtext/trix">Trix</a> — contenteditable (trix-editor)</li> | ||
| <li><a href="/form/richtext/summernote">Summernote</a> — contenteditable (.note-editable)</li> | ||
| </ul> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
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.
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.
Looks too broad for hidden elements: it climbs up to
document.bodyand can accidentally bind an unrelated hidden field to any editor on the page