英文:
expected <h1> to contain @selectedText
问题
你要做的是比较"class"为"ellipsis-2-lines text-lg text-weight-semibold"的文本与下一页的"h1"是否相同。
这个测试抛出了一个错误:期望 <h1.text-h4.text-weight-semibold.q-mb-sm> 包含 @selectedText
我认为在使用invoke函数时出现了错误。可能甚至不可能以这种方式使用它。如何修复这个错误?
英文:
What i want to do is compare that the text in class "ellipsis-2-lines text-lg text-weight-semibold" is the same as h1 on next open page
this test throws an error: expected <h1.text-h4.text-weight-semibold.q-mb-sm> to contain @selectedText
I think there is an error when using invoke function. it might not even be possible to use it that way. How to fix this error?
describe('template spec', () => {
it('visit gxpbox', () => {
cy.visit('https://gxpbox.org/')
cy.get("input[placeholder=\"Where are you going?\"]").type("Dubai")
cy.get('[class="q-item q-item-type row no-wrap q-item--clickable q-link cursor-pointer q-focusable q-hoverable items-center"]').contains('Dubai, United Arab Emirates').click()
cy.get("button[type=\"submit\"]").click();
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.should('have.length.gt', 19)
.its('length')
.then((n) => Cypress._.random(0, n - 1))
.then((k) => {
cy.log(`picked random index ${k}`)
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]').eq(k).click().invoke('text').as('selectedText');
})
cy.get('h1').should("contain", "@selectedText")
})
})
答案1
得分: 4
你可以使用这种语法与插件 bahmutov/cypress-aliases。
这方便地修改了 .should()
命令,以接受别名作为第二个参数。
import 'cypress-aliases/commands/should';
it('访问 gxpbox', () => {
cy.visit('https://gxpbox.org/')
...
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.should('have.length.gt', 19)
.its('length')
.then((n) => Cypress._.random(0, n - 1))
.then((k) => {
cy.log(`选择了随机索引 ${k}`)
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.eq(k).click().invoke('text').as('selectedText');
})
cy.get('h1').should("contain", "@selectedText")
否则,你需要添加另一个嵌套层级
import 'cypress-aliases/commands/should';
it('访问 gxpbox', () => {
cy.visit('https://gxpbox.org/')
...
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.should('have.length.gt', 19)
.its('length')
.then((n) => Cypress._.random(0, n - 1))
.then((k) => {
cy.log(`选择了随机索引 ${k}`)
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.eq(k).click().invoke('text')
.then(title => {
cy.get('h1').should("contain", title)
});
})
英文:
You can use that syntax with plugin bahmutov/cypress-aliases.
This conveniently modifies the .should()
command to accept an alias as a second parameter.
import 'cypress-aliases/commands/should'
it('visit gxpbox', () => {
cy.visit('https://gxpbox.org/')
...
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.should('have.length.gt', 19)
.its('length')
.then((n) => Cypress._.random(0, n - 1))
.then((k) => {
cy.log(`picked random index ${k}`)
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.eq(k).click().invoke('text').as('selectedText');
})
cy.get('h1').should("contain", "@selectedText")
otherwise you have to add another nesting level
import 'cypress-aliases/commands/should'
it('visit gxpbox', () => {
cy.visit('https://gxpbox.org/')
...
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.should('have.length.gt', 19)
.its('length')
.then((n) => Cypress._.random(0, n - 1))
.then((k) => {
cy.log(`picked random index ${k}`)
cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
.eq(k).click().invoke('text')
.then(title => {
cy.get('h1').should("contain", title)
});
})
答案2
得分: 1
问题在于使用.should('contains', '@selectedText')
时,第二个参数被解释为字符串而不是别名的值。相反,我们可以使用别名将其与h1
元素的字符串值进行比较。
cy.get('h1').invoke('text').then((text) => { // 获取h1元素的文本
cy.get('@selectedText').should('contain', text);
});
英文:
The issue is that when using .should('contains', '@selectedText')
, the second parameter is being interpreted as a string and not the aliased value. Instead, we can use the alias to compare it to the string value of the h1
element.
cy.get('h1').invoke('text').then((text) => { // get the text of the h1
cy.get('@selectedText').should('contain', text);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论