Den*_*son 2 jasmine angularjs protractor
我正在编写一个量角器规范,它使用angularJS和jasmine从页面获取文本,我后来想要从字符串转换为整数.我的第一个问题是访问函数外部的getText值.例如,在下面的代码中:
var post_count = element.all(by.binding('InboxItem.Count')).get(0);
post_count.getText().then (function (text){
console.log('mytext_inside_function = ' + text);
return text;
});
console.log('mytext_outside_function = ' + post_count);
Run Code Online (Sandbox Code Playgroud)
我可以看到代码是异步执行的,console.log我的text_outside_function在my_text_inside_function之前运行,因此返回"对象对象"而不是数字7(这是文本而不是数字)
? protractor protractor.conf.js --suite inbox
[10:56:34] I/local - Starting selenium standalone server...
[10:56:35] I/launcher - Running 1 instances of WebDriver
Started
mytext_outside_function = [object Object]
mytext_inside_function = 7
.
1 spec, 0 failures
Finished in 0.938 seconds
Run Code Online (Sandbox Code Playgroud)
我是茉莉花承诺的新手,不知道如何纠正这一点.我添加了.then(函数...对我的测试,但似乎没有解决承诺.我的最终目标是获取文本(7)然后将其从当前字符串转换为数字,以便我可以做计算(添加)它.
编辑....感谢Nick的建议.我想我做错了.我添加了mytext变量,我认为它在函数之外(可能是错误的).见下文..
var post_count = element.all(by.binding('InboxItem.Count')).get(0);
var mytext = post_count.getText().then (function (text){
console.log('mytext_inside_function = ' + text);
return text;
});
console.log('mytext_outside_function = ' + my text);
Run Code Online (Sandbox Code Playgroud)
我收到这个错误.
mytext_outside_function = ManagedPromise::810 {[[PromiseStatus]]: "pending"}
Run Code Online (Sandbox Code Playgroud)
或者调用console.log
在的分辨率Promise
:
var result = element(by.binding('InboxItem.Count')).getText().then(parseFloat);
result.then((value) => console.log("count: ", value));
Run Code Online (Sandbox Code Playgroud)
或者在执行流程中推送一个新函数来显示结果:
var count = 0;
element(by.binding('InboxItem.Count')).getText().then((text) => {
count = parseFloat(text);
});
browser.controlFlow().execute(() => {
console.log("count: ", count);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1018 次 |
最近记录: |