下面是兩個測驗:
it("It should now show the Send Reset Instructions link", () => {
fsrloginpage.SendResetInstructions("Send me reset").should('exist');
});
it("Ihe Send Reset Instructions link should be disabled if the Email is empty", () => {
fsrloginpage.UsernameEmailField().clear();
fsrloginpage.SendResetInstructions("Send me reset").should('have.attr','disabled','true');
});
這是 .SendResetInstructions 物件定義:
SendResetInstructions(linklabel){
return cy.get('button[class^="mat-focus-indicator mat-button mat-raised-button
mat-button-base mat-primary"]').contains(linklabel);
}
這是我的結果:
It should now show the Send Reset Instructions linkpassed
TEST BODY
1
getbutton[class^="mat-focus-indicator mat-button mat-raised-button mat-button-base mat-primary"]
2
containsSend me reset
3
assertexpected <span.mat-button-wrapper> to exist in the DOM
Ihe Send Reset Instructions link should be disabled if the Email is emptyfailed
TEST BODY
1
getinput[id="mat-input-2"]
2
clear
3
getbutton[class^="mat-focus-indicator mat-button mat-raised-button mat-button-base mat-primary"]
4
containsSend me reset
5
assertexpected <span.mat-button-wrapper> to have attribute disabled
AssertionError
Timed out retrying after 4000ms: expected '<span.mat-button-wrapper>' to have attribute 'disabled'
mcare/integration/FSRLoginBVT.spec.js:68:57
66 | it("Ihe Send Reset Instructions link should be disabled if the Email is empty", () => {
67 | fsrloginpage.UsernameEmailField().clear();
> 68 | fsrloginpage.SendResetInstructions("Send me reset").should('have.attr','disabled','true');
| ^
69 | });
70 |
71 | it("Ihe Send Reset Instructions link should be enabled if the Email is filled", () => {
因此,它在第一個測驗中找到了物件,但是有一個斷言(#1)。在第二個測驗中,它似乎忽略了按鈕,并嘗試使用它在斷言 (#2) 中提到的 <span.mat-button-wrapper> 物件。我認為這樣做是因為按鈕的識別符號在按鈕內的范圍內。這是我正在測驗的代碼:
<button mat-button="" mat-raised-button="" color="primary" disabled="true">
<span > Send me reset password instructions </span>
<span matripple="" ></span>
<span ></span>
</button>
關于如何解決這個問題的任何想法?最好的解決方案是讓開發人員將 ID 放入他們的代碼中,但這不太可能及時發生。
uj5u.com熱心網友回復:
您并不真的希望選擇器中包含這些類,它們用于 Material Design 樣式,并且很可能很常見以至于無法區分按鈕。
只需通過標簽內容識別按鈕
SendResetInstructions(linklabel) {
return cy.contains('button', linklabel);
}
我會說 POM 方法命名也意味著固定文本,例如你永遠不會這樣呼叫,這會令人困惑
fsrloginpage.SendResetInstructions("Log me in")
所以你不妨把文字封裝起來
SendResetInstructions() {
return cy.contains('button', 'Send me reset');
}
最后,使用be.disabled斷言而不是have.attribute斷言,因為disabled屬性(有時)存在或不存在,而不是真或假。be.disabled涵蓋這兩種情況。
fsrloginpage.SendResetInstructions().should('be.disabled');
uj5u.com熱心網友回復:
問題來自這樣一個事實,即您.contains()產生了span它找到的元素,而不是button包裝三個spans 的元素。yieldedspan沒有disabled=true,因此您的斷言失敗。
為了回傳父元素,您可以采用一些策略,但最簡單的一種是傳入產生的所需元素型別.contains。
SendResetInstructions(linklabel){
return cy.get('button[class^="mat-focus-indicator mat-button mat-raised-button
mat-button-base mat-primary"]').contains('button', linklabel);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507612.html
上一篇:NPM版本太舊
