小编use*_*624的帖子

Cypress - 比较数组循环内的元素文本

我有一个包含卡片元素的页面,当用户向下滚动时会加载更多卡片。我正在尝试编写一个测试来向下滚动,直到加载所有卡片元素。

我正在尝试什么:

  1. 获取页面上最后一张卡片的文本(lastCard)
  2. 向下滚动
  3. 获取刚刚加载的最后一张卡片的文本(currentCard)
  4. 如果两张卡片具有相同的文本,则结束循环。如果没有,请重复。
let stopScroll
let currentCard
let lastCard
let scrollArray = Array.from({length:100},(v,k)=>k+1)
    cy.wrap(scrollArray).each((index) => {
            if (stopScroll != true) {
                cy.get('[data-testid="card"]').last().then(($element) => {
                    lastCard = $element.text();
                })
                cy.get('[data-testid="footer"]').scrollIntoView().should('be.visible')
                cy.wait(1000)
                cy.get('[data-testid="card"]').last().then(($element) => {
                    currentCard = $element.text();
                    if (lastCard == currentCard){
                        stopScroll = true
                        cy.log(stopScroll)
                        }
                })
                .then(() => {
                    if (stopScroll == true){
                    return false
                }
              })
            }
        })
Run Code Online (Sandbox Code Playgroud)

即使 stopScroll 设置为 true,循环也会继续进行。一切似乎都达到了“.then(()”的程度。我不确定这是否是正确的方法。

loops if-statement cypress

3
推荐指数
1
解决办法
45
查看次数

标签 统计

cypress ×1

if-statement ×1

loops ×1