問題描述
我想在我的 Angular 2 應用程序中測試我的隱藏顯示按鈕的功能(測試是用 Jasmine 編寫的),所以我需要檢查相關元素的 display
屬性的值.如何使用 Angular 的 debugElement
獲取此屬性?測試代碼:
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');
我得到錯誤:預期 undefined 為假".
I get the error: Expected undefined to be 'false'.
推薦答案
對于任何偶然發(fā)現(xiàn)這個例子的人來說,display
這個特定問題的解決方案是 hidden
屬性在 debugElement
上.如果元素被隱藏,它將包含 true
,否則將包含 false
.
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.
這篇關于Angular 2 測試 - 獲取 DOM 元素樣式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!