-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Provide a "Cypress" way to access textContent (and/or innerText) - .text() command #630
Comments
Agreed. This is now on our radar. I think the biggest issue here is that |
I guess if you'd implement your own normalization, you can't call the feature innerText in any way or it would be confusing. If the feature would be named inner Text, you'd have to use the (current) browser implementation I guess. |
any progress on this ? |
@brknlpr you can just use jquery methods to access text content of elements - its our top FAQ question.
|
thanks a lot! |
in the meantime, can you please add this to https://docs.cypress.io/guides/core-concepts/interacting-with-elements.html , and/or link to the FAQ entry from there? |
Thanx @brknlpr saved my life! 👍 |
Not sure how this would work. Can some one provide an example. I don't like using 'contain' and would love to use the inner text if possible. How would this work?? cy.get('element').invoke('text') |
Ok, I figured it out I found the link https://on.cypress.io/using-cypress-faq#How-do-I-get-an-element%E2%80%99s-text-contents |
Ok, it was bothering me to use contains because I wanted more of a valid check that would not possibly give me a false positive. For example if my text is 66.67% and use contains 6.67% my test will pass. I can see where that might be a problem where there is a row with 6.67% and I validate the wrong row and my test still passes, i realize the probability of that is slight, but still possible. I then started using 'have.text', and my test were failing because my cell elements has empty spaces in it. I fixed it by re-moving spaces in the table cell, but the cell was formatted that way by a developer and they could easily go in put the formatting back and break the test. I don't want my test to be that brittle. I then used this to prevent that problem, cy.get('#id-1').find('.budget-item-percent').invoke('text').then((text => {
expect(text.trim()).to.eq('66.67%')
})); Is there any easier way to do this. Notice I am trimming the spaces so the if someone re-formats the it does not break my code (or so I hope) |
I'm using the cy.get('element).invoke('text') to get the text of an h2 element in a test. The text of the element is Event 4 Spans Several Days instead the Cypress invoke call yields Event 4 spans several days making the text lower case and leading to some false positive assertions. Does anyone know why this would be? |
Sounds like a (unrelated) bug. Maybe Cypress is trying to lowercase HTML tags (span in this case) where it shoudn't? I'd open a separate issue for this. |
You can also create your own command if you think Cypress.Commands.add("text", { prevSubject: true}, (subject, options) => {
return subject.text();
}); and then use it like this: cy.get('._19odbsb1')**.text()**.then(value => results.yearlyTotal = value); |
@sahithya most likely you have |
There is a plugin that offers a More about the command can be found here https://github.com/Lakitna/cypress-commands/blob/develop/docs/text.md |
When I use the plugin's I just want to get a number string from a page and assert that it's greater than zero. // I would like to use this syntax, but it fails because it compares a string with a number
❌ cy.get(someSelectorHere).text().should("be.greaterThan", 0)
// This also fails because it returns a Chainable object, which to JavaScript is not a number (NaN)
❌ expect(cy.get(someSelectorHere).invoke("text")).to.be.greaterThan(0)
// This hack works for this case, but it won't work for number ranges
✅ cy.get(someSelectorHere).text().should("not.be", 0) How can I do it properly? |
You can always do something like this cy.get(someSelector)
.text()
.then((str) => Number(str))
.should("be.above", 2); Do you think that If I add that an option will be required to turn this behaviour on/off. When the default is on then a major version release will be necessary. |
oh, hi there @Lakitna ! I wasn't expecting the lib author to appear 😄🙇♂️ I like your code example, it just adds a Hmm, I don't think I would prefer a |
😄 Cypress commands by definition always return a Chainer, so making it return a string/array is impossible. In all other situations the jQuery cy.get(singleElementSelector)
.then((element) => {
expect(element.text()).to.equal('foo');
}); |
Impossible? Fine... 😢 |
It's impossible because of how Cypress works. The code you write isn't
executed immediately. Instead it sets up a script to be executed later. See
my lecture slides here for more info :
https://codelikethis.com/lessons/javascript/cypress#anchor/nothing_happens_immediately
Because of this delayed execution, the only way to get fine grained access
to the actual values in the page -- to then do comparisons or conversions
-- is through a callback (like with ".then").
Hope this helps!
- A
On Mon, Jun 24, 2019 at 11:11 AM Diogo Nunes ***@***.***> wrote:
Impossible? Fine...
Then how about a .number() that *always* does the casting that you
mentioned?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#630?email_source=notifications&email_token=AAACCTF3JTVXL7JXYVKZST3P4DP3NA5CNFSM4D2NG2EKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYNIBNA#issuecomment-505053364>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAACCTAAT6745BHUOTQSAWDP4DP3NANCNFSM4D2NG2EA>
.
|
I am trying to get the text of a group of elements and populate them into a map so that I can access the values in the following page to do some validation.However the map object gives undefined once out of the invoke().then() functionality. |
@rhamasv Note that If you have, please create an issue in the |
@Lakitna Thanks for your response.I forgot to mention .text() function already works for some elements in our existing framework (still trying to found out if cypress-commands is already plugged in our framework) and throws the error I mentioned above for elements with a (hyperlink) tags. |
Is this retried until some timeout, or is the retry loop limited to only waiting on |
@brknlpr I guess there's no retry mechanism there as the jQuery
|
I'm still kinda new to the syntax, but how do I return an element from an array of elements with a text match? |
@tchu10 Kinda getting off-topic here (direct these kinds of questions to the Gitter chat) but for that you probably need the |
I know this is not the best place to ask questions, but how to verify if text (on button or label) has changed or was updated? We don't know in test what the target value should be - and thus using cy.contains() makes no sense. |
@markrason These are the kinds of questions you can best ask in Gitter https://gitter.im/cypress-io/cypress |
@markrason also you can use our documentation search to find the answer in https://docs.cypress.io/faq/questions/using-cypress-faq.html#How-do-I-get-an-element%E2%80%99s-text-contents |
Here is my solution
|
I am also looking for this workaround |
@vjvibhanshu That will never work. The |
it is giving the output like this: |
@verheyenkoen thanks for the insight |
@vjvibhanshu This isn't really the place for support (look here) but you could probably do something like this: cy.request('{API url}').then(response => {
cy.get('{selector}')
.invoke('text') // this invokes the text method, so the equivalent of $(el).text()
.should('eq', response.body.path.to.property) // JSON response body is automatically deserialized to an object
}) |
@verheyenkoen Thanks for the code snip, However it is little messy I want to keep UI and API part separate |
This is my solution.
|
I'm a little confused about this one, if |
This is ridiculous that I can't store text/value outside of chain. Also per Cypress documentation, here is one way that works:
BUT if using alias outside of beforeEach it return "undefined" again:
Why make it so complicated, whats the reason behind all this ? |
It has bitten me as well. Such commonly used operation feels like it should be bulletproof and very easy to use. |
In Cypress, you can access the textContent or innerText of an element using the .invoke('text') or .invoke('prop', 'innerText') commands. Know more : https://ravirathore.com/ |
Desired behavior:
Add a
.text()
method similar to jQuery's text method to access textContent and/or innerText.I guess for convenience, by default
cy.text()
should just return whatever$(...).text()
returns.It may also be good to provide a way to get the innerText of an HTMLElement as it returns a rendered text representation (removing sub-tags and whitespace that may be part of the node, but not visible once rendered). Perhaps a flag in the
.text()
method or via.its('innerText')
or something.The text was updated successfully, but these errors were encountered: