I've used the idea described here to run the test multiple times with different input values. But I've met a problem that Cypress manages loop pretty strange. To test the problem I've created a minimized application:
$(document).ready(() => {
$('#submit').on('click', () => {
$('#result').val($('#result').val() + $('#select').val() + '\n');
});
});
select, button, textarea{
font-size: 1.2em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select">
<option value="" disabled="" selected="">Choose</option>
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
<option value="fourth">Fourth</option>
</select>
<button id="submit">Add</button>
<br>
<textarea id="result" rows="20" cols="50"></textarea>
The test I run using Cypress:
describe('Test', () => {
it('should add entries to textarea', () => {
cy.visit('http://localhost/cypress-fail/index.html');
cy.get('#select', { timeout: 10000 }).should('be.visible');
let selected = [];
for (let i = 0; i < 4; i++) {
selected.push(i + 1);
let last_selected = selected[selected.length - 1];
cy.get('#select').children('option').then($options => { console.log(($options[last_selected])); cy.wrap($options[last_selected]).invoke('attr','value').then(value => cy.get('#select').select(value))});
cy.get('#submit').click().then(()=> console.log("submitted"));
cy.wrap(selected).should('have.length', i + 1);
//expect(selected).to.have.length(i+1);
cy.get('#result').invoke('val').then(text => {
let list = text.split('\n').filter(a => a);
cy.wrap(list).should('have.length', i + 1);
})
}
})
})
As the result of the test I get assert error:
assert: expected [ 1, 2, 3, 4, 5 ] to have a length of 1 but got 5
However if I use the "expect" line and try Chai-style this test passes, but it checks the array each loop first and then loops again to add selected entries into textarea.
Can it be that Cypress has its own loop execution logic? Is there a way to make it work more predictably?
And in total I've noticed that since version 10.0.0 Cypress won't wait where it did before, like waiting for page to load where now I have to add timeout configs.