I have this code, I want to go thru all the links available at the bottom of the page. After clicking them I want to make sure the URL opened is the correct one. I think the the recursive calls are done too early. Another issue is how can I do to tell that link belongs to certain URL.

我有这个代码,我想通过页面底部的所有链接。点击它们后,我想确保打开的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();
    }
};

1 个解决方案

#1


0

Humh, it seems like you were stuck with this days ago.I recommend page-object,it will help you stay away hardcode and easier to change css in the future.

嗯,好像你几天前就被困住了。我推荐页面对象,它会帮助你远离硬编码,将来更容易改变css。

A home page object(home.js) may be like this :

主页对象(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)'
    }
  }
};

And in your test should be like :

在你的测试中应该是这样的:

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:In case you dont know how to create a page-object, here is the doc http://nightwatchjs.org/guide#using-page-objects.

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

In config file
Nightwatch.js:

在配置文件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" : "",

更多相关文章

  1. 我在显示随机选择的对象时遇到问题
  2. 如何在JavaScript / jQuery中获取对象的属性?
  3. 令人惊奇的JavaScript面向对象(一)
  4. JavaScript面向对象程序设计三——原型模式(上)
  5. 迭代angularjs中对象中的属性列表
  6. Safari / Chrome中的全局控制台对象被重置
  7. 图片在页面内随意飘动,遇到边界还会反弹
  8. 如何判断字符串是一个字符串化的JSON对象
  9. js点击button按钮跳转到另一个新页面

随机推荐

  1. Tensorflow部分函数功能
  2. python爬网页,做k线图
  3. python类的成员和装饰器
  4. 用python完成1-100的加法
  5. python多线程文件传输范例(C/S)
  6. [Python]PDF合成小程序PDF合成小程序
  7. 关于python return 和 print 的区别
  8. python基础练习--列表问题
  9. python list range 字符串的截取 如 text
  10. 如何将两个列表中的数据写入csv中的列?