https://cypress.io logo
#help
Title
# help
n

narrow-answer-2036

11/04/2021, 8:09 PM
Copy code
ts
 cy.document().then((doc) => {
      const paragraph = doc.querySelector(nthParagraph(n))
      if (!paragraph) return
      return docs.find(doc => doc._id == paragraph.id)
  })
b

bitter-fountain-36713

11/04/2021, 9:42 PM
Are you expecting a paragraph htmlElement to return from your doc.querySelector?
n

narrow-answer-2036

11/04/2021, 10:09 PM
yeah, it returns ok but idk how to return the result from the function
because this function is a helper function
Copy code
ts
export const findNthParagraph = (docs:any[],n:number) => {

    cy.document().then((doc) => {
      const paragraph = doc.querySelector(nthParagraph(n))
      if (!paragraph) return
      const data = docs.find(doc => doc._id == paragraph.id)
      return data
  }).then((a,b) => {
    
    })



}
it should return the nthParagraph
but since it's wrapped in the cy.document() call it doesn't return anything
@User
i

important-river-75795

11/04/2021, 11:20 PM
@User Did you try wrapping the data instead of returning it
so instead of return data
do
cy.wrap(data).as("someData")
n

narrow-answer-2036

11/04/2021, 11:21 PM
Yeah just figured that out!
Copy code
ts
Cypress.Commands.add("getNthParagraphDBEntry",(docs:any[],n:number) => {
  cy.getNthParagraph(n).then(elem => {
    if (!elem) return
    const data = docs.find(doc => doc._id == elem[0].id)
    return cy.wrap(data).as("data")
  })
  return cy.get("@data")
})
The problem im having now is mapping commands across an array
Copy code
ts
  cy.wrap(children).each(elem => cy.getNthParagraphID(elem)).then(rs => {
    console.log(rs)
  })
2 Views