我的目标是在 Cypress 中编写一个测试,检查元素的宽度是否小于或等于 355px。
我有这个代码,但它只检查确切的尺寸:
cy
.get('.mat-dialog-container:visible')
.should('have.css', 'width', '355px')
Run Code Online (Sandbox Code Playgroud)
dwe*_*lle 17
可以自动化任何事情,应该是(除非这样做的预期效用是由实施和维护的成本抵销,当然),因此,我认为,自动化测试RD是一个好主意。检查容器尺寸是否是实现它的方法是一个悬而未决的问题(可以说您应该检查应该隐藏的元素、隐藏的元素和应该可见的元素是否可见,以及 UI 是否像预期的)。
唉,这里是如何实现你想要的。
我会使用 jQuery outerWidth ,这是您通常想要检查的而不是width(如果有padding或border):
cy.get(selector).invoke('outerWidth').should('be.lt', 355);
Run Code Online (Sandbox Code Playgroud)
如果你真的想断言实际计算的 css 值,你确实可以使用 jQuery csshelper(或者使用window.getComputedStyle,这并不重要):
cy.get(selector).invoke('css', 'width')
.then(str => parseInt(str)).should('be.lt', 355);
// or use jQuery.prototype.width (I'm not sure if there's any meaningful
// difference, but there might be --- better check the docs)
cy.get(selector).invoke('width').should('be.lt', 355');
Run Code Online (Sandbox Code Playgroud)
小智 8
最近刚刚尝试了这段代码,它在我的系统中完美运行。
**
cy.get('.vc_icon_element-inner')
.invoke('height').should('be.greaterThan', 47).and('be.lessThan',50)
Run Code Online (Sandbox Code Playgroud)
**
在此,Invoke函数默认从 get 参数 .vc_icon_element-inner 中获取高度 CSS,从字段中修剪px值并获取整数。
应该断言检查给定范围内的范围并显示输出。
工作截图
