断言输入元素包含赛普拉斯中的特定值
Asserting that an input element contains a specific value in Cypress
我试图断言以下内容,其中 propSizeSel
是我的数字输入元素的 CSS 选择器:
cy.get(propSizeSel).clear().type(100)
.should('contain', 100);
不幸的是,尽管输入元素接受值 100,但此断言以下列方式失败。
如您所见,输入元素已按预期接受值 100:
为什么我似乎不能做出这个简单的断言?
请尝试在单引号中使用 100,在断言中请使用 should('have.value', '100')
而不是 contain;
cy.get('propSizeSel').clear().type('100').should('have.value', '100');
或尝试使用 promise
断言
cy.get('propSizeSel').clear().type('100').invoke('val')
.then(val=>{
const myVal = val;
expect(myVal).to.equal('100');
})
通过@soccerway 的回答和一些更改,我找到了一种方法来实现我想要的,因为我无法正确比较浮点数,事实上,如果该字段有例如 1.320 并且我想检查值是 1.32,用 @ soccerway answer using string 我会遇到类似 expected value to be '1.32' but got '1.320'
的错误,然后我完成了:
cy.get('propSizeSel')
.invoke('val')
.then(val => {
if (val !== undefined) {
expect(parseFloat(val.toString())).to.equal(1.32);
} else {
expect(val).not.equal(undefined);
}
});
在这个问题的情况下 parseInt
可以用来代替 parseFloat
,为了更进一步,因为我有几个这样的电话要做,所以我做了一个 cypressCommonCommands.ts
包含两个函数的文件:
export class CypressCommonCommands {
public static expectElementValueEqualsFloat(pCyElement: string, pValue: number) {
cy.get(pCyElement)
.invoke('val')
.then(val => {
if (val !== undefined) {
expect(parseFloat(val.toString())).to.equal(pValue);
} else {
expect(val).not.equal(undefined);
}
});
}
public static expectElementValueEqualsInt(pCyElement: string, pValue: number) {
cy.get(pCyElement)
.invoke('val')
.then(val => {
if (val !== undefined) {
expect(parseInt(val.toString())).to.equal(pValue);
} else {
expect(val).not.equal(undefined);
}
});
}
}
然后用于本题:
cy.get('propSizeSel').clear().type('100');
CypressCommonCommands.expectElementValueEqualsInt('propSizeSel', 100);
我试图断言以下内容,其中 propSizeSel
是我的数字输入元素的 CSS 选择器:
cy.get(propSizeSel).clear().type(100)
.should('contain', 100);
不幸的是,尽管输入元素接受值 100,但此断言以下列方式失败。
如您所见,输入元素已按预期接受值 100:
为什么我似乎不能做出这个简单的断言?
请尝试在单引号中使用 100,在断言中请使用 should('have.value', '100')
而不是 contain;
cy.get('propSizeSel').clear().type('100').should('have.value', '100');
或尝试使用 promise
cy.get('propSizeSel').clear().type('100').invoke('val')
.then(val=>{
const myVal = val;
expect(myVal).to.equal('100');
})
通过@soccerway 的回答和一些更改,我找到了一种方法来实现我想要的,因为我无法正确比较浮点数,事实上,如果该字段有例如 1.320 并且我想检查值是 1.32,用 @ soccerway answer using string 我会遇到类似 expected value to be '1.32' but got '1.320'
的错误,然后我完成了:
cy.get('propSizeSel')
.invoke('val')
.then(val => {
if (val !== undefined) {
expect(parseFloat(val.toString())).to.equal(1.32);
} else {
expect(val).not.equal(undefined);
}
});
在这个问题的情况下 parseInt
可以用来代替 parseFloat
,为了更进一步,因为我有几个这样的电话要做,所以我做了一个 cypressCommonCommands.ts
包含两个函数的文件:
export class CypressCommonCommands {
public static expectElementValueEqualsFloat(pCyElement: string, pValue: number) {
cy.get(pCyElement)
.invoke('val')
.then(val => {
if (val !== undefined) {
expect(parseFloat(val.toString())).to.equal(pValue);
} else {
expect(val).not.equal(undefined);
}
});
}
public static expectElementValueEqualsInt(pCyElement: string, pValue: number) {
cy.get(pCyElement)
.invoke('val')
.then(val => {
if (val !== undefined) {
expect(parseInt(val.toString())).to.equal(pValue);
} else {
expect(val).not.equal(undefined);
}
});
}
}
然后用于本题:
cy.get('propSizeSel').clear().type('100');
CypressCommonCommands.expectElementValueEqualsInt('propSizeSel', 100);