Cypress - how to find by text content?

后端 未结 3 1841
面向向阳花
面向向阳花 2021-02-12 17:40

In Cypress, I want to select a button from a group of buttons based on its text-content. How can I do it? Here is my approach:

export const getCustomerButton = (         


        
3条回答
  •  無奈伤痛
    2021-02-12 18:23

    Another option that's not mentioned in the previous answers here.

    Use testing-library/cypress-testing-library

    After the installation, just import it in cypress' commands.js:

    import '@testing-library/cypress/add-commands'
    

    And in your tests

    cy.findAllByText("Jackie Chan").click();
    cy.findByText("Button Text").should("exist");
    cy.findByText("Non-existing Button Text").should("not.exist");
    cy.findByLabelText("Label text", { timeout: 7000 }).should("exist");
    cy.get("form").within(() => {
      cy.findByText("Button Text").should("exist");
    });
    cy.get("form").then((subject) => {
      cy.findByText("Button Text", { container: subject }).should("exist");
    });
    
    

    This is pretty straightforward and easy to use. We use this in our production site along with react testing library. Highly recommend :)

提交回复
热议问题