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

267
Visualizações
Javascript: Cypress: select sample with not contain value

I want to select a random value from a dropdown, with the restriction that a specific value should not be selected.

    const initialOrderReason = cy.xpath("//tbody/tr[1]/td[4]");
    tkm
      .dropdownOptions()
      .should("be.visible")
      .then((options) => {
        cy.get(Cypress._.sample(options))
          .not.contain(initialOrderReason)
      });

how is it possible?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There's a lot relevant code that you have not shown, including the HTML but here's my solution based on what you've given

cy.xpath("//tbody/tr[1]/td[4]") should be accessed it via .then() instead of the returned value.

And for all elements extract the text of the elements.

cy.xpath('//tbody/tr[1]/td[4]')
  .invoke('text')                   // extract text
  .then(initial => {

  tkm.dropdownOptions().should("be.visible")
    .then((options) => {
      const texts = [...options].map(option => option.innerText)    // extract texts

      const notInitial = texts.filter(text => text !== initial)     // filter out initial

      const random = Cypress._.sample(notInitial)   // random other value
      
      // Assuming "//tbody/tr[1]/td[4]" gives a <select> element
      cy.xpath('//tbody/tr[1]/td[4]').select(random)

    });

  cy.xpath('//tbody/tr[1]/td[4]')       // requery the dropdown
    .should("not.have.text", initial);  // to allow DOM to change 
})
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