【问题标题】:Not sequential execution of JavaScript with Protractor tests不使用 Protractor 测试顺序执行 JavaScript
【发布时间】:2019-11-27 03:43:28
【问题描述】:

每当我尝试通过 Protractor 运行使用 JavaScript 编写的自动化测试脚本时,我可以看到这两个脚本实际上是相互独立地并行运行的。示例:

it("Validation of ND account", function() {

    // I logged in, and navigated to the page I need
    // And this is where is gets intresting

    console.log("\n");
    console.log("Before getting number of users");
    var numberOfUsers = element(by.xpath('//div[@viewid="userList"]//div[@class="results-count ng-binding"]')).getText().then(function(text) {
        console.log(text);
    });
    console.log("After getting number of users");

//  for (i=1, i<numberOfUsers, i++) {
//      console.log(i);
//  }

    });

我假设我以相同的顺序获取日志 - 之前、数字和之后,但首先我得到 JS,然后是量角器(因为加载需要更长的时间)。这是在控制台输出中运行此脚本的结果:

    Started

    Before getting number of users
    After getting number of users
    161

话虽如此,我的问题是,如果我想打开一个页面,获取一个元素文本,然后对其执行一些操作(运行一个被注释掉的 FOR 循环),它不会这样做,因为它将在加载页面之前返回一个未解决的承诺。更准确地说,它的作用是在页面加载之前立即开始打开页面,它将运行该循环,这取决于页面中的元素。循环失败,因为该元素还没有出现,并且程序仍然没有它的 text 属性。那么问题来了:是否可以在没有JS超时或等待函数的情况下严格遵守脚本顺序(在量角器命令执行完成之前,不让JS运行量角器命令之后编写的脚本)?

【问题讨论】:

    标签: javascript angularjs automation protractor


    【解决方案1】:

    您需要了解Promises 并使用回调使其按顺序工作。

    对于这种特殊情况,您不能等待numberOfUsers 让您的测试继续进行,您必须在回调函数中继续:

    console.log("\n");
    console.log("Before getting number of users");
    element(by.xpath('//div[@viewid="userList"]//div[@class="results-count ng-binding"]')).getText().then(function(text) {
        console.log(text);
        // continue working here with "text"
        console.log("After getting number of users");
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    相关资源
    最近更新 更多