如何解决如何获取我在 cypress 字段下插入的文本长度
我需要验证一个字段,假设用户名字段不允许作为用户输入插入超过 50 个字符。我可以使用以下内容在此字段中插入文本(60 个字符):
const FIRST_NAME = '#dwfrm_contactus_firstname';
cy.get(FIRST_NAME).type('123456789012345678901234567890123456789012345678901234567890');
/* here i want to get the length of the text which is under the FIRST_NAME input field with a check that it should be equal to 50*/
但是在输入之后,我需要检查在该字段上插入了多少个字符或者字符的长度不应超过50个。在cypress中有没有办法做到这一点?
解决方法
假设 #dwfrm_contactus_firstname
是一个输入元素,类似这样
<input maxlength="50" />
您会从 value
属性而不是 text
属性读回文本。
您的测试可能如下所示
it('ensure first name limits entry to 50 char',() => {
const firstNameText = '123456789012345678901234567890123456789012345678901234567890';
cy.get(FIRST_NAME)
.type(firstNameText)
.should('have.value',firstNameText.substring(0,50));
})
您可以通过将 substring 增加到 51 来检查测试本身,它应该会失败。
如果想直接查看数字长度,需要将主题从元素改为文本,
it('ensure first name limits entry to 50 char',() => {
const firstNameText = '123456789012345678901234567890123456789012345678901234567890';
cy.get(FIRST_NAME) // subject is <input> element
.type(firstNameText)
.invoke('val') // change subject to input's value
.should('have.length',50); // assert the length property is 50
})
为了更好的衡量,在测试输入时,最好先清除它,
cy.get(FIRST_NAME)
.clear() // ensure empty input
.type(firstNameText)
...
,
我使用以下内容来检查这里的验证:
... // text60Characters = 60 个字符,例如 123467890...
cy.get(FIRST_NAME_INPUT).type(testdata.text60Characters); cy.get(FIRST_NAME_INPUT).invoke('val').its("length").should("eq",50);
...
这对我有用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。