question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select / highlight text

See original GitHub issue

Current behavior:

I’m having a hard time creating a text selection. Things I’ve tried:

  • document.createRange() and window.getSelection()
  • Using cy.trigger to simulate a cursor drag over the paragraph.
  • Using 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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:16
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

20reactions
erquhartcommented, Mar 11, 2020

Here are a couple of commands that are working for my team, using Cypress to test a Slate based editor: https://gist.github.com/erquhart/37bf2d938ab594058e0572ed17d3837a

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 setCursorBefore/After with the same text matching interface, plus some other lower level commands.

20reactions
kucebcommented, Dec 13, 2018

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')
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to select or highlight text in Windows | Digital Citizen
Highlighting text in Windows by clicking or tapping works pretty much the same way as selecting multiple items. Click or tap once, and...
Read more >
Highlight your text - Microsoft Support
Select the text that you want to highlight. · On the Home tab, select the arrow next to Text Highlight Color Text highlighter...
Read more >
How to select and highlight text in rich text editors | TinyMCE
In this article we have a deep dive into complex cases of text selection and highlighting in text editors and explain possible solutions....
Read more >
How to get the Highlighted/Selected text in JavaScript?
There may be a need to find out the text selected/highlighted by the user. It can be done very easily using the window...
Read more >
How to Apply, Remove, and Find Highlighted Text in Word
Click the arrow next to the Text Highlight Color button. Choose the color you want to use from the drop-down menu.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found