Copying and Pasting text in Cypress
Along with testing payment processing, I have to validate refunds. In order to do this, I needed Cypress to be able to tell what the payment amount was and use this. Since this isn’t always the same value, I needed to figure out how to get Cypress to get this from the website. Here’s how I did that:
Right-click on the dollar amount and select Inspect > Copy JS path. Example:
document.querySelector("body > div.sc-imABML.erDmXz.sc-bfYoXt.bsPcTV__refund-modal > div.MuiPaper-root.MuiCard-root.sc-bMVAic.jnwpjJ.MuiPaper-elevation1.MuiPaper-rounded > div > form > div > div.MuiGrid-root.sc-imABML.erDmXz.sc-bfYoXt.bsPcTV__original-payment-method.MuiGrid-item.MuiGrid-grid-xs-12 > p:nth-child(2) > strong")
Grab just the end: p:nth-child(2) > strong and add to the cy.get()
cy.get('p:nth-child(2) > strong')
.then(($temp)=>{
const txt = $temp.text()
Then call it and “type” it into the field:
${txt}
) })
Example from my script:
// Grabs the dollar amount and types it into the appropriate field:
cy.get('p:nth-child(2) > strong') // $ of Original Total paid
.then(($temp)=>{
const txt = $temp.text()
cy.get('[id="money-field"]').type(${txt}
) }) //REFUND AMOUNT field