0

I have these codes. it uses each() twice. It always returns false even it should be true. i added two cy.log() line. It got these: isFound value after assignment is:true isFound value before return: false

verifyRuleInTable(ruleName) {
        const table = cy.get('ex-table[data-testid="deployedapis-table"]').shadow().find('div[role="rowgroup"]');
        const realTable = table.find('div[role="row"]')
        let isFound = false;
        realTable.each(($row, rowIndex) => {
            cy.wrap($row).find('div span span').each(($column,columnIndex) =>{
                if(columnIndex===1){
                    cy.wrap($column).invoke('text').then((result) => {
                        if (result.trim() === ruleName.trim()){
                            isFound = true;
                            cy.log('isFound value after assignment is:' + isFound)
                        }
                    })
                }
            })
        })
        
        cy.log('isFound value before return: ' + isFound)
        return isFound ? true : false;
    }

then I changed the code to:

   verifyPolicyRuleInTable(policyRuleName) {
        const table = cy.get('ex-table[data-testid="deployedapis-table"]').shadow().find('div[role="rowgroup"]');
        const realTable = table.find('div[role="row"]')
        let isFound = false;
        realTable.each(($row, rowIndex) => {
            cy.wrap($row).find('div span span').each(($column,columnIndex) =>{
                if(columnIndex===1){
                    cy.wrap($column).invoke('text').then((result) => {
                        if (result.trim() === policyRuleName.trim()){
                            isFound = true;
                        }
                    })
                }
            })
        }).then(() => {
            if (isFound) {
                cy.log('Value found!');
            } else {
                cy.log('Value not found!');
            }
        });

        }

this time it prints 'value found' when isFound is true and 'value not found' when isFound = false, however, if I replaced cy.log('Value found!') with return true, and cy.log('Value not found!') with return false, this piece code returns empty.

How could I let me code returns true when isFound===true, and returns false when isFound===false

Jim
  • 55
  • 1
  • 5
  • 1
    It async code. It can't return `true` or `false` depending on `isFound`. The function could return a promise that resolves to `true` or `false`. The function returns before `if (isFound)` is evaluated. – jabaa Aug 02 '23 at 00:04
  • 1
    By the way, `.each()` is totally unnecessary here, and do not use variables such as `const table = cy.get(...)`. – Lola Ichingbola Aug 02 '23 at 00:37
  • How can I avoid using .each() here? – Jim Aug 02 '23 at 00:38
  • how can i let my function return a promise so that it could be resolved to true or false? – Jim Aug 02 '23 at 00:39
  • 1
    Just drop it from the test code - `cy.get('ex-table...).find('div[role="row"]').find('div span span').eq(1).invoke('text')`. – Lola Ichingbola Aug 02 '23 at 00:40
  • 1
    Also, don't write conditional code in Cypress, they are not proper tests when you don't know the expected outcome. – Lola Ichingbola Aug 02 '23 at 00:43
  • 1
    Instead change inner `.then()` to `.should(result => expect(result.trim()).to.eq(policyRuleName.trim()))` – Lola Ichingbola Aug 02 '23 at 00:45
  • 1
    Wrap the code in a promise and return it: `return new Promise((res) => { realTable.each... })` and call `res(true)` resp. `res(false)` next to `cy.log`. – jabaa Aug 02 '23 at 01:22
  • Hi @LolaIchingbola could you please give me the revised code if possible? I was totally lost. Thank you so much! – Jim Aug 02 '23 at 01:43

0 Answers0