javascript - 带有 if 条件 Protractor 的异步 for 循环

标签 javascript testing protractor end-to-end

    var createTime=10:00;
    var createdPatient="vijay";
    /* In for loop after comparing the time with createdTime 'i' value should be the compare index but 'i' value becoming last index each time*/ 
    element(by.xpath("//div[@id='patients']")).all(by.xpath("div[@class='ng-scope']")).then(
function(divtags){
for(i=0;i<divtags.length;i++){
divtags[i].element(by.xpath("div[1]/div[2]")).getText().then(function(time){
if(time==createdTime){
 /*Here i am getting 'i' value every time last divtag length */        divtags[i].element(by.xpath("div[@class='patient']")).getText().then(function(patientName){
if(patientName==createdPatient){
divtags[i].click();
}})}});}});

请告诉我如何在将时间与 Protractor 循环代码中创建的时间进行比较后获得正确的索引“i”值。

最佳答案

不,这不是您在 Protractor 中解决此类问题的方式。你需要一个 filter()按时间和姓名过滤所需的“患者”。

这是经过一些额外改进的代码:

$$("#patients > div").filter(function (patient) {
    return patient.element(by.xpath("div[1]/div[2]")).getText().then(function (time) {
        return patient.$(".patient").getText().then(function (patientName) {
            return time === createdTime && patientName == createdPatient;
        });
    });
}).first().click();

不能保证它会按原样工作,因为我没有办法重现和测试它,但我希望你能理解并能适本地调整建议的解决方案。

关于javascript - 带有 if 条件 Protractor 的异步 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38819756/

相关文章:

javascript - Google Cloud Vision API 返回错误结果?

javascript - Three.js:使用 RawShaderMaterial 渲染 LineSegments 线框

javascript - 是否可以在 TypeScript 中实例化 JavaScript 对象?

c# - SpecFlow 如何测量每个步骤的持续时间?

javascript - React 组件未接收 Redux 存储更新的 props

nhibernate - 如何测试直接使用NHibernate的服务层?

testing - 低内存开销 XML 解析

Angular2 E2E Protractor 。等到元素有类

angularjs - Protractor - 在 Ubuntu 14.04 上启动时出错

html - protractor-jasmine2-html-reporter 不显示多重能力测试的结果