Search code examples
textradio-buttoncypress

How to verify text of a corresponding radio button in cypress?


I am trying to retrieve the text of corresponding radio buttons and want to verify the text is correct.

code

view


Solution

  • There's a few unusual things about the DOM you have, so am making some guesses.

    The text you seek is in text nodes which are not attached to elements, so the usual Cypress selectors won't work.

    Based on this question jQuery: How to select text between two closed html tags, specifically this bit

    var txtHelp = jQuery('b.page-title')[0] // get the dom object
      .nextSibling // get the text node next to it
      .textContent; // get text content
    console.log(txtHelp);
    

    I came up with this Cypress test. It assumes the xpath="1" attribute indicates the selected radio, but I could not verify that syntax.

    cy.get('input[xpath="1"]')                  
      .then($el => $el[0].nextSibling.textContent.trim())
      .should('eq', 'Option1')