我需要将一个页面的几个文本元素存储到一个 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/