https://cypress.io logo
Join Discord
Powered by
# help
  • f

    flat-electrician-52949

    05/20/2021, 7:58 PM
    but I think I just found a better way
  • s

    stocky-dream-36427

    05/20/2021, 7:58 PM
    Cypress.Commands.add is what you want
  • s

    stocky-dream-36427

    05/20/2021, 7:58 PM
    then you can add it to the chain
  • s

    stocky-dream-36427

    05/20/2021, 7:59 PM
    Kk
  • f

    flat-electrician-52949

    05/20/2021, 7:59 PM
    arhh.. looks also quite useful
  • f

    flat-electrician-52949

    05/20/2021, 8:00 PM
    My new way is using the
    beforeEach
    and
    afterEach
    where the latter is doing the assertion
  • f

    flat-electrician-52949

    05/20/2021, 8:01 PM
    I know the
    afterEach
    is commonly used to clean up
  • f

    flat-electrician-52949

    05/20/2021, 8:02 PM
    but I have a
    let expectedCartState
    outside that I just assign at the end of each
    it()
  • f

    flat-electrician-52949

    05/20/2021, 8:20 PM
    every time I solve something I always step up the challenge 🧑‍🚒 Got some divs each with a button in. The buttons have the same text and I would like to control which button I grap.
    Copy code
    ts
    
    <div>
     <div>
       ....
        <div>
          <h2>This text is different from in each component</h2>
        </div>
       ...
     </div>
      <div>
       ....
        <div>
          <button>SAME TEXT FOR EACH BUTTON</button>
        </div>
       ...
     </div>
    </div>
  • f

    flat-electrician-52949

    05/20/2021, 8:20 PM
    I just tried
    Copy code
    ts
    cy.get('div')
            .contains(dishOne.title)
            .within(() => cy.get('button').contains('LÄGG TILL'))
            .as('ADD DISH ONE')
  • f

    flat-electrician-52949

    05/20/2021, 8:20 PM
    but that's graping the nearst
    div
  • f

    flat-electrician-52949

    05/20/2021, 8:22 PM
    what's the best way to traverse out?
  • f

    flat-electrician-52949

    05/20/2021, 8:24 PM
    is it to get the
    h2
    then use
    parents()
    to get to outer
    div
    and then go in to the
    button
    ?
  • f

    flat-electrician-52949

    05/20/2021, 8:25 PM
    or is there already a cypress command for this type work?
  • f

    flat-electrician-52949

    05/20/2021, 8:33 PM
    I see that I can perhaps grap the outer by partial matching the class name for that (using css modules so can't do a full match) and then use a
    .find
    tried (not working):
    cy.get('*[class^="DishCard"]').contains(dishOne.title).find('button').as('ADD DISH ONE')
  • s

    stocky-dream-36427

    05/20/2021, 8:40 PM
    I would personally just modify the node to have a data-testid or data-cy selector
  • s

    stocky-dream-36427

    05/20/2021, 8:41 PM
    Using siblings and complex selectors makes your tests really brittle
  • f

    flat-electrician-52949

    05/20/2021, 8:57 PM
    Copy code
    ts
    beforeEach(() => {
          const CartState = () => {
            const { cartItems } = useCart()
            return <ContextConsumer name="Cart" state={cartItems} />
          }
    
          mount(
            <>
              <GlobalStyle />
              <CartProvider>
                <MenuProvider>
                  <SoldOutProvider>
                    <SideBySide>
                      <DishCard id={dishOne.id} data-testid="dishOne" />
                      <DishCard id={dishTwo.id} />
                    </SideBySide>
                    <CartState />
                  </SoldOutProvider>
                </MenuProvider>
              </CartProvider>
            </>
          )
    
          cy.findByTestId('dishOne').find('button').as('ADD DISH ONE')
        })
  • f

    flat-electrician-52949

    05/20/2021, 8:58 PM
    I like the idea - just not working
  • f

    flat-electrician-52949

    05/20/2021, 9:00 PM
    cy.get('[data-testid="dishOne"]').find('button').as('ADD DISH ONE')
    - not working either
  • f

    flat-electrician-52949

    05/20/2021, 9:01 PM
    cy.get('[data-testid="dishOne"]').within(() => cy.get('button').as('ADD DISH ONE'))
    not working
  • u

    user

    05/20/2021, 9:03 PM
    Can your
    data-testid
    be dynamically set on the button itself?
  • f

    flat-electrician-52949

    05/20/2021, 9:04 PM
    arhh.. found the issue
  • f

    flat-electrician-52949

    05/20/2021, 9:04 PM
    forgot to add the prop that puts in the
    data-testid
  • f

    flat-electrician-52949

    05/20/2021, 9:05 PM
    thanks for the help!
  • f

    flat-electrician-52949

    05/20/2021, 9:07 PM
    just a quick note: it's only working for the
    within()
    attempt and not for the
    find()
  • s

    stocky-dream-36427

    05/20/2021, 9:34 PM
  • f

    flat-electrician-52949

    05/21/2021, 6:48 AM
    It should be the same, right?
  • s

    stocky-dream-36427

    05/21/2021, 6:48 AM
    I suppose I need the example in its entirety. It's a bit hard to read what you mean.
  • f

    flat-electrician-52949

    05/21/2021, 6:53 AM
    I can make a quick example repo - is there a boilerplate repo that is bootstrapped with cypress component testing?
1...121314...252Latest