Check if element has class only with Jasmine test framework

£可爱£侵袭症+ 提交于 2019-11-30 20:54:26

问题


I am using webdriverJS and Jasmine to perform an end-to-end testing of a web page. I would like to test if an element has class under certain circumstances, but I would like to do it using methods from pure jasmine.

This is the part of the code where the issue is located:

describe('Header bar', function() {
    it('should show/hide elements accoding to the window position', function() {
        this.driver.executeScript('scroll(0, 1000)');
        var elemSearch = this.driver.findElements(webdriver.By.id('animatedElement, animatedElement2, animatedElement3'));
        expect(elemSearch).toContain('appear-2');
    });
})

Do you know if there's a way to solve this issue, or a couple of examples I could look at, without using extensions like jasmine-jquery?

Thanks in advance for your replies!


回答1:


If you don't want having jasmine-jquery or other third-party packages introducing custom jasmine matchers as a dependency, you can always extract the toHaveClass() matcher implementation and use it. Note that having your assertion logic encapsulated inside custom matchers helps to follow the DRY principle and make your tests cleaner.

FYI, here is toHaveClass implementation we are currently using:

beforeEach(function() {
    jasmine.addMatchers({
        toHaveClass: function() {
            return {
                compare: function(actual, expected) {
                    return {
                        pass: actual.getAttribute("class").then(function(classes) {
                            return classes.split(" ").indexOf(expected) !== -1;
                        })
                    };
                }
            };
        },
    });
});


来源:https://stackoverflow.com/questions/32698155/check-if-element-has-class-only-with-jasmine-test-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!