如何扩展回调函数中声明的变量的范围。 Nightwatch e2e测试

时间:2017-09-19 06:21:53

标签: nightwatch.js

在我的夜班测试中,我想将一个值从回调传递给我的断言语句。但我无法找到解决方案。

const navCategoriesMen = function(client, subCat, assertUrl, catName){ client .url("") .waitForElementVisible('div.row.header-main-nav-content li.MEN') .moveToElement('div.row.header-main-nav-content li.MEN',0,0) .pause(3000) .elements('css selector',subCat, function(els){

         for(var i=1; i<=els.value.length; i++){
            var abc;

            var catName= client.getText('li.MEN.open ul li li:nth-child(1) ul > a:nth-child('+i+')', function(res){
                abc = res.value;
                console.log(abc);

            }); 

            client
            .click('li.MEN.open ul li li:nth-child(1) ul > a:nth-child('+i+')')
            .waitForElementVisible('.banner-section.category-page-banner.banner-sec')
            .assert.containsText('#facet-browse > div > div > ul > li.global-views-breadcrumb-item-active',abc)

但我得到了&#39; abc&#39;作为未定义的值。

2 个答案:

答案 0 :(得分:1)

显然,assert语句在回调返回之前执行,就像这个伪代码所示:

&#13;
&#13;
var abc; //undefined

var catName = client.getText('li.MEN.open ul li li:nth-child(1) ul > a:nth-child(' + i + ')', //waiting for the answer

//execute that inbetween
client
  .click('li.MEN.open ul li li:nth-child(1) ul > a:nth-child(' + i + ')')
  .waitForElementVisible('.banner-section.category-page-banner.banner-sec')
  .assert.containsText('#facet-browse > div > div > ul > li.global-views-breadcrumb-item-active', abc) //abc still undefined
  
  //response from the getText, execute the call back
  function(res) {
  abc = res.value;
  console.log(abc);

});
&#13;
&#13;
&#13;

您必须使用返回模式(您可能会考虑ES8 async / await)或回调模式,但您无法从回调中获取值。如果可能的话,你可以做的是测试回调中的结果,如下所示:

&#13;
&#13;
const navCategoriesMen = function(client, subCat, assertUrl, catName) {
    client
      .url("")
      .waitForElementVisible('div.row.header-main-nav-content li.MEN')
      .moveToElement('div.row.header-main-nav-content li.MEN', 0, 0)
      .pause(3000)
      .elements('css selector', subCat, function(els) {

          for (var i = 1; i <= els.value.length; i++) {
            var abc; // undefined

            var catName = client.getText('li.MEN.open ul li li:nth-child(1) ul > a:nth-child(' + i + ')', function(res) {
              abc = res.value;
              console.log(abc); // something, I'm sure you know what 
              
              // abc has the good value, let's run the test
              client
                .click('li.MEN.open ul li li:nth-child(1) ul > a:nth-child(' + i + ')')
                .waitForElementVisible('.banner-section.category-page-banner.banner-sec')
                .assert.containsText('#facet-browse > div > div > ul > li.global-views-breadcrumb-item-active', abc) //abc = something
            });
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您还可以使用执行功能

进行环绕
const navCategoriesMen = function(client, subCat, assertUrl, catName){
client
    .url("")
    .waitForElementVisible('div.row.header-main-nav-content li.MEN')
    .moveToElement('div.row.header-main-nav-content li.MEN',0,0)
    .pause(3000)
    .elements('css selector',subCat, function(els){

     for(var i=1; i<=els.value.length; i++){
        var abc;

        var catName= client.getText('li.MEN.open ul li li:nth-child(1) ul > a:nth-child('+i+')', function(res){
            abc = res.value;
            console.log(abc);

        }); 

        client
        .click('li.MEN.open ul li li:nth-child(1) ul > a:nth-child('+i+')')
        .waitForElementVisible('.banner-section.category-page-banner.banner-sec');
        client.perform(()=>{client.assert.containsText('#facet-browse > div > div > ul > li.global-views-breadcrumb-item-active',abc)});