小编典典

循环无法正常工作-守夜人

selenium

我有这段代码,我想浏览页面底部所有可用的链接。单击它们后,我要确保打开的URL是正确的。我认为递归调用还为时过早。另一个问题是如何判断链接属于某个URL。

function links(browser, total_links) {
    if (total_links <= 0) {
        browser.end();
        return;
    }

    console.log("Number of links: " + total_links);
    console.log('Flag1');

         browser
            .waitForElementVisible('.bottom .socal>span:nth-child(' + total_links + ')', 1000, function () {

            console.log('Flag2');
            browser.execute('scrollIntoView(alignToBottom)')

            .moveToElement('.bottom .socal>span:nth-child(' + total_links + ')', 3, 3)
                .pause(3000)
                .click('.bottom .socal>span:nth-child(' + total_links + ') a', function () {
                    console.log('Flag3');
                    browser.keys(['\uE006'])
                    //  .assert.urlContains('facebook')
                    //.assert.urlEquals('https://www.facebook.com/unitel.ao/?fref=ts')
                            .window_handles(function (result) {
                            console.log('Flag4');
                            browser.assert.equal(result.value.length, 2, 'There should be two windows open.');
                            var handle_1 = result.value[0];
                            var handle_2 = result.value[1];
                            browser.switchWindow(handle_2, function () {
                                browser.closeWindow()
                                    .switchWindow(handle_1, function () {
                                        total_links = total_links - 1;
                                        links(browser, total_links);
                                    });
                            });
                         });

                    console.log('Flag5');
                });
            console.log('Flag6');   
        });
}

module.exports = {
    'Social links': function (browser) {
        var total_links;

        browser
            .url('http://m.unitel.ao/fit/')
            .execute(function () {
                    return document.querySelectorAll("ul.navbar-nav>li").length;
                },
                function (tags) {
                    total_links = tags.value;
                    links(browser, total_links);

                });

        //  .end();
    }
};

阅读 378

收藏
2020-06-26

共1个答案

小编典典

嗯,看来您几天前就受够了。我建议您使用page-object,它将帮助您避免使用硬代码,并在将来更轻松地更改css。

主页对象(home.js)可能像这样:

module.exports = {
  url: function() {
    return 'http://m.unitel.ao/fit/';
  },
  commands: [{
    getUrl: function(n) {
      if (n === 3) {
        return 'youtube.com/user/tvUNITEL';
      }
      if (n === 1) {
        return 'facebook.com/unitel.ao/?fref=ts';
      }
      if (n === 2) {
        return 'instagram.com/unitelangola/';
      }
      if (n === 4) {
        return 'plus.google.com/110849312028181626033/posts';
      }
    }
  }],
  elements: {
    facebook: {
      selector: '.bottom .socal>span:nth-child(1)',
    },
    instagram: {
      selector: '.bottom .socal>span:nth-child(2)'
    },
    youtube: {
      selector: '.bottom .socal>span:nth-child(3)'
    },
    googleplus: {
      selector: '.bottom .socal>span:nth-child(4)'
    }
  }
};

并且在您的测试中应该像:

module.exports = {
  'Social links': function(browser) {
    const homePage = browser.page.home();
    var j = 0;
    for (var i in homePage.elements) {
      homePage
        .navigate()
        .waitForElementVisible(homePage.elements[i].selector, 5000, false,
          function() {
            browser.pause(3000);
          })
        .click(homePage.elements[i].selector, function() {
          browser
            .pause(2000)
            .window_handles(function(result) {
              url = homePage.getUrl(j + 1);
              var home = result.value[0];
              var handle = result.value[1];
              browser
                .switchWindow(handle)
                .verify.urlContains(url)
                .closeWindow()
                .switchWindow(home);
              j += 1;
            });
        })
    }
  }
};

PS:如果您不知道如何创建页面对象,请访问http://nightwatchjs.org/guide#using-page-
objects文档。

在配置文件
Nightwatch.js中

  "src_folders" : ["tests"],
  "output_folder" : "reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "./lib/pages", /* you need to add the path,e.g: './lib/pages', */
  "globals_path" : "",
2020-06-26