获取量角器中的元素属性值 [英] get element attribute value in Protractor

查看:82
本文介绍了获取量角器中的元素属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个量角器测试,该测试必须等待element属性具有非空值,然后要将其返回给调用方函数。事实证明,这比我想象的要难写!

I'm writing a Protractor test that has to wait for an element attribute to have a non-empty value and then I want to return that value to the caller function. This has proven to be more difficult to write than I expected!

我能够正确安排 browser.wait()命令等待element属性具有非空值,并且我已经验证了该值实际上是我期望在回调函数中获得的值,但是由于某些原因,我无法返回该值

I am able to correctly schedule a browser.wait() command to wait for the element attribute to have a non-empty value and I have verified that this value is in fact what I am expecting to get inside the callback function, but for some reason, I am not able to return that value outside of the callback function and onto the rest of the test code.

这是我的代码的样子:

function test() {
    var item = getItem();
    console.log(item);
}

function getItem() {
    var item;
    browser.wait(function() {
        return element(by.id('element-id')).getAttribute('attribute-name').then(function(value) {
            item = value;
            // console.log(item);
            return value !== '';
        });
    });
    return item;
}

我可以说执行顺序不符合我的预期,因为当我取消注释回调函数中的 console.log()调用时,我看到了预期值。但是,在 test()函数中的同一调用将显示未定义。

I can tell that the order of execution is not as I expect it to be, because when I uncomment the console.log() call inside the callback function, I see the expected value printed out. However, the same call in the test() function prints 'undefined'.

这是怎么回事?我想念什么?如何从回调函数中正确获取属性值?

What is going on here? What am I missing? How can I get the attribute value out of the callback function properly?

感谢您的帮助。

推荐答案

我不会将等待和获取属性部分结合在一起-逻辑上这是两个分开的东西,将它们分开

I would not combine the wait and the getting attribute parts - logically these are two separate things, keep them separate:

browser.wait(function() {
    return element(by.id('element-id')).getAttribute("attribute").then(function(value) {
        item = value;
        // console.log(item);
        return value !== '';
    });
});

element(by.id('element-id')).getAttribute("attribute").then(function (value) {
    console.log(value);
});

请注意,您可以通过以下方式简化等待条件:

Note that, you may simplify the wait condition this way:

var EC = protractor.ExpectedConditions;
var elm = $('#element-id[attribute="expected value"]');

browser.wait(EC.presenceOf(elm), 5000);
elm.getAttribute("attribute").then(function (value) {
    console.log(value);
});






仅供参考,您可能已经解决了当前的问题与 递延

function test() {
    getItem().then(function (value) {
        console.log(value);
    });
}

function getItem() {
    var item = protractor.promise.defer();
    browser.wait(function() {
        return element(by.id('element-id')).getAttribute('attribute').then(function(value) {
            var result = value !== '';
            if (result) {
                item.fulfill(value);
            }
            return result;
        });
    });
    return item.promise;
}

这篇关于获取量角器中的元素属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆