billowy-petabyte-65450
05/03/2023, 2:14 PMgray-kilobyte-89541
05/03/2023, 2:15 PMcy.contains command)gray-kilobyte-89541
05/03/2023, 2:15 PMbillowy-petabyte-65450
05/03/2023, 2:28 PMgray-kilobyte-89541
05/03/2023, 4:36 PMfast-artist-45202
05/03/2023, 8:26 PMcy.contains('string') selects anything that contains that string. It's not an assertion, it doesn't assert or check that something contains that string before trying to find it.
The assertion is .should('contain', "string")
So, separating those two topics ahead of time is important.fast-artist-45202
05/03/2023, 8:30 PMbutton [data-id="loginButton"] class="btn.login" with the text "login"
you can use any of the following to select that login button.
cy.get('button').should('contain', "login")
This gets the object button, and asserts that one of the buttons on the page contains the text "login". It doesn't select that button, it just asserts that one of them contains that text.
cy.get('button').contains("login").should('be.visible")
This asserts that the button that contains the text "login" is visible. It selects the exact button we want as long as no other buttons contain the string "login". This does get your correct button, but may be brittle in the future.
cy.get('button.btn.login').should('be.visible")
This selects the login button by it's unique class. If that class ever changes or another element ever shares that class, it'll break text.
cy.get('[data-id="loginButton"]').should('be.visible')
This gets the exact button we want by ID, and asserts it's visible. ID is often the best option for important DOM elementsfast-artist-45202
05/03/2023, 8:33 PM[data-id="loginButton"] is so good, because it's a constant ID that you can always use and will always get the right thing. Classes can change, but that data ID should always remain the same.
Next best is using a unique class, like .btn.login, if you know that element will be the only one that ever can be selected by that class.
Lastly, cy.contains() is great, if you know that text will never change, and no other elements will contain that exact string. That's relying on a few things not to change, so it's better to go with something that you know will stay the same if there's any uncertainty.fast-artist-45202
05/03/2023, 8:34 PMfast-artist-45202
05/03/2023, 8:45 PMwonderful-match-15836
05/03/2023, 10:55 PMfindByLabelText and in some cases findByRole, but I would do most element selection with cy.contains() when that is possible. Combining some of@fast-artist-45202's examples, I would do something like this:
cy.get('button[data-id="loginButton"]', 'Log In').should('be.visible')
Because I want to find the element with a certain data attribute, but it should also use a real button element have the expected text (using judgement about whether the text matters). You can get these things "for free" with contains, in a very clean, short way. I have post coming soon about this on the Cypress blog, I'll updated when it is posted.
> If there is role attribute you can use it (without testing library).
@gray-kilobyte-89541 I think people use findByRole not specifically expecting a role attribute to be added on the element, but to also include implicit roles that elements have. For example finding by role link would find anchor elements (with no role attr) or find a div that has the role attribute added and does navigation by JS or something. So the test doesn't care about how the role is created, only that the role is present. I still prefer the app to use real HTML in almost all cases, so I barely ever use findByRole but afaik we can't replicate it with CSS selectors (maybe there's some way to get an OR in there that I'm not thinking of).
To that point though - Cypress Testing Library is useful if you are testing an app that has been written in a good accessible way and all those roles and labels etc are correctly associated with the proper elements. Often that's not the case and in many apps you will do just fine with lots of cy.get('[data-test=login']).wonderful-match-15836
05/03/2023, 10:58 PMbillowy-petabyte-65450
05/04/2023, 1:05 PMfast-artist-45202
05/04/2023, 2:45 PMgray-kilobyte-89541
05/04/2023, 4:14 PMcy.contains retries if it does not find an element with matching textfast-artist-45202
05/04/2023, 4:21 PMgray-kilobyte-89541
05/04/2023, 4:24 PMwonderful-match-15836
05/04/2023, 8:30 PMbillowy-petabyte-65450
05/05/2023, 6:48 AM