I’m doing a test and I put a value in a texfield. If I get some data I want it to be found, otherwise I want “no data” to be found. This code doesn’t work… Why? And how can I do it?
JavaScript
x
14
14
1
it('Test on filter', function () {
2
const valueInserted = 'VALUE';
3
cy.get('#autorouter-patname').type(valueInserted);
4
cy.get('button[type="submit"]'.click();
5
cy.get('tbody>tr>td')
6
.then(($el) => {
7
if (cy.get($el).contains('No data available')) {
8
return cy.contains('No data available')
9
} else {
10
return cy.get($el).eq(2).contains(valueInserted);
11
}
12
})
13
})
14
Advertisement
Answer
You are trying to use the contains
command from cypress to get a boolean, but it acts like an assertion itself. It tries to search something that contains the provided text and if gets no results, the test fails.
I am doing conditional testing like this:
JavaScript
1
6
1
cy.get('body').then(($body) => {
2
if ($body.find('._md-nav-button:contains("' + name + '")').length > 0) {
3
cy.contains('._md-nav-button', name).click();
4
}
5
});
6