仅使用 Jasmine 测试框架检查元素是否具有 class

Check if element has class only with Jasmine test framework

我正在使用 webdriverJSJasmine 对网页执行端到端测试。我想测试一个元素在某些情况下是否具有 class,但我想使用 pure jasmine.

中的方法来进行测试

这是问题所在的代码部分:

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');
    });
})

你知道是否有办法解决这个问题,或者我可以看几个例子,没有 使用像 jasmine-jquery 这样的扩展?

提前感谢您的回复!

如果您不希望 jasmine-jquery 或其他第三方包引入自定义 jasmine 匹配器作为依赖项,您可以随时提取 toHaveClass() matcher implementation and use it. Note that having your assertion logic encapsulated inside custom matchers helps to follow the DRY principle 并使您的测试更清晰。

仅供参考,这是我们当前使用的 toHaveClass 实现:

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;
                        })
                    };
                }
            };
        },
    });
});