Cypress.io:无论如何要测试特定的滚动量? [英] Cypress.io: Anyway to test for specific scroll amount?

查看:137
本文介绍了Cypress.io:无论如何要测试特定的滚动量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有某种方法可以在Cypress.io的一定范围内测试滚动量.

Wanted to know if there is some way to test for scroll amount, within a certain range with Cypress.io.

更具体地

  1. 从页面顶部开始,按下按钮
  2. 页面向下滚动到特定高度
  3. 测试滚动高度在一定范围内是否正确

我的尝试 我认为最好的方法是进行测试,然后进行测试,以确保当前的div不在手机屏幕的可见区域内.然后向下滚动,使其确实可见.

My attempt: The way I assume would be best is to test that would to test out, that a current div is not within the viewable area of phone screen. Then to scroll down so it does become viewable.

cy.get('#angular-projects').should('not.be.visible') // div #angular-projects
cy.get('#developer-projects').click() // button
cy.get('#angular-projects').should('be.visible')

想知道是否可以通过 mocha chai 完成此操作,如果没有 press 解决方案

Wanted to know whether this can be done via mocha, chai, if there is no cypress work around

推荐答案

您可以使用

You can get the window object with cy.window() and then build your assertion checking if the scrollY equals what you expect.

类似的方法应该测试滚动距离顶部是否在300到500像素之间(closeTo的第一个参数指定您想要的值,第二个参数是您要接受的误差范围):

Something like this should test if the scroll is between 300 and 500 pixels from the top (the first argument of closeTo specifies the value you want and the second is the margin of error you want to accept):

cy.window().then(($window) => {
  expect($window.scrollY).to.be.closeTo(400, 100);
});

这篇关于Cypress.io:无论如何要测试特定的滚动量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆