通过CSS定位元素时,量角器不会返回正确的值

时间:2015-03-03 22:34:47

标签: javascript protractor

我有以下代码

            map['alarmName'] =element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(2) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-left > h4')).getText();
            map['siteName'] = element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(2) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-left > h5')).getText();
            map['timeStamp'] = element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(2) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-right > div > h4')).getText();
            map['status'] = element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(2) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-right > div > h5:nth-child(2)')).getText();
            map['owner'] = element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(2) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-right > div > h5:nth-child(3)')).getText();

我希望得到上面所有标签的文字但是当我在webstorm中运行这个量角器测试时,我得到以下结果


alarmName { then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

siteName { then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

timeStamp { then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

status { then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

我已经通过运行chrome控制台验证了该元素是否存在,并且已定义nthAlarm。但不确定我做错了什么。

非常感谢任何帮助。 感谢。


好吧我把我的代码重写为

this.getAlarmDetailsFromUnclaimedAlarms = function (nthAlarm) {

    var map = new Object();
    // as per the DOM, this is needed. So, first alarm should have nthAlarm value as 2 and so on.
    var nthAlarm = nthAlarm+1;




    browser.sleep(10000);

/*  element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(1) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-left > h4'))
        .className.then(function(className) {
            map['severity'] = className;
        });*/


    element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(1) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-left > h4'))
        .getText().then(function(text){
            map['severity'] = text;
        });


    element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(1) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-left > h5'))
        .getText().then(function(text){
            map['alarmName'] = text;
        });

    element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(1) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-right > div > h4'))
        .getText().then(function(text){
            map['timeStamp'] = text;
        });

    element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(1) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-right > div > h5:nth-child(2)'))
        .getText().then(function(text){
            map['status'] = text;
        });



    console.log("the value is ",map['status']);
    return map;

};

当我运行此代码时,它返回UNDEFINED。

1 个答案:

答案 0 :(得分:0)

getText返回获取文本的承诺。它实际上并不返回文本。阅读https://github.com/angular/protractor/blob/master/docs/control-flow.md

您可以尝试以下方式:

 element(by.css('div.col-md-4.alarms-list-container.ng-scope > ul:nth-child(2) > li:nth-child('+nthAlarm+') > a > div.col-md-5.pull-left > h4'))
    .getText().then(function(actualText) { 
       map['alarmName'] = actualText;
    });