嗨,我是柏树的新手。请帮助:为什么我不能将列表文本生成到数组?我尝试使用invoke,但它导致文本不在数组中。我已经尝试过以下方法。
it('can sort by aToz', () => {
cy.get('h4.card-title')
.then($titles => {
const title = $titles
.toArray()
.map($el => $el.innerText)
// assertion comes from chai-sorted
expect(title).to.be.sorted()
})
})
it('checks sort by A to Z', () => {
cy.get('#sort').select('A to Z')
cy.wait(4000)
function getTableData() {
let cellContents = [];
return new Cypress.Promise(resolve => {
cy.get('h4.card-title')
.each(($el, $index) => {
cellContents.push($el.text());
}) .then(() => resolve(cellContents))
})
}
getTableData().then(cellContents => {
expect(cellContents).to.be.sorted()
})
但是两者都会产生这个结果:
(12) [h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title, h4.card-title]
0: h4.card-title
1: h4.card-title
2: h4.card-title
3: h4.card-title
4: h4.card-title
5: h4.card-title
6: h4.card-title
7: h4.card-title
8: h4.card-title
9: h4.card-title
10: h4.card-title
11: h4.card-title
length: 12
__proto__: Array(0)
它正确记录。但是为什么会有这个错误?
Invalid Chai property: sorted
答案 0 :(得分:0)
赛普拉斯没有隐式提供chai-sorted依赖项。
要使用chai-sorted
,您需要:
npm i chai-sorted
在support/index.js
中添加:
const chaiSorted = require('chai-sorted');
chai.use(chaiSorted);
“ chai-sorted”已准备就绪,可以使用:expect(<list of string>).to.be.sorted()