【发布时间】:2017-11-08 12:46:58
【问题描述】:
我想在 Cypress 中测试增加和减少 HTML 输入字段 (type="number") 的值。更准确地说,我更喜欢使用箭头键来增加和减少值,但我似乎无法使用最明显的方法来实现它。
作为一个最小的工作示例,我设置了一个 React 组件,其渲染方法如下所示:
render() {
return (<input type="number" />);
};
在赛普拉斯之外,在此字段中输入内容没有任何问题。使用箭头键和鼠标递增和递减值也是如此。
According to Cypress' API documentation on the .type-method,可以使用所谓的特殊字符序列 {uparrow} 和{downarrow} 作为cy.type() 的参数来模拟用户的向上和向下按键。我已经尝试在输入标签和文档正文 (in case the increment/decrement listeners are somehow defined globally) 上使用这种方法,如下所示,但它似乎不起作用:
it('Increments the input value when the up key is pressed', () => {
cy.get('input').type('1000{uparrow}');
// Sets the value to 1000, but does not increment the value
cy.get('body').type('{uparrow}');
// Still at 1000. The event listener is not global after all.
cy.get('input').type('{selectall}{backspace}');
// Selecting all the input and deleting it
// using some of the other "special character sequences" works.
});
查看 Cypress 的控制台输出(下图),向上箭头键事件(键代码 38)显然是由 Cypress 发送的。不过,与常规按键相比,此按键触发的生命周期事件更少。
有人知道我可能做错了什么吗?或者我还能尝试什么?我对完全避免模拟按键的方法持开放态度,只要它们不涉及手动提取、递增和将值插入输入字段。
【问题讨论】:
标签: javascript testing cypress