admin管理员组文章数量:1334161
I'm trying to test that invalid input warns the user. The html "type" of that input is "email", so the browser actually displays this bubble rather than an element we expose on the site.
This bubble in question as far as I can tell is browser-specific and as far as I can tell it doesn't appear in the DOM. Also, I can't use the "select an element" cursor tool on it, it ignores that bubble as if it wasn't there. If it helps, it's a browser-specific event triggered by the input form.
Is there a way to confirm that this alert exists using Cypress assertions?
I'm trying to test that invalid input warns the user. The html "type" of that input is "email", so the browser actually displays this bubble rather than an element we expose on the site.
This bubble in question as far as I can tell is browser-specific and as far as I can tell it doesn't appear in the DOM. Also, I can't use the "select an element" cursor tool on it, it ignores that bubble as if it wasn't there. If it helps, it's a browser-specific event triggered by the input form.
Is there a way to confirm that this alert exists using Cypress assertions?
Share Improve this question asked Feb 23, 2022 at 15:19 J.LongJ.Long 3331 gold badge4 silver badges12 bronze badges3 Answers
Reset to default 5For the record, I found a great example here
Cypress examples (v9.4.1) - Form validation
<form id="form-validation" action="/action_page.php">
<div>
<label for="item">Item:</label>
<input id="item" type="text" name="item" required />
</div>
cy.get('#item:invalid')
.invoke('prop', 'validationMessage')
.should('equal', 'Please fill out this field.')
Okay, found a workable solution. If someone has a better solution please feel free to post.
As HTML form validation alert bubble text is browser specific, it's probably not a good idea to test for specific content within the bubble. Instead, we can test that the form fired an invalid event and giving the input
form the :invalid
pseudo-class, and that the browser blocked the form from submitting.
These two things happen regardless of browser. The easiest way to detect this behavior in the DOM is to go from selecting input
to asserting that input:invalid
exists and that login did not happen.
As mentioned in the cypress docs, you can do the validation like this:
cy.get('username-selector').then(($input) => {
expect($input[0].validationMessage).to.include(
`Please include an '@' in your email address.`
)
})
版权声明:本文标题:javascript - Cypress - Testing that a browser-specific alert exists when submitting invalid input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742364497a2461013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论