Asserting an element is focused

后端 未结 7 891
傲寒
傲寒 2020-12-16 18:50

According to the How do I assert an element is focused? thread, you can check if an element is focused by switching to an activeElement() and assert this is the

7条回答
  •  独厮守ぢ
    2020-12-16 19:33

    For the future readers, I've created a custom matcher to assert if an element is active/focused:

    beforeEach(function() {
        jasmine.addMatchers({
            toBeActive: function() {
                return {
                    compare: function(elm) {
                        return {
                            pass: elm.getId().then(function (activeElementID) {
                                return browser.driver.switchTo().activeElement().getId().then(function (currentElementID) {
                                    return jasmine.matchersUtil.equals(currentElementID, activeElementID);
                                });
                            })
                        };
                    }
                };
            }
        });
    });
    

    Usage:

    expect(element(by.css("#myid")).toBeActive();
    

提交回复
热议问题