Another option, and my preferred way is to use find I would also recommend not using then or or other nested function like each if you don't need it. And even though you can use jquery selectors like nth-child(), using the eq() makes it a bit more readable (IMHO). Also nth-child index is not 0-based but 1-based, so nth-child(2) returns the 2nd column, not the 3rd.
for example, this will give you the 8th row and 3rd column
cy.get('[name="courses"] > tbody >tr')
.eq(7)
.find('td')
.eq(2)
if I repeat anything more than twice in a test, I tend to write helper functions in the spec.
// define somewhere at the beginning of the file (or add to commands.js)
const getTableCell(r,c) => cy.get('[name="courses"] > tbody >tr')
.eq(r)
.find('td')
.eq(c)
describe('♂️', () => {
it('', () => {
getTableCell(0,0).should('have.text', 'Instructor')
getTableCell(0,1).should('have.text', 'Course')
getTableCell(0,2).should('have.text', 'Price')
getTableCell(1,0).should('have.text', 'Rahul Shetty')
getTableCell(1,1).should('contain.text', 'Appium')
getTableCell(1,2).should('have.text', '25')
// etc...
})
})
in some cases, depending on your html markup, the text might be padded with white space. In that case you might want to use contain.text instead of have.text but that could also cause issues, eg. 125 would match contain.text("25").
another option
cy.get('[name="courses"] > tbody >tr') // select all rows
.contains("Python") // find 1st matched row
.find("td") // find all columns in row
.eq(2) // pick 3rd column
.should("have.text", "25") // make asserion
Additionally, you can use find if you want to find a child element from a selector. In this case I added .find('td').eq(2). But note that this example shows a really convoluted way to search up and down the DOM.
cy.get('[name="courses"] > tbody >tr >td:nth-child(2)').each((e1, index)=>{
const course = e1.text()
if(course.includes('Python')){
cy.get('[name="courses"] > tbody >tr >td:nth-child(2)').eq(index).parent().find('td').eq(2).then((price)=>{
const courseprice = price.text()
// expect(courseprice).to.be.equal('25')
cy.log(courseprice)
})
}
})