Is there a way to do assertions for texts in spec level #16511
-
this.getBreadcrumText = async (value) => {
const a = await cy.xpath("//*[contains(@data-testid,'breadcrumbs-route-Dashboards')]").text().then(txt => {
expect(txt).equal('Dashboards')
});
} In the above method i was able to expect the text within the page. IF i want to do assertions in the spec level i should be able to return the value of a to the spec file where this function is called, which is currently not happening. Ideally in the test/spec file we should not be using the element references also. Basically in spec file if i use |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Cypress is really cool and the above thing is stopping me from migrating the protractor scripts to cypress |
Beta Was this translation helpful? Give feedback.
-
Cypress commands are asynchronous but you should not be using For asserting the text of an element, see https://docs.cypress.io/faq/questions/using-cypress-faq#How-do-I-get-an-element-s-text-contents |
Beta Was this translation helpful? Give feedback.
-
Sounds like you are trying to implement a page object. We've tried to implement something similar but returning values was not well supported specifically because we are not supposed to await their promises. Our workaround was to return the HTML element in question instead of returning its text and letting the assertion happen from the Cypress side expect( await dashboardPage.getBreadcrumText()).equal('Dashboards')
// becomes
dashboardPage.getBreadcrumbElement().should('have.text', 'Dashboards') |
Beta Was this translation helpful? Give feedback.
Cypress commands are asynchronous but you should not be using
async / await
syntaxhttps://docs.cypress.io/guides/core-concepts/introduction-to-Cypress#Commands-Are-Asynchronous
For asserting the text of an element, see https://docs.cypress.io/faq/questions/using-cypress-faq#How-do-I-get-an-element-s-text-contents