我有一个案例,我需要测试通过网站上的活动生成的按钮(让游戏在他们的主页上创建一个按钮)。这些按钮应该将用户带到他们的游戏门户。
按钮包含在.sidebar - 按钮主题中,它们从上到下列出。
首先,我尝试使用以下方法单击.sidebar - 按钮中的第n个按钮:
it('selects the 3rd game in the sidebar', () => {
cy.get('.sidebar--button').eq(2).click()
cy.wait(1000)
cy.url()
.should('include', 'portal')
这失败了,因为我不能使用.eq来点击一个元素。
然后我尝试使用.within在.sidebar - 按钮中选择一个生成的按钮,使用:
it('deletes the current game', () => {
cy.get('.sidebar--button').should('have.length', 1)
cy.get('.sidebar--button').within((".sidebar--button") => {
cy.get('.button').click()
})
cy.wait(1000)
cy.url()
.should('include', 'portal')
这也失败了。
我怎么能cy.get('nth button')。点击()只包含.sidebar中的按钮 - 按钮?
您可以使用该css选择器。例如,如果您有像.parent>.child*4>button
这样的场景,您可以通过.parent:nth-child(3) button
访问第三个元素。对于更精确的css选择器我必须知道html结构。
-----编辑-----
这个案例的工作选择器.sidebar--button:nth-child(3) .button
。
这个按钮有不同的文字吗?如果是,那么使用
cy.get('.sidebar--button').contains('whatever the text is').click()