简体   繁体   English

赛普拉斯主题中的“获取”按钮

[英]Cypress get button within subject

I have a case where I need to test buttons that are generated through activity on the site (making a game creates a button on their homepage). 我有一种情况,我需要测试通过网站上的活动生成的按钮(制作游戏会在其主页上创建一个按钮)。 These buttons should take the user to their game portal. 这些按钮应将用户带到他们的游戏门户。

The buttons are contained within .sidebar--button subject, they are listed from top to bottom. 这些按钮包含在.sidebar--button主题中,从上到下列出。

First I attempted to click the nth button in .sidebar--button by using: 首先,我尝试使用以下命令单击.sidebar--button中的第n个按钮:

    it('selects the 3rd game in the sidebar', () => {
    cy.get('.sidebar--button').eq(2).click()
    cy.wait(1000)
    cy.url()
        .should('include', 'portal')

This failed as I cannot use .eq to click an element. 这失败了,因为我无法使用.eq来单击元素。

Then I tried to use .within to select a single generated button within the .sidebar--button using: 然后我尝试使用.within在.sidebar--button中使用以下命令选择一个生成的按钮:

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')

This failed as well. 这也失败了。

How can I cy.get('nth button').click() with only the buttons contained within .sidebar--button ? 如何仅使用.sidebar--button中包含的按钮cy.get('nth button')。click()?

You can use for that css selectors. 您可以使用该CSS选择器。 For example if you have scenario like this .parent>.child*4>button you can access to third element by .parent:nth-child(3) button . 例如,如果您具有类似.parent>.child*4>button ,则可以通过.parent:nth-child(3) button访问第三个元素。 for more exact css selector i have to know the html structure. 对于更确切的CSS选择器,我必须知道html结构。

----- Edited ----- -----编辑-----

Working selector for this case .sidebar--button:nth-child(3) .button . 这种情况下的工作选择器.sidebar--button:nth-child(3) .button

Does this button has different text? 此按钮的文字是否不同? if yes, then use 如果是,则使用

cy.get('.sidebar--button').contains('whatever the text is').click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM