将页面的几个元素存储到JSON对象
我需要将页面的几个文本元素存储到一个JSON对象,但是由于asynchronous性,我无法在量angular器中执行此操作。
这里是我的代码的一个例子,它是在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);
但是,当我login时,我的JSON对象是不确定的,我只能在那里访问它。
您可以用protractor.promise.all()
一次性解决多个承诺:
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); });
我看到意大利的名字:D我们可以用意大利语说话:D
顺便说一句,我build议你用这个代码来改变它,以pipe理所有的承诺:
var json = {}; var promises = [ e[i].element(by.css(".nome_projeto")).getText(), e[i].element(by.css(".nome_cliente")).getText(), e[i].element(by.css('.data_fim')).getText() ]; protractor.promise.all(promises).then(function(data) { json.nome = data[0]; json.cliente = data[1]; json.data = data[2]; list.push(json); });
它应该工作
- 嵌套承诺陷入困境
- Angular 4+ Express不是从Heroku开始的
- 包括TypeScript编译上下文中的app.component.ts
- 未被捕获(承诺):收到响应之前,消息端口已closures
- 要删除Browser.sleep()
- 如何使用Mongoose,Express,Angular 4和NodeJS上传/保存和显示图片
- 我正在使用angular2斜angularcli,并尝试使用ng2-datetime,但它给错误“TypeError:无法读取属性'jQuery'未定义的”
- Aurelia e2e:检查div内的跨度是否包含特定的文本
- 在Angular 2 + Electron中使用绝对path