javascript - 如何使用柏树将多个元素的值存储在数组列表中的一个根元素下
问题描述
对于以下类似结构的结果
<div class="results">
<div class="result">
<div class="name">x</div>
<div class="price">99</div>
</div>
<div class="result">
<div class="name">y</div>
<div class="price">88</div>
</div>
</div>
我想存储在这样的数组列表中
var resultList = [{"name":"x","amount":"99"}, {"name":"y","amount":"88"}]
在 cypress 中,我在 cypress 中有以下代码,但它打印为 null,我不知道如何解决它?
let listOfResults = [];
let singleResult = {};
cy.get('[class="results"]').each((resultItem) => {
singleResult = {};
//Retrive name
cy.wrap(resultItem)
.find('div[class$="name"]')
.invoke("text")
.then((val) => {
cy.log("singleResult value for Title:" + val);
singleResult["title"] = val;
cy.log("singleResult::" + JSON.stringify(singleResult));//prints correctly
});
//Retrive price
cy.wrap(resultItem)
.find('div[class$="amount"]')
.invoke("text")
.then((val) => {
cy.log("singleResult value for Title:" + val);
singleResult["amount"] = val;
cy.log("singleResult::" + JSON.stringify(singleResult)); //prints correctly
});
});
listOfResults.push(JSON.stringify(singleResult)); //singleResult became blank
cy.log("list:" + JSON.stringify(listOfResults));
解决方案
.then()
由于赛普拉斯命令是异步的,因此您必须在提取器之后包装代码。
let listOfResults = [];
let singleResult = {};
cy.get('[class="results"]')
.each((resultItem) => {
...
})
.then(() => {
listOfResults.push(JSON.stringify(singleResult));
cy.log("list:" + JSON.stringify(listOfResults));
})
当你调用它时,它总是在测试运行之前cy.log()
取它的值,除非它在. 如此见空。.then()
cy.log("list:" + JSON.stringify(listOfResults))
listOfResults
还有三件事:
迭代类=“结果”
cy.get('[class="results"]')
应该是cy.get('[class="result"]')
因为 class="results"
是整体的div
,但你想迭代class="result"
div(假设你的删减的 HTML 是准确的)
推入 .each()
listOfResults.push(JSON.stringify(singleResult))
应该在里面,.each()
因为你正在推动每次迭代的 singleResult。但是您还需.then()
要这样做,以确保在填充 singleResult后推送(与上述相同的问题,异步命令)。
不要字符串化
您不需要对日志进行字符串化,只需添加一个逗号。这样你就可以保持对象结构更容易/更好的向下测试使用。
const listOfResults = [];
// let singleResult = {}; // don't need this here
cy.get('[class="results"]').each((resultItem) => {
const singleResult = {}; // declare it fresh each iteration
//Retrive name
cy.wrap(resultItem)
.find('div[class$="name"]') // also 'div.name'
.invoke("text")
.then((val) => {
cy.log("singleResult value for Title:" + val);
singleResult["title"] = val;
cy.log("singleResult::", singleResult);
});
//Retrive price
cy.wrap(resultItem)
.find('div[class$="amount"]') // also 'div.amount'
.invoke("text")
.then((val) => {
cy.log("singleResult value for Title:" + val);
singleResult["amount"] = val;
cy.log("singleResult::", singleResult);
});
// Now push to list
cy.then(() => {
listOfResults.push(singleResult)
})
})
.then(() => {
cy.log("list:", listOfResults);
})
或使用别名
const listOfResults = [];
cy.get('[class="result"]').each((resultItem) => {
...
})
.then(() => {
cy.wrap(listOfResults').as('listOfResults') // save for later in the test
})
// later on
cy.get('@listOfResults')
.then(listOfResults => {
cy.log("list:", listOfResults) // logs the value of parameter
// not the outer listOfResults
})
推荐阅读
- swift - 斯威夫特 4.2/5。获取分配给字典中此键的数组中数字的“键”
- python - 在尝试在没有 sudo 权限的服务器上安装 virtualenv 时,我得到:`TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'`
- javascript - JSLint 和使用三元表达式赋值
- linux - 试图在lua中打印一张表
- curl - 调整脚本以 grep curl 的输出
- jquery - Grails 4:Grails Spring Websocket 2.5.0.M1 升级破坏了 websockets
- javascript - 如何从普通的 JavaScript 页面重定向到 Angular 6(微型站点到 Angular 6)?
- jquery - Datatables 响应式插件将 Reactstrap (Bootstraps 4) 按钮显示为对象
- python - 如何在 python 中的 sklearn 中获取 GridSearchCV 中的选定功能
- bash - OhMyZsh 未运行与别名关联的命令