Skip to content
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

support partial matching text,html,value #3259

Merged
merged 14 commits into from
Jul 9, 2019
Merged

Conversation

kuceb
Copy link
Contributor

@kuceb kuceb commented Jan 30, 2019

fix #1969

adds the following partial matchers:

// <div id="foo">foo bar baz</div>
cy.get('#foo').should('contain.text', 'foo')
cy.get('#foo').should('not.contain.text', 'baz foo')

cy.get('#foo').should('contain.html', '<div')
cy.get('#foo').should('not.contain.html', 'fizzbuzz')

// <input value="foobarbaz">
cy.get('#foo').should('contain.value', 'foo')
cy.get('#foo').should('not.contain.value', 'baz foo')

expect(el).to.contain.value('foo') // works too

@kuceb kuceb requested a review from brian-mann January 30, 2019 21:57
@kuceb kuceb changed the title Issue 1969 partial matchers support partial matching text,html,value,id Jan 30, 2019
@kuceb kuceb changed the title support partial matching text,html,value,id support partial matching text,html,value Jun 6, 2019
@kuceb kuceb requested a review from a team June 6, 2019 04:03
@jennifer-shehane
Copy link
Member

jennifer-shehane commented Jun 6, 2019

Hmm, is this a bugfix? breaking change? feature? 🤔

Seems this could break some peoples expected implementation today.

Does this also close this issue? #3887

@kuceb
Copy link
Contributor Author

kuceb commented Jun 6, 2019

I have the issue tagged as 3.4.0 feature release.

@flotwig flotwig merged commit 52ca8ed into develop Jul 9, 2019
@reallyusefulengine
Copy link

reallyusefulengine commented Jul 24, 2019

Given an element like this:

<button type="button" class="⚛️ flex-grow-1 flex-lg-grow-0 btn btn-danger" id="CANCEL" data-survey-action="cancel" data-url="">
    Cancel form
  </button>

I have to do something ugly like this:

cy.get('#CANCEL').should('have.text', '\n Cancel form\n ').and('have.class', 'btn-danger');

or some convoluted way like this:

cy.get('#CANCEL').then((exp) => {
                const text = exp[0].textContent.trim();
                expect(text).to.equal('Cancel form');
            });

Would be great if there was an easy way to strip new lines and leading and trailing white spaces

@jennifer-shehane
Copy link
Member

@reallyusefulengine This shouldn't be necessary in 3.4.0 of Cypress. What version are you using?

@reallyusefulengine
Copy link

reallyusefulengine commented Jul 24, 2019

Im using 3.4.0
image

image

when I run:

cy.get('#CANCEL').should('have.text', 'Cancel form').and('have.class', 'btn-danger');

@kuceb
Copy link
Contributor Author

kuceb commented Jul 24, 2019

@reallyusefulengine use contain.text instead of have.text

@reallyusefulengine
Copy link

reallyusefulengine commented Jul 24, 2019

Ok thanks @bkucera for the comment. The problem i have with contain.text is that if a developer updates the text of a button from "Submit" to "Submit Now" the test will still pass as it contains Submit. Thats why I was using have.text.

To illustrate what I mean I created a custom function that basically does what I want:

Cypress.Commands.add(
    'hasText',
    {
        prevSubject: true,
    },
    function(subject, expectedText) {
        const text = subject[0].textContent.trim();
        expect(text).to.equal(expectedText);
    }
);

Not sure if doing that is good practice though

@jennifer-shehane
Copy link
Member

@reallyusefulengine Custom commands are encouraged! It's the best way to get the right fit for your project and looks like a good solution. 👍

rsuller added a commit to rsuller/cyress_poc that referenced this pull request Sep 20, 2019
Commented out some WIP for the change in domain for payment.
Added custom command for checking screen text inspired by cypress-io/cypress#3259
Updated README.md
@minbarnett
Copy link

am I the only who still sees this old issue reported here #1969 on cypress version 6.4.0

@jennifer-shehane
Copy link
Member

@minbarnett Please open a new issue with a reproducible example if you're seeing a bug in 6.4.0.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Feb 10, 2021
@flotwig flotwig deleted the issue-1969-partial-matchers branch January 24, 2022 18:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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