【问题标题】:Cypress_ Clicking an element until you don't find a text valueCypress_单击一个元素,直到找不到文本值
【发布时间】:2021-05-22 15:41:45
【问题描述】:

我是赛普拉斯的新手。有一个功能可以检查在日历中创建的一些事件 查看。

如果我选择了每月活动,那么我将在日历中获得 12 个活动 (即;获得文本“我的事件”的#table span.fc-title 的计数将是 12。 并且每次我需要单击日历中的下一个按钮以转到下个月视图)。

我希望在 cypress 中编写一个方法来验证此功能。

这是逻辑:

文本“我的活动”在当前月份是否可见(执行 12 次){

If yes, add 1 to the constant count and click on the next button and navigate to the next month and do the step1
If the text is not found, exit the loop.}

我刚刚设法编写了以下代码。但是任何人都可以请查看这个和 帮我完成这个?我想循环直到找不到文本“我的事件”

常量计数=1; 常量 i = 12;

cy.get('#table  span.tc-title')
  .each(($span, i) => {
    const text = Cypress.$($span).text()
    if (text.includes('my event')) {

    const count= count+1
    cy.get('#table button.tc-next-button').click
    
     //if the text is not there I need to exit the loop;
     // I need the value of the count of events too;
      return false;
    }
  })

【问题讨论】:

    标签: loops text calendar cypress each


    【解决方案1】:

    我自己找到了一个可行的解决方案。所以也将代码结构发布给其他人:

      for (let i=0; i < 12; i++){
       const monthCount = parseInt(12 - monthNum); // This is to find the current 
      month and navigate through 
      the remaining months in the current year
    for (let i=0; i < monthCount; i++){
      // You can use invoke('text') to get the desired text
      cy.get(#table).find('span-title').invoke('text')
     .then((text) => {
        if (text.includes('My events')) {
      
            cy.get('#table  button.next-button').should('be.visible').click();
            cy.wait(2000);
            cy.log(i);
       }
     })
      }
    

    }

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 2020-03-01
      • 2020-07-04
      • 2023-02-17
      • 2020-10-01
      相关资源
      最近更新 更多