How to assert a specific text in cypress - cypress

Please see the below code.
I am trying to assert value 2 but my code is not working.

You do something like this:
cy.get('#_evidon_message').should('contain.text', '2')

Based on the information you provided, it isn't clear if you want to check the entire text within the div or just for the number before partners.
If you only want to validate there is a 2 within the entire string, then:
cy.get('#_evidon_message')
.invoke('text')
.should('include.text', '2')
You can use also use regex on the string as well.
cy.get('#_evidon_message')
.invoke('text')
.then(text => {
const regexMatcher = /(\d+) months with (?<numPartners>(\d+)) partners/i
const numPartners = text.match(regexMatcher)?.group?.numPartners
expect(numPartners).to.eq(2)
})

The bold tags <b> are interfering with your text evaluation. It's actually HTML inside
cy.get('#_evidon_message').then($el => {
const html = $el.html()
console.log(html) // yields same as screenshot
})
To access the inner <b> use additional commands, for example
cy.get('#_evidon_message')
.find('b')
.eq(2)
.should('contain', '2')

Related

How to check either the headline or the content should contain "FIFA" keyword?

How do I write this test in Cypress?
enter image description here
I have to confirm that either the headline or paragraph should contain the same keyword.
It's better to have JQuery elements in hand before the assertion so that you can use JQuery methods on them. cy.get() acts just like $(...) in JQuery, it will be enough to have the elements. (more here)
Once u have the elements, i.e. $el1 and $el2 below, then you can get their text via .text() method (more here) and then you can write your assertion.
Instead of a separate assertion, below I used a single one and checked if either of them includes the desired text by using || operator.
cy.get('first-el').then($el1 => {
cy.get('second-el').then($el2 => {
const inEl1 = $el1.text().includes('FIFA');
const inEl2 = $el2.text().includes('FIFA');
expect(inEl1 || inEl2).to.be.true;
});
});

What is the best way to assert that a component contains a string "case insensitive" using Cypress?

I want to assert that a component contains a string without caring about the string case.
For example, I want
cy.get('#label').should('contain.text', 'integrator');
to pass even if the label contains "Integrator."
What is the best way I can make this assertion?
You can also use cy.contains() with a regular expression
cy.contains('#label', /integrator/i) // should is implied in this command
or as an option
cy.contains('#label', 'integrator', {matchCase:false})
With should() you get retry of the expect()
cy.get('#label')
.should($el => {
expect($el.text().toLowerCase()).to.eq('integrator') // exact
// or
expect($el.text().toLowerCase()).to.contain('integrator') // partial
})
What you need is Regular expressions.
You can use the match assertion:
cy.get('#label')
.invoke('text')
.should('match', /integrator/i) //i = case sensitive
You can do like this as well:
cy.get('#label').then(($ele) => {
expect($ele.text().toLowerCase()).to.contain('integrator')
})

How can we compare multiple text in cypress (not wanted and wanted or)

If I have an element having text 'xyzzy', but I wanted to check this against all these: 'par' or 'xyzzy' or 'arc', how we can do it?
I'm not able to get it with this:
.should('have.text','Ab demo 1').and('have.text','ab demo 2')
Can we replace 'and' with 'or'?
Can use satisfy assertion,
cy.get('(//div[#class="xyz"]')
.invoke('text')
.should('satisfy', (text) => text === 'par' || text === 'xyz' || text === 'arc')
You can use the cypress assertion to.be.oneOf and do that. Something like:
cy.get('selector').then(($ele) => {
expect($ele.text().trim()).to.be.oneOf(['xyzzy', 'par', 'arc'])
})
Can be done in a .should() command with a callback function
const texts = ['par', 'xyzzy', 'arc']
cy.get('(//div[#class="xyz"]').eq(2)
.should($el => {
const text = $el.text()
expect(texts.includes(text)).to.eq(true)
})
The oneOf assertion can go inside .should()
cy.get('(//div[#class="xyz"]')
.invoke('text')
.should('be.oneOf', ['par', 'xyzzy', 'arc'])
Just add the word any and provide the list to match
cy.get('(//div[#class="xyz"]')
.should('have.any.text', ('Ab demo 1', 'Ab demo 2'))

Want to validate the part of a string shouldn't be null

I have this following element which contain a string for Practitioner, its value is 1- zzz. How to validate after - it shouldn't be null. Even if there is a string or empty. It shouldn't print null. Also want to select the value under Practitioner (currently hard coded the position of the element as 2)
<div class="styles__container___BfTYi">
<div class="styles__subHeader___18Yg1">Practitioner</div>
<div class="styles__data___1senX">1- zzz</div>
</div>
I have the following code to retrieve the text,
cy.get(pageSelector.practitionerValidator).eq(2).then(function($getText) {
let practitionerName = $getText.text();
var validateLastName = practitionerName.split(' ');
cy.log(validateLastName[1]);
expect(validateLastName[1]).to.not.equal('null');
})
You can directly check that the entire string is not null like this:
cy.get('.styles__data___1senX').then(($ele) => {
expect($ele.text()).to.not.be.null
})
Or if you want to check that your inner text is not empty you can do:
cy.get('.styles__data___1senX').then(($ele) => {
expect($ele.text()).to.not.be.empty
})
You can find the selector from the text Practitioner like this:
cy.contains('Practitioner')
.parent()
.within(() => {
cy.get('div[class*="styles__data__"]').then(($ele) => {
expect($ele.text()).to.not.be.null
})
})
Appreciate the level of detail you gave.
I will be going off this assumption.
the Practitioner string will be random can spaces or no spaces after the - (ie 34- sdfwe, 3- , 1- )
I would use a regex to check the format of the string to check the string starts with a digit followed by a dash and a space with either a string, spaces, or nothing. /\d+\-\s(\w+|\s+)?/
Your code would look a bit like this.
cy.get(pageSelector.practitionerValidator)
.eq(2)
.invoke('text') // get text
.should('match', /\d+\-\s(\w+|\s+)?/) // use regex assertion

Cypress: store value in a variable

I want to store a td value in a variable. Why this code doesn't work?
let storedValue;
cy.get('tbody>tr:nth-child(1)>td:nth-child(3)').invoke('text').then(text =>
{
storedValue = text;
})
cy.contains(storedValue).should('exist');
It returns "cy.contains() can only accept a string, number or regular expression"
A better approach would be to use aliases.
cy.get('tbody>tr:nth-child(1)>td:nth-child(3)').invoke('text').as('storedValue')
cy.get('#storedValue').then((storedValue) => {
//Access storedValue here
cy.log(storedValue) //prints value
})
Or if you just want to check whether the element exists or not, you can directly do:
cy.get('tbody>tr:nth-child(1)>td:nth-child(3)').should('exist')
And for the question of why is your code not working, its because javascript works asynchronously, so instead of the code that gets the text first, the contains statement is executed first and the reason it fails is that storedValue doesn't have anything in it, in short, it is undefined. To fix then you have to add then() so that the code runs in a sequence we intend it to run in.
let storedValue
cy.get('tbody>tr:nth-child(1)>td:nth-child(3)')
.invoke('text')
.then((text) => {
storedValue = text
})
.then(() => {
cy.contains(storedValue).should('exist')
})

Resources