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.

contain.text and include.text should do substring matches but instead do full-string matches

See original GitHub issue

Current behavior:

    // cy.get('div#preview').should('have.text', 'Hello');     // this is a full match, but we want a partial match, to ignore newlines etc.
    // cy.get('div#preview').should('contain.text', 'Hello');  // bug: this does the same as have.text
    // cy.get('div#preview').should('include.text', 'Hello');  // bug: this also does the same as have.text
    cy.get('div#preview').then((el)=> {
      assert.include(el.text(), 'Hello');  // this works but it isn't pretty
    });

the log for all three of the above says:

ASSERT: expected <div#preview> to have text Hello, but the text was Hello\n

which makes me think that there might be some aliasing going on since I’d think the error messages for “include” and “contain” would say “include” and “contain”, not "have

Desired behavior:

  • have.text should test whether the element’s full text is equal to the given string
  • contain.text and include.text should test whether the element’s text contains the given string but is not necessarily equal to it

This follows the Chai docs here: http://www.chaijs.com/api/bdd/#method_include “When the target is a string, .include asserts that the given string val is a substring of the target.”

Steps to reproduce:

Versions

  • Cypress 3.0.1
  • MacOS High Sierra
  • Google Chrome / Chromium as installed by npm

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

11reactions
bahmutovcommented, Jun 6, 2019

Ok, here is example - either explicit or using regular expression

// validates text using OR
cy.get('.assertions-p').should(($el) => {
  const text = $el.text()

    const hasThisText = text.includes('unknown')
  const hasThatText = text.includes('even more text')

    expect(hasThisText || hasThatText,
    'element has either this or that string').to.be.true
})
// same check is better expressed using a regular expression and
// https://on.cypress.io/contains
cy.contains('.assertions-p', /unknown|even more text/)
10reactions
brian-manncommented, Jun 17, 2018

The reason this isn’t working is because the subject / target is a DOM element, not a string.

I’m not sure this is actually a bug, since we vendor in chai (it does what it does), but we did write our own chai assertions for DOM elements here: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cypress/chai_jquery.coffee

I would start diving into what contain.text and include.text actually trigger - whether its chai methods or the chai jquery stuff we wrote to understand it better.

All of this works though you just have to drill into and change the subject to be a string:

cy.wrap('foobar').should('contain', 'foo') // passes
cy.wrap('foobar').should('include', 'foo') // passes

cy.get('button').invoke('text').should('contain', 'foo') // passes
cy.get('button').invoke('text').should('include', 'foo') // passes

If you’d like to do the legwork to see what assertion is being applied to a DOM element that would be great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress: How to check if the element has a text but the text is ...
Another way, test for any text of non-zero length cy.visit('http://example.com') cy.get('h1').invoke('text').should('have.length.gt', ...
Read more >
if specifc line contains substring then output other substrings
I used the following text as sample (modified the sample provided by the OP): <TEST1> <text:p text:style-name="P4">Hello<text:span ...
Read more >
re — Regular expression operations — Python 3.11.1 ...
Source code: Lib/re/ This module provides regular expression matching operations ... Regular expressions can contain both special and ordinary characters.
Read more >
Element selectors | Playwright 中文文档
Text body can be escaped with single or double quotes for full-string case-sensitive match instead. This means text="Login" will match ...
Read more >
GREL functions - OpenRefine
contains ("ee") returns false. You can search for a regular expression by wrapping it in forward slashes rather than quotes: "rose is a...
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