-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Select / highlight text #2839
Comments
What are you trying to test with highlighting text? It'll be easier to determine a fix if we understand what you're trying to do. |
My application keeps a record of highlighted text. I'm trying to test that that works correctly. |
We've gotten this question several times. We don't have a specific API implemented to handle highlighting text. We do have a generic I will update this issue to reflect request for this feature. This issue should now track:
|
This feature also requires work that's already in progress for our Native Events issue: #311 |
Related comment from @tnrich on wanting ability to highlight text with
@bkucera Would this situation be covered in the Native Events work currently? |
This would have to be a new command entirely, and isn't planned for the native events release. However we do plan to support text selection in the future In the meantime, you can try this: cy.get('p.mytext')
.trigger('mousedown')
.then(($el) => {
const el = $el[0]
const document = el.ownerDocument
const range = document.createRange()
range.selectNodeContents(el)
document.getSelection().removeAllRanges(range)
document.getSelection().addRange(range)
})
.trigger('mouseup')
cy.document().trigger('selectionchange') |
@bkucera why this line |
Here are a couple of commands that are working for my team, using Cypress to test a Slate based editor: Includes the ability to naively select based on text matching: cy.get('p').setSelection('foo') Or set both the start and end points of the selection: cy.get('p').setSelection('foo', 'baz') Also |
@vctormb it's what the browser fires if you were to select text manually, but it won't make a difference if your app code doesn't listen for it |
FYI, I couldn't get the gist above working with |
@erquhart Thanks you so much !!! The highlighting works !!! I feel maybe this can be made into an official way of highlighting text instead of just being labelled as a workaround !!! hehe |
Yeah I used this Gist successfully on a selection-heavy project test suite and encountered no real issue, I'd be fine if this was the code used for official support despite its potential limitations, it's still better than no support |
Glad it worked for you! It's super naive, Eg., duplicates of the targeted string can trip it up easily. We've avoided this by not using duplicate strings in our tests (they're generally short strings anyway). Probably not a viable candidate for a first class solution, but a good stopgap. For anyone running into issues w/ the gist, our implementation has evolved a bit and may work better. You'll just have to dig a bit in the source file to find the relevant code. Here's the source as of today: https://github.com/netlify/netlify-cms/blob/a4b7481a99f58b9abe85ab5712d27593cde20096/cypress/support/commands.js#L180 |
@erquhart Thanks a lot! You saved us hours of work. I really recommend you to create a separate package with all selection related commands so people can just install it via npm in the future and use it directly. I'd be the first one to star it! |
For future readers: if @erquhart solution is not working for you, try to trigger a 'selectstart' after the triggered 'mousedown' in the 'selection' command. That seems to fake better a real mouse interaction. |
@jennifer-shehane how can we move this issue forward? |
If the previous workarounds didn't work for some people (like me), you could try |
@erquhart What a great solution and many thanks for sharing. I have similar issues starting out testing my own rich text editor app and this saved me a lot of time. |
Any other suggestions for handling selecting text inside iframe ? |
@erquhart thanks for your solution!! Here's a simple fork to select anything within a Node. No need to change Code/**
* Returns an `Array` with all `#text` nodes in a Node.
* @param {Node} target root node
* @returns {Node[]} the requested nodes
*/
function getTextNodes(target) {
if (target.nodeType === 3) {
return [target];
}
let nodes = [];
const doc = target.ownerDocument;
const win = doc.defaultView;
const walk = doc.createTreeWalker(target, win.NodeFilter.SHOW_TEXT, null);
let node;
while (node = walk.nextNode()) {
if (node.nodeType === 3) {
nodes = [...nodes, node];
}
}
return nodes;
}
/**
* Select all the text of the first textNode of a given element.
* @param {Node} target target element
*/
export default function selectText(target) {
const textNodes = getTextNodes(target);
const [startNode, endNode] = [...textNodes.slice(0,1), ...textNodes.slice(-1)];
console.log(startNode,endNode)
const doc = target.ownerDocument;
const win = doc.defaultView;
const range = new win.Range();
range.setStart(startNode , 0);
range.setEnd(endNode, endNode.textContent.length);
win.getSelection().removeAllRanges();
win.getSelection().addRange(range);
} How to
it('selects all text in a Node', 90 => {
cy.get('SELECTOR').then((target) => {
selectText($target[0]);
// now do something with all that selected text
expect(target[0].ownerDocument.getSelection().toString().length).to.be.above(0);
})
}) Last but not least: have fun! |
This worked like a charm! Thanks @NZhlebinkov |
I want to select and highlight span all the elements present under the element. After all the element under the mentioned tag are selected, we get a word "Santa" that is highlighted.
None of the workarounds mentioned above seem to work. Anyone is aware how can this be done?
|
|
Current behavior:
I'm having a hard time creating a text selection. Things I've tried:
document.createRange()
andwindow.getSelection()
cy.trigger
to simulate a cursor drag over the paragraph.dblclick
on the paragraph.Desired behavior:
Test highlighting text.
Steps to reproduce:
I can provide sample code for the approaches I tried, but I'll hold off for now in case there's a completely different known way of doing this.
Versions
Cypress 3.1.1, Chrome 70.0.3538.102
The text was updated successfully, but these errors were encountered: