You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress报错:Max -14876746378应大于min 0,求解决方案

Cypress测试报错:Max -14876746378 should be greater than min 0 解决方案

问题详情

我是编程及自动化测试新手,使用Cypress执行测试时反复遇到报错:Max -14876746378 should be greater than min 0。用@faker-js/faker生成随机日期之前正常,现在测试执行到勾选复选框环节时出现该错误,测试代码如下:

beforeEach(() => {
        cy.visit("xxx")
        cy.get("input[type='text']").type("Test@admin.com")
        cy.get("input[type='password']").type("Test2023!")
        cy.get("button[type='submit']").click()
        cy.xpath("//span[normalize-space()='Cycle Calendar']").click()
        //cy.wait(2000)
        cy.xpath("//span[normalize-space()='Add New Cycle Calendar']").click()
        //cy.wait(1000)
    })

    it("Receiving from the mobiler", () => {
        //generate random start and end dates for the date picker
        const startDate = faker.date.future(    )
        const endDate = faker.date.between(startDate, faker.date.future())

        //Format date to yyyy-mm-dd
        const formattedStartDate = dayjs(startDate).format('YYYY-MM-DD')
        const formattedEndDate = dayjs(endDate).format('YYYY-MM-DD')

        //click start date
        cy.get("input[placeholder='Start date']").click({force: true}).type(formattedStartDate)
        cy.get("input[placeholder='End date']").click({force: true}).type(formattedEndDate)

        //checkboxes MON-SAT
        cy.xpath("(//input[@type='checkbox'])[1]").check({force: true})
        cy.xpath("(//input[@type='checkbox'])[2]").check({force: true})
        cy.xpath("(//input[@type='checkbox'])[3]").check({force: true})
        cy.xpath("(//input[@type='checkbox'])[4]").check({force: true})
        cy.xpath("(//input[@type='checkbox'])[5]").check({force: true})
        cy.xpath("(//input[@type='checkbox'])[6]").check({force: true})
        cy.xpath("(//input[@type='checkbox'])[7]").check({force: true})

        const validDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        const validDaysCount = countValidDaysInRange(formattedStartDate, formattedEndDate, validDays)
        cy.log('Number of valid days between ${formattedStartDate} and ${formattedEndDate}: ${validDaysCount}')
        //cy.get("button[type='submit']").click()
        //cy.xpath("//span[contains(text(),'Continue')]").click()

        function countValidDaysInRange(formattedStartDate, formattedEndDate, valid) {
            const start = dayjs(formattedStartDate)
            const end = dayjs(formattedEndDate)
            let count = 0
        
            for (let current = start; current <= end; current.add(1,'day')) {
                if (validDays.includes(current.format('dddd'))){
                    count++
                }
            }}

    })

错误原因分析

  1. 无限循环引发数值溢出:countValidDaysInRange函数里的循环逻辑错误,current.add(1,'day')没有重新赋值给current,导致current一直等于起始日期,循环条件永远成立,触发无限循环后引发数值溢出,出现报错。
  2. 函数无返回值:countValidDaysInRange没返回计算好的count,导致validDaysCount是undefined,后续日志输出无效。
  3. 模板字符串用错:cy.log里用单引号包裹模板字符串,没法解析变量,日志内容不对。
  4. faker日期参数缺失:faker.date.future()没传年限参数,可能生成不符合预期的日期(不是当前报错直接原因,但建议规范使用)。

修复后的代码

beforeEach(() => {
    cy.visit("xxx")
    cy.get("input[type='text']").type("Test@admin.com")
    cy.get("input[type='password']").type("Test2023!")
    cy.get("button[type='submit']").click()
    cy.xpath("//span[normalize-space()='Cycle Calendar']").click()
    cy.xpath("//span[normalize-space()='Add New Cycle Calendar']").click()
})

it("Receiving from the mobiler", () => {
    // 生成未来1年内的随机起止日期
    const startDate = faker.date.future(1)
    const endDate = faker.date.between(startDate, faker.date.future(1))

    // 格式化日期为yyyy-mm-dd格式
    const formattedStartDate = dayjs(startDate).format('YYYY-MM-DD')
    const formattedEndDate = dayjs(endDate).format('YYYY-MM-DD')

    // 填写起止日期
    cy.get("input[placeholder='Start date']").click({force: true}).type(formattedStartDate)
    cy.get("input[placeholder='End date']").click({force: true}).type(formattedEndDate)

    // 勾选所有星期复选框
    cy.xpath("(//input[@type='checkbox'])[1]").check({force: true})
    cy.xpath("(//input[@type='checkbox'])[2]").check({force: true})
    cy.xpath("(//input[@type='checkbox'])[3]").check({force: true})
    cy.xpath("(//input[@type='checkbox'])[4]").check({force: true})
    cy.xpath("(//input[@type='checkbox'])[5]").check({force: true})
    cy.xpath("(//input[@type='checkbox'])[6]").check({force: true})
    cy.xpath("(//input[@type='checkbox'])[7]").check({force: true})

    const validDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    const validDaysCount = countValidDaysInRange(formattedStartDate, formattedEndDate, validDays)
    // 用反引号包裹模板字符串,正确解析变量
    cy.log(`Number of valid days between ${formattedStartDate} and ${formattedEndDate}: ${validDaysCount}`)
    // cy.get("button[type='submit']").click()
    // cy.xpath("//span[contains(text(),'Continue')]").click()

    function countValidDaysInRange(formattedStartDate, formattedEndDate, validDays) {
        const start = dayjs(formattedStartDate)
        const end = dayjs(formattedEndDate)
        let count = 0

        // 更新current变量,避免无限循环;用dayjs自带方法做日期比较
        for (let current = start; current.isBefore(end) || current.isSame(end); current = current.add(1,'day')) {
            if (validDays.includes(current.format('dddd'))){
                count++
            }
        }
        // 返回计算结果
        return count
    }
})

修复说明

  • 解决无限循环:把current.add(1,'day')改成current = current.add(1,'day'),确保每次循环current都更新为下一天;同时用current.isBefore(end) || current.isSame(end)替代current <= end,符合dayjs的日期比较规范。
  • 添加返回值:在countValidDaysInRange函数末尾加return count,保证能拿到有效计数。
  • 修正模板字符串:把cy.log里的单引号换成反引号,正确渲染变量内容。
  • 规范faker调用:给faker.date.future()传1,指定生成未来1年内的日期,避免异常日期。

内容的提问来源于stack exchange,提问作者Wigii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:44:55