我有一个div,其中有另一个div,我想检查div内部文本的相等性。我已经想出如何使用invoke('text')函数来做到这一点,但是我想知道这是否是最好的方法。所以我的问题是:如何使用cypress检查元素的内部文本是否相等?
it('the channel name should contain be Anakin Skywaler', () => {
//This works but can we be more specific with our selector
cy.get("[data-test-id='Skywalker,Anakin']").should('contain', 'Skywalker,Anakin');
})
it('the channel name should equal Skywalker,Anakin', () => {
cy.get("[data-test-id='Skywalker,Anakin']").find('.channel-name').invoke('text').then((text) => {
expect(text.trim()).equal('Skywalker,Anakin')
});
});
请忽略《星球大战参考》!
答案 0 :(得分:6)
我认为您可以简化这一点。
假设您有这样的HTML:
<div data-test-id="Skywalker,Anakin">
<div class=".channel-name">Skywalker,Anakin</div>
</div>
您可以这样写断言:
cy.get('[data-test-id="Skywalker,Anakin"]')
.should('have.text', 'Skywalker,Anakin');
这对我来说是成功的,如果我将HTML修改为 Skywalker,Anakin 1 ,它会失败,正如您所料。赛普拉斯使用have.text查看呈现的内容,因此不必担心任何标记,而只需查看结果即可。
这不适用于修剪。您将需要添加回调以进行修整。
cy.get('[data-test-id="Skywalker,Anakin"]')
.should(($div) => {
expect($div.text().trim()).equal('Skywalker,Anakin');
});
答案 1 :(得分:1)
您可以检查div内部是否包含字符串:
cy.get("[data-test-id='Skywalker,Anakin']").contains('Skywalker,Anakin');
或者,如果您需要确保div仅包含 指定的文本,而没有其他内容,则可以在此额外的断言上进行标记:
cy.get("[data-test-id='Skywalker,Anakin']").contains('Skywalker,Anakin').should((elem) => {
expect(elem.text()).to.equal('Skywalker,Anakin');
});
说明:
// Get the data
cy.get("[data-test-id='Skywalker,Anakin']")
// Get the child or children of the previous command that
// contain the text - this command fails if no child
// element is found containing the given text
.contains('Skywalker,Anakin');
// These two lines are explained above
cy.get("[data-test-id='Skywalker,Anakin']")
.contains('Skywalker,Anakin')
// Like a .then(), except the contents are retried until
// all contained assertions are successful or until the
// command times out
.should((elem) => {
// Expect the element's text to exactly equal the
// string, not just contain it
expect(elem.text()).to.equal('Skywalker,Anakin');
});
答案 2 :(得分:0)
我认为当前这是最好的选择,因为它不检查是否包含。我希望能用较短的代码来做到这一点。
it('the channel name should equal Skywalker,Anakin', () => {
cy.get("[data-test-id='Skywalker,Anakin']").find('.channel-name').invoke('text').then((text) => {
expect(text.trim()).equal('Skywalker,Anakin')
});
});
答案 3 :(得分:0)
以下是如何检查元素中字符串的完全匹配或部分匹配的方法:
<ion-button expand="block" (click)="getDataStandard()">getDataStandard</ion-button>
//matches exact text of result string
cy.get("[data-test-id='Skywalker,Anakin']").should('have.text', 'Skywalker,Anakin');
//matches partial text of result string
cy.get("[data-test-id='Skywalker,Anakin']")
.text()
.then(value => {
cy.log("Text value is :", value);
expect(value).to.include('Anakin');
});
文件中的text()
定义如下:
command.js
答案 4 :(得分:0)
不敢相信我没有看到神奇的柏树 .should
匹配之一。我也使用 typescript cypress,它提供了很好的查找/智能感知。
cy.get("[data-test-id='Skywalker,Anakin']")
.should("have.text", "Skywalker,Anakin")
.should("have.attr", "data-test-id","Skywalker,Anakin'")
commands.ts
Cypress.Commands.add(
'shouldHaveTrimmedText',
{
prevSubject: true,
},
(subject, equalTo) => {
if (isNaN(equalTo)) {
expect(subject.text().trim().replace(/ +/g, ' ')).to.eq(equalTo);
} else {
expect(parseInt(subject.text())).to.eq(equalTo);
}
return subject;
},
);
cypress/support/index.d.ts
Cypress.Commands.add(
'shouldHaveTrimmedText',
{
prevSubject: true,
},
(subject, equalTo) => {
if (isNaN(equalTo)) {
// removes double spaces and ending spaces, does not remove special characters such as tabs or \n
expect(subject.text().trim().replace(/ +/g, ' ')).to.eq(equalTo);
} else {
expect(parseInt(subject.text())).to.eq(equalTo);
}
return subject;
},
);
tsconfig
{
"types": ["cypress","./cypress/support"]
// or "typeRoots": ... but not both
}
cy.get("[data-test-id='Skywalker,Anakin']")
.shouldHaveTrimmedText("Skywalker,Anakin")