#jquery #cypress #ui-automation #chai
#jquery #cypress #пользовательский интерфейс-автоматизация #чай
Вопрос:
Я хочу выполнить поиск яблока и утверждать, что если яблоко отображается хотя бы в одном столбце в строке.
cy.get('[row-id] [role="grid"]').each((item) => {
expect(item.text()).to.have.text("Keyword")
Журнал сбоев
expected { Object (userInvocationStack, specWindow, ...) } to have text Apple
You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object.
The chai-jQuery assertion you used was:
> text
The invalid subject you asserted on was:
> SeanLandsmanAppleCarrot
To use chai-jQuery assertions your subject must be valid.
This can sometimes happen if a previous assertion changed the subject.
метод .text() возвращает значение в виде одной строки, и поэтому утверждение не выполняется. Есть ли способ получить значение «Sean Landsman Apple Carrot» вместо «SeanLandsmanAppleCarrot»
Ответ №1:
«Я хочу выполнить поиск Яблока и утверждать, что если Яблоко появится хотя бы в одном столбце в строке»
Столбец в строке — это «ячейка», в DOM это span
с классом «ag-cell», так что это
cy.contains('span.ag-cell', 'Apple'); // Apple anywhere
добьется успеха, если где-нибудь найдется «Яблоко».
Немного сложно сказать, действительно ли это то, чего вы хотите.
Возможно, вы хотите проверить, есть ли «Яблоко» в строке «Шон Ландсман», и в этом случае
cy.contains('span.ag-cell', 'Sean Landsman') // find the name
.parent('div.ag-row') // select the row
.contains('Apple'); // Apple in that row
Но что, если вы хотите проверить весь текст строки целиком?
cy.contains('span.ag-cell', 'Sean Landsman') // find the name
.parent('div.ag-row') // select the row
.find('span.ag-cell') // all the cells in that row
.then(cells => {
return [...cells].map(cell => cell.text()) // map them to their texts
})
.should('deep.eq', ['Sean Landsman', 'Apple', 'Carrot']);
// or might be ['', 'Sean Landsman', 'Apple', 'Carrot'] since there's a checkbox