I am fairly new to cypress and practicing its functionalities on Facebook app. I am having an issue on testing these following scenarios:
1.When clicked on First Name, this floating dialog should be visible. 2.Validating the text in the dialog box to be 'What's your name?'. 3.When clicked on Last Name, again a floating box with the same content "What's your name?" should be visible.
Now I am able to test the first two scenario with this.
cy.get('[name="firstname"]').click();//i)-> Click on the firstname
cy.get('[name="lastname"]').click();//ii)-> Click on the last name
cy.get('[name="firstname"]').click(); //iii)-> This will triggers the error message popup for firstname
cy.contains('What\'s your name?').should('be.visible').and('contain','What\'s your name?');// Checking the message popup should be displayed and its message is 'What's your name'
cy.get('[name="lastname"]').click();//v)-> This will trigger the error message popup for lastname
cy.contains('What\'s your name?').should('be.visible').and('contain','What\'s your name?');//vi)-> Checking for that message but not working
But in the third scenario, it is showing the error. I tried to access the element using id but it was showing not found.

I think cy.contains('What\'s your name?') is catching the message from firstName field which was activated from the initial two commmands but hidden when you navigated to the lastName field (but still remains in the page).
Specify which one you want
cy.get('div:contains(What\'s your name?)').eq(1).should('be.visible') // .and(contain) not needed
In case that quote in the text distorts the selection you may need to go with cy.get('div:contains(your name?)').eq(1).
Don't use cy.contains('What\'s your name?') it will only ever return the first one it finds.
The Facebook page is not very tester friendly, usually you can request the page elements have some test id's added so you can go directly to then.
Another way to tackle the anonymous popup stuff is just to grab the last thing added to the page, like
cy.get('body')
.children()
.last()
.should('contain', 'What\'s your name?')
.should('be.visible')