Nightwatch 变量范围和访问外部 .element 调用

Nightwatch variable scope and accessing outside .element call

我的 Nightwatch 测试中有以下方法没有按预期工作:

function checkCategoryRows(browser, theOptionText, rows) {
    var isGood = true;
    rows.value.forEach(function (row) {
        browser.elementIdText(row.ELEMENT, function (categoryText) {
            if (categoryText.value != theOptionText) {
                browser.verify.ok(0 == 1, categoryText.value + ' = ' + theOptionText);
                isGood = false;
            } else 
                isGood = false; //<-- Manually making sure it sets either way
        })
    })
    browser.verify.ok(isGood == true, theOptionText + ' category is good...');
}

isGood

始终为真;即使我手动设置它。似乎 browser.element(){} 调用中的任何内容都只在该调用中起作用。 我如何让它工作,以便在 for loop 的末尾我可以证明在特定的一组行中有东西是 'not good'?

显然,在 Nightwatch 中,没有 'document' 或 'window' 对象。有一个 'browser' 对象似乎是相似的。这是我解决它的方法:

function checkCategoryRows(browser, theOptionText, rows) {
     browser.isGood = true;
     rows.value.forEach(function (row) {
         browser.elementIdText(row.ELEMENT, function (categoryText) {
             //console.info(categoryText)
             if (categoryText.value != theOptionText) {
                 browser.verify.ok(0 == 1, categoryText.value + ' = ' + theOptionText);
                 browser.isGood = false;
             }
             //else
             //    browser.isGood = false; // for testing
             console.log('browser.isGood=' + browser.isGood);
         })
     })
     browser.verify.ok(browser.isGood == true, theOptionText + ' category is good...');
}

设置 browser.isGood = true; 最初使其可用于方法的其余部分。