【问题标题】:Click on picture using Cypress使用赛普拉斯点击图片
【发布时间】:2021-03-11 12:43:50
【问题描述】:

如何使用赛普拉斯点击收藏按钮?我试过了:

cy.contains('img', {matchCase: false}).click({force: true}); 
cy.get('[alt="Image"]').click();
cy.get('[src="Images/outline_star_border_black_18dp.png"]').click({force: true});

【问题讨论】:

    标签: testing cypress


    【解决方案1】:

    我会通过获取工具栏并深入了解它,

    cy.get('.topbar')  // get the containing toolbar
      .children()      // all the children with it
      .eq(1)           // take the second one
      .find('img')     // all the icons
      .eq(0)           // take the first one (NOTE they are reverse order to display)
      .click()
    

    或者使用.find()和部分匹配的源字符串在工具栏中搜索

    cy.get('.topbar')  // get the containing toolbar
      .find('img[src*="outline_star"]')     // *= gives a partial match on src
      .click()
    

    【讨论】:

      【解决方案2】:

      嗯,这样的事情会有所帮助。首先通过 cy.xpath()cy.get() 找到元素,然后过滤更像具有 src 标签的元素,然后如果 >src 具有包含 outline_star_border_black 的 url,然后单击元素简单:)

      const elem= cy.xpath('div[class="main"]>div:nth-child(2)>div>
      img:nth-child(1)').should('have.attr', 'src')
      
      if (elem.should('contain', 'outline_star_border_black')) {
      elem.click()
      }
      

      【讨论】:

      • const elem= cy.get('div[class="col-lg-2 col-md-2"]').should('alt="Image", Images/outline_star_border_black_18dp.png ') if (elem.should('contain', 'outline_star_border_black')) { elem.click() }
      • 我们不能像你那样在 .should() 中使用我们选择的参数。阅读此docs.cypress.io/api/commands/should.html#Syntax
      • 写这个 const elem= cy.get('div[class="col-lg-2 col-md-2"]>img').should('have.attr', 'alt ') 如果你想断言“alt”属性
      • 写这个 const elem= cy.get('div[class="col-lg-2 col-md-2"]>img').should('have.attr', 'alt ') 如果你想断言“alt”属性但问题是 alt='image 在 div 标签下不止一个,并且类名看起来也不是很独特。试试这个:cy.get('.topbar').children().eq(1).find('img').eq(0).click()
      猜你喜欢
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多