javascripthtmlcypressweb-api-testing

Cypress: How to get elements from a variable with HTML?


I'm currently working on API testing. The API that I'm testing is returning an HTML body. How do I get an element from the response body? If I visit the link, it returns an iframe and I cannot access it properly inside cy.origin.

How to get the https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d or the XudSov7Y6JDm ? I've been going through this for hours but can't seem to find a solution :/

var message = `<html>

<body style="background-color:#f5f5f5;font-family:'Open Sans',sans-serif;padding:24px 0;">
    <div style="margin:0 auto 24px auto;text-align:center">

        <img width="130" src="cid:quipper_logo" alt="School LINK" />

  </div>
        <hr style="border:none;" />
        <div style="margin:12px auto;max-width:540px;background-color:white;border:1px #dcdcdc solid;padding:20px;">
            <p><strong>** This email is automatically generated. Please do not reply. **</strong></p>


            <p>Your Excel spreadsheet for is ready to download.</p>



            <p>Please note: you will need to enter the supplied username and password to download the file.</p>




            <p><strong>Download:</strong> <a
                    href="https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d">https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d</a>
            </p>


            <p>Username: test@test.com</p>
            <p>Password: XudSov7Y6JDm</p>



        </div>
</body>

</html>`

Solution

  • You can use the regex in python:

    import re
    
    message = # insert you HTML Body 
    link = re.search(r'https://\S+', message).group(0)
    password = re.search(r'Password: (\S+)', message).group(1)
    print(download_link)
    print(password)