jquery - 无法将 autoId 值获取到 jobId 对象中,这里 localStorage autoId 是一个字符串
问题描述
无法将 autoId 值获取到jobId
对象中,这里的 localStorageautoId
是一个字符串。有人可以在这里就问题提出建议吗?我正在尝试根据 jobId 查找表行并检查是否jobRow [columnPosition] == value
断言它......
注意:我正在尝试尽可能优化和减少代码行。
let tableDatacy = "Reports_Table";
let columnName= "Customer";
let checkCase = "should";
let value = "Main Customer";
let jobId = {};
const columnPosition = Cypress.$(`[data-cy=${tableDatacy}]`)
.find(`th:has(a:contains(${columnName}))`)
.index()
console.log("Get the silly index !: "+columnPosition)
cy.getLocalStorage('autoId').then(autoId => jobId = autoId.jobId);
let jobRow = Cypress.$(`[data-cy=${tableDatacy}] > tbody > tr :contains(${jobId})`);
if (checkCase === "should" && jobRow [columnPosition] == value){
result = "true";
expect(result).to.include("true");
} else if (checkCase === "should not" && jobRow.length == 0) {
result = "false";
expect(result).to.include("false");
}
解决方案
你有一个异步行,其余的都是同步的,所以任何依赖jobId
都应该放在.then()
.
否则,同步代码将在异步行完成之前全部运行。
const columnPosition = Cypress.$(`[data-cy=${tableDatacy}]`)
.find(`th:has(a:contains(${columnName}))`)
.index()
console.log("Get the silly index !: "+columnPosition)
// Make async call and everthing that needs the result
// must be inside the callback
cy.getLocalStorage('autoId').then(autoId => {
jobId = autoId.jobId
// let jobRow = Cypress.$(`[data-cy=${tableDatacy}] > tbody > tr :contains(${jobId})`);
let jobRow = Cypress.$(`[data-cy=${tableDatacy}] > tbody > tr:has(td:contains(${jobId}))`);
// if (checkCase === "should" && jobRow [columnPosition] == value){
const jobRowText = Cypress.$(jobRow).find('td').eq(columnPosition).text()
if (checkCase === "should" && jobRowText === value) {
result = "true";
expect(result).to.include("true");
// } else if (checkCase === "should not" && jobRow.length == 0) {
} else {
result = "false";
expect(result).to.include("false");
}
});
选择行
所以tr :contains(${jobId})
意思是“给我tr
那个包含的孩子jobId
,结果是td
。
但tr:has(td:contains(${jobId}))
意思是“给我tr
那个td
包含jobId
”。
选择行中的列并比较文本
jobRow
不是数组,它是一个元素,因此您需要使用更多的 jQuery 来提取要比较的文本。
const jobRowText = jobRow.find('td').eq(columnPosition).text()
if (checkCase === "should" && jobRowText === value) {
或结合
let jobRowText = Cypress.$(`[data-cy=${tableDatacy}] > tbody > tr:has(td:contains(${jobId}))`)
.find('td').eq(columnPosition).text()
赛普拉斯命令方式
cy.contains('[data-cy=${tableDatacy}] > tbody > tr', jobId)
.find('td').eq(columnPosition)
.invoke('text')
.then(jobRowText => {
if (checkCase === "should" && jobRowText === value) {
result = "true";
expect(result).to.include("true");
} else {
result = "false";
expect(result).to.include("false");
}
})
推荐阅读
- python - 为什么新的 Conda 环境不附带像 numpy 这样的包?
- java - 如何在 Spring Boot 分页中返回 DTO 而不是实体?
- python - python - 如何使用pygame确定是否在python中按下了一个键
- javascript - 使用模态弹出附加表 tr
- java - java.lang.NullPointerException - 应用程序不断崩溃
- php - 在 Laravel 请求中调整大小和替换图像
- javascript - 从中检索超链接值
- 在 HTML 正文标记中?
- node.js - 递归nodejs mysql查询
- c++ - 我一直收到相同数量的高级服务使用分钟数和到期金额
- mongodb - 使用 $project 更改 MongoDB 中返回的数据结构