1

The values on my array getting cleared after exit .then scope

in below code tableValues1.length gives me correct length until it is inside each loop the moment it exit .then scope -array length is zero.

Please can any one help me on this -Thanks

describe('Test setting basic Alert-Data update option', () => {
it('Test SetAlert-Data update', () => {     
  var tableValues1=[];

 cy.contains('browse',{timeout: 60000}).should('be.visible',{ timeout:   60000 });
cy.contains('browse',{timeout: 60000}).click().then(()=>
{
   cy.LoadProject();
})
//create analysis using smart search function and save to story
cy.mthode1(downAxis,acrossAxis,filterAxis);

cy.get('.gradContainer').find('table').as('Table');
cy.get('.gradContainer').find('table').each(($table, index, $list) => {

    var headerLength=$table.find('thead').length;
    var headers=$table.find('thead');

    if ($table.find('thead').length>0)
    {

        cy.log('inside if');
        cy.log($table.find('th').length);
        cy.wrap($table).find('th').each(($header)=>{
          cy.wrap($header).invoke('text').then(($elementvalue)=>{
          //Add values to array
          **tableValues1.push($elementvalue);**
          **cy.log('length INSIDE .then '+tableValues1.length);//---GIVES ME correct count
                   })
        cy.log('length AFTER .then '+tableValues1.length);//--GIVES me zero**


else
{
// add some other set of values

}

})
})
user8710571
  • 405
  • 2
  • 12
  • 25
  • This is a duplicate of https://stackoverflow.com/q/3884281/10068463 – Nate Feb 21 '19 at 05:12
  • @Nate no it's not. This is about Cypress – kuceb Feb 21 '19 at 13:00
  • @bkucera Yes it is. You clearly don’t understand the fundamental concept of `then`. The library you use is irrelevant. Read the post I linked. They’re using async code. “after” the `then` the array hasn’t been filled yet because they haven’t waited for the async code to finish. – Nate Feb 21 '19 at 13:44
  • 1
    @Nate no, really. A cypress .then is not a promise .then. it's only similar – kuceb Feb 21 '19 at 13:52
  • 1
    https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Closures they might not be actual promises, but they’re designed to work exactly like them. “If you’re familiar with native Promises the Cypress .then() works the same way.” – Nate Feb 21 '19 at 13:58

2 Answers2

2

Try this instead:

cy.wrap(null).then(()=>{cy.log(array.length)})
kuceb
  • 16,573
  • 7
  • 42
  • 56
  • See my comment above. This is a duplicate of https://stackoverflow.com/q/3884281/10068463 – Nate Feb 21 '19 at 13:45
  • The solutions are entirely different, in Cypress commands are enqueued, they're not executed synchronously – kuceb Feb 21 '19 at 13:58
  • Hence why you can’t write a command after a `then` and expect the `then` to have fired yet. – Nate Feb 21 '19 at 13:59
  • You do see that OP is logging the array OUTSIDE of the `then`, right? It may be after in the code, but it’s not after in terms of execution order. – Nate Feb 21 '19 at 14:00
  • @bkucera Working fine -Thank you so much – user8710571 Feb 21 '19 at 20:07
  • using `cy.then(() => {})` directly is fine, too (unless cypress is deprecating this undocumented behavior, /cc @bkucara) – dwelle Feb 22 '19 at 19:48
1

cy.log is an enqueued command; it's not synchronous like a console.log

Try using Cypress.log instead:

Cypress.log({ name: 'debug', message: length })
kuceb
  • 16,573
  • 7
  • 42
  • 56
  • thanks - my issue is not with log - i even tried to write to a file but as soon as it is outside then -values are null – user8710571 Feb 21 '19 at 19:53