javascript - 将页面的多个元素存储到 JSON 对象

标签 javascript node.js protractor

我需要将一个页面的几个文本元素存储到一个 JSON 对象中,但是由于异步性,我无法在 Protractor 中执行此操作。

这是我的代码示例,它位于 element.all() 循环中:

var json = {};
e[i].element(by.css(".nome_projeto")).getText().then(function(text){
    json.nome = text;
});
e[i].element(by.css(".nome_cliente")).getText().then(function(text){
    json.cliente = text;
});
e[i].element(by.css('.data_fim')).getText().then(function(text){
    json.data = text;
});
console.log(json);
list.push(json);

但是,当我登录时,我的 JSON 对象是未定义的,我只能在 then 中访问它。

最佳答案

您可以使用 protractor.promise.all() “一次”解决多个 promise :

protractor.promise.all([
    e[i].element(by.css(".nome_projeto")).getText(),
    e[i].element(by.css(".nome_cliente")).getText(),
    e[i].element(by.css('.data_fim')).getText()
]).then(function (texts) {
    var obj = {};

    obj.nome = texts[0];
    obj.cliente = texts[1];
    obj.data = texts[2];

    console.log(obj);
});

关于javascript - 将页面的多个元素存储到 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43812516/

相关文章:

javascript - knockout : Clone observable object into another variable and turn it not observable

javascript - 使用 Knockjs 时未触发单击事件

javascript - ECMAScript 在其第六版中是否已经停止使用基于原型(prototype)的编程语法?

javascript - 调用函数或将数据传递给另一个 Controller AngularJS

node.js - 加载配置时出错 - 您似乎正在使用 native ECMAScript 模块配置文件 (Jest)

node.js - 无法使用 Node 检查器 : websocket_closed and Assertion failed: Unknown experiment canvasInspection 调试 Nodejs

javascript - 简单测试但定位器无效 :-(

typescript - 如何使用 ts-node 运行 Protractor 配置文件 (.ts)?

node.js - Jest - 函数 fs.writefile 中的模拟回调

javascript - 使用 Protractor 和 Angular 带模态测试登录对话框时出现超时错误