如何在cypress中运行递归函数或使用异步等待查找长度

时间:2019-09-24 13:37:56

标签: e2e-testing cypress

我正在使用cypress进行测试。 我有一系列莱特币地址。我试图在输入中设置第一个。然后提交表格。
如果地址重复,则会显示通知,并且“提交”按钮将不可见。我想设置第二个元素。依此类推,直到阵列结束。 我尝试了递归函数:

function runTillElementFound (totalCount, currentCount, litecoin_addresses)
    {
        var self = this;
        if (currentCount < totalCount) {
            return cy.get('body').then(($body) =>
            {
                if ($body.find(dash_page.save_wallet_circle_btn)) {
                    //if there is save button then set address and submit form
                    cy.log('taken address: ' + litecoin_addresses[ currentCount ]);
                    dashact.fill_wallet(litecoin_addresses[ currentCount ]);
                    cy.log('address is filled');
                    dashact.submit_wallet(true, 0);
                    self.runTillElementFound(totalCount, currentCount++);
                }
            });
        } else {
            return false; //if element not present after Max count reached.
        }

我尝试称呼它

it('Set wallet', () =>
    {
        cy.log('this is array length: ' + litecoin_addresses);
        runTillElementFound(20, 0, litecoin_addresses);
        /* comact.submit_form(true, 1);
        let ltc_address = promisify(dashact.get_wallet_value());
        cy.log('this is address: ' + ltc_address);
        //close popup and check that it is closed:
        popact.submit_payment(); */
    });

但是我收到未定义的信息: undefined error

我还尝试了非递归功能:

for (var i = 0; i < litecoin_addresses.length; i++) {
            cy.log('taken address: ' + litecoin_addresses[ i ])
            if (litecoin_addresses[ i ] == wallet_before_edit || litecoin_addresses[ i ].length == 0 || litecoin_addresses[ i ].startsWith('ltc')) {
                continue;
            }
            else {
                cy.log('this is curent i: ' + i);
                dashact.fill_wallet(litecoin_addresses[ i ]);
                dashact.submit_wallet(true, null);
                cy.get('body').then(($body) =>
                {
                    // synchronously query from body
                    // to find which element was created
                    if ($body.find(com_page.not_message).length) {
                        // error was found, do something else here
                        cy.log('error was found');
                    }
                    else {
                        cy.log('error not found');
                        // input was not found, do something else here
                        i = litecoin_addresses.length;
                        cy.log('current i value: ' + i);
                    }
                })
            }

但是可以肯定的是,它不起作用,因为我在promise内部的价值很高,但是在循环中仍然保持不变。

1 个答案:

答案 0 :(得分:0)

如果在测试代码中使用特定的数组,则可以使用.lenght轻松获取数组的长度,并使用for循环访问其元素。