赛普拉斯-使用JQuery对XHR请求进行XML响应解析

时间:2019-03-20 03:47:38

标签: javascript jquery html xml cypress

我有一个赛普拉斯测试,它会触发XHR请求,该请求正在返回XML响应。我的目标是解析XML响应并验证几个节点。我使用了JQuery-parseXML(),它按预期工作;但是,当我尝试迭代解析的XML时,遇到了以下错误,

ReferenceError: $ is not defined

我怀疑这行-> Cypress.$(java).each(function()

赛普拉斯测试:

 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);
        });
    });

示例XML响应:

<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>

做了一些分析,并从-https://docs.cypress.io/api/utilities/$.html#Syntax

中注意到了以下详细信息
  

调用Cypress。$('button')将自动查询您的远程窗口中的元素。换句话说,赛普拉斯会自动将文档设置为您当前通过cy.visit()导航到的文档。

任何帮助克服此问题的人。

1 个答案:

答案 0 :(得分:1)

我假设在cy.log($(this)....内,您可能仍需要添加cy.log(Cypress.$(this)...才能在赛普拉斯中使用jquery。我还没有尝试过,但是值得一试。

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