Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

753
Visualizações
Cypress: How to Copy/Paste single text word from string to paste later

I am new to stackoverflow so apologies if this is a bit all over the place but I can't seem to find any easy answer online for what seems like a pretty small task! :(

I am trying to Copy some data from a pop-up, that says something like "Congrats, Your Ref (REF1234) completed successfully" (just the REF1234 part) and copy it to paste into another field on the next screen... This is what I have so far, which hopefully makes sense to someone...

export function addRefToThing() {    
  cy.get('[class="ref-success"]').find(':selected').contains('REF').then(($clipboard) => {
    const copiedRef = $clipboard[0]._vClipboard.clipboardAction.text;

// <<filler steps, moving around pages>> //

    webpage.RefNo().type(copiedRef)})

This is pretty much as far as I've been able to get and pulling my hair out ... Looks like it's getting caught up at -find :selected but not actually sure it's even copying the data I want...

I'm pretty new to cypress so this is proving pretty confusing and if anyone has any good training material regarding this kind of request, that'd be awesome! Thanks in advance!

HTML:

<div class="notification-group" style="width: 300px; bottom: 0px; right: 0px;">
   <span>
      <div data-id="1" class="notification-wrapper" style="transition: all 300ms ease 0s;">
         <div class="notification-template notification success">
            <div class="notification-title">Successful</div>
            <div class="notification-content">Ref (REF123456789) created successfully</div>
         </div>
      </div>
   </span>
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try it with a regex to parse the ref.

Using /\(REF([0-9]+)\)/ you get two matches,

  • matches[0] is the whole "(REF123456789)""
  • matches[1] is the inner group "123456789"
cy.get('div.notification-content')
  .then($el => $el.text().match(/\(REF([0-9]+)\)/)[1])  // parsing step
  .then(ref => {
    webpage.RefNo().type(ref)
  })

In your function,

export function addRefToRef() {    
  cy.get('[class="ref-success"]').find(':selected').contains('REF')
    .then(($clipboard) => {
      const copiedRef = $clipboard[0]._vClipboard.clipboardAction.text;
      const innerRef = copiedRef.match(/\(REF([0-9]+)\)/)[1];

      // <<filler steps, moving around pages>> //

      webpage.RefNo().type(innerRef)
    })
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can directly save the inner text value from the notification-content div and use it later. You don't have to use the clipboard method.

//Some code that will trigger the notification
cy.get('.notification-content')
  .should('be.visible')
  .invoke('text')
  .then((text) => {
    cy.log(text) //prints Ref (REF123456789) created successfully
  })

In case if you want to save it and then use it later in the test, you can use alias .as:

//Some code that will trigger the notification
cy.get('.notification-content')
  .should('be.visible')
  .invoke('text')
  .as('notificationText')
//Some other code
cy.get('@notificationText').then((notificationText) => {
  cy.log(notificationText) //prints Ref (REF123456789) created successfully
})

Or, if you want to assert the content:

  1. Exact match:
cy.get('.notification-content')
  .should('be.visible')
  .and('have.text', 'Ref (REF123456789) created successfully')
  1. Partial Match
cy.get('.notification-content')
  .should('be.visible')
  .and('include.text', 'REF123456789')

If you just want to extract the reference number then you have to first use split to get the second text and then use slice to remove the opening and closing brackets, something like this:

//Some code that will trigger the notification      
cy.get('.notification-content').should('be.visible').invoke('text').as('notificationText')
//Some other code
cy.get('@notificationText').then((notificationText) => {
  cy.log(notificationText.split(' ')[1].slice(1,-1))  //REF123456789 
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda