'Get first 3 words of a phrase

Can someone advise how i can check that a table row has a specific text. i tried to do it with a command as such:

Cypress.Commands.add('start', (text) => {
   cy.contains('td',text)
})

Then i do cy.start('Specify a selector') in my test

My scenario is i am searching a table using first 3 words of a phrase..It has to be first 3 and if not found it should fail. if it has the first two text correct it should not find it..It has to be verified by the first 3 text itself. Assuming that i have the text

Specify a selector to filter DOM elements containing the text

in my test i am writing:

cy.start('td','Specify a selector') i see the test passing but if i put "elements" it also passes

How can i achieve this please.

Thank you



Solution 1:[1]

You could use a regex instead of a simple string, and that should accomplish what you'd like. The ^ symbol signifies that this should be the beginning of the matched string.

Cypress.Commands.add('start', (text) => {
   cy.contains('td', RegExp(`^${text}`))
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 agoff