检查元素是否仅具有Jasmine测试框架的类 [英] Check if element has class only with Jasmine test framework
问题描述
我正在使用 webdriverJS
和 Jasmine
来执行网页的端到端测试。我想在某些情况下测试一个元素是否有 class
,但我想使用 pure jasmine
。
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');
});
})
你知道是否有办法解决这个问题,或者我可以看一些例子,没有使用像 jasmine-jquery $这样的扩展c $ c>?
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
?
提前感谢您的回复!
推荐答案
如果你不想要 jasmine-jquery
或其他第三方软件包引入自定义茉莉花匹配器作为依赖项,您始终可以提取 toHaveClass()
matcher implementation 和使用它。请注意,将断言逻辑封装在自定义匹配器中有助于遵循 DRY原则并使您的测试更清洁。
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.
仅供参考,这是 toHaveClass
我们目前正在使用的实施:
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;
})
};
}
};
},
});
});
这篇关于检查元素是否仅具有Jasmine测试框架的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!