Angular 2 tests - get DOM element styles

依然范特西╮ 提交于 2019-12-04 17:38:25

问题


I want to test the functionality of my hide-show button in my Angular 2 app(Tests are written in Jasmine), so I need to check the value of the display property of the relevant element. How can I get this property using Angular's debugElement? Test code:

let input = fixture.debugElement.query(By.css('input'));
expect(input.styles['visibility']).toBe('false');

I get the error: Expected undefined to be 'false'.


回答1:


I was having the same issue. The DebugElement.styles is always an empty object even if I set some style to that element explicitly(maybe bug in angular code?). So I would rather get that from the browser native element directly:

let input = fixture.debugElement.query(By.css('input'));
expect(input.nativeElement.style.visibility).toBe('false');



回答2:


For anyone stumbling upon this example, the solution for this specific issue with display is the hidden property on the debugElement. It will contain true if the element is hidden and false otherwise.



来源:https://stackoverflow.com/questions/41979957/angular-2-tests-get-dom-element-styles

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