Kondasamy Jayaraman
Kondasamy Jayaraman

Reputation: 1864

Cypress - XML response parsing of XHR request using JQuery

I have a Cypress test which fires an XHR request, which is returning XML response. My objective is to parse the XML response and validate few nodes. I used JQuery - parseXML(), which is working as expected; but, when I try to iterate the parsed XMLs, I face some the below error,

ReferenceError: $ is not defined

which I suspect at the line -> Cypress.$(java).each(function()

Cypress test:

 it("cy.request - make an XHR request", () => {
    cy.request({
        log: true,
        url: "SOME_URL",
        auth: {
            user: Cypress.env('userName'),
            pass: Cypress.env('password')
        }
    }).then(response => {
            const xml = Cypress.$.parseXML(response.body)
            cy.log(response.body)
            console.log(xml)
            const java = xml.getElementsByTagName('java')
            Cypress.$(java).each(function() {
                cy.log($(this).find("configuration>property>name").text())
            })
            expect(response).property("status").to.equal(200);
        });
    });

Sample XML response:

<workflow-app name="Samyghjggjg" xmlns="hjkh">
    <action name="etl-69b5" retry-max="0" retry-interval="10">
        <java>
            <configuration>
                <property>
                    <name>mapred.job.queue.name1</name>
                </property>
            </configuration>
        </java>
        <java>
            <configuration>
                <property>
                    <name>mapred.job.queue.name2</name>
                </property>
            </configuration>
        </java>
    </action>
</workflow-app>

Did some analysis and noted the below details from - https://docs.cypress.io/api/utilities/$.html#Syntax

Calling Cypress.$('button') will automatically query for elements in your remote window. In other words, Cypress automatically sets the document to be whatever you’ve currently navigated to via cy.visit().

Any help would be appreciated to overcome this.

Upvotes: 4

Views: 3501

Answers (1)

soccerway
soccerway

Reputation: 11961

I assume that inside the cy.log($(this).... you might still need to add cy.log(Cypress.$(this)... in order to use the jquery in Cypress. I haven't tried yet, but its worth to give a try.

Cypress.$(java).each(function() {
            cy.log(Cypress.$(this).find("configuration>property>name").text())
        })

Upvotes: 1

Related Questions