Sometimes, automating a test is straightforward, and sometimes, it’s not. Take grids for example. Adding data-testids
helps but there are times when you may not be able to be able to easily select the element you need. Sometimes, the only thing you can do is turn to your .parents()
TL;DR Use .parents() to track which parent element you need to select
cy.get(".rt-td").parents()
Once, we know which parent to select, we can use .find()
to select the element we want to take action on.
Let’s use DemoQA’s Web Tables page to test this out.
Let’s say we need to automate clicking the edit button in a row. We could use the #id available to us…
But what if we wanted to make sure that we are editing the “right” row that includes the name “Cierra”?
Here’s how we can do that.
beforeEach(() => {
cy.visit("https://demoqa.com/webtables");
});
describe("Find the parents", () => {
it("Select the parent element to edit the correct row", () => {
cy.get(".rt-td").contains("Cierra").parents();
});
});
At first, we’ll leave .parents()
empty. Let’s see what happens! 😁
We have sooooo many parents to choose from! But, we only need the parent of the row we want to edit.
Using the Cypress dashboard, click the “parents” displayed in the UI.
Clicking “parents” pins the results in the devTools console inside of Cypress! Right-click “parents” and select “Inspect”
Click the “Console” tab
Here are our choices. Sometimes the choice is clear and sometimes it isn’t. We can go back and check the DOM tree structure or use some good old trial and error to find the parent you need.
cy.get(".rt-td")
.contains("Cierra")
.parents("div.rt-tr-group")
Sweet! That selected the whole row! Now, we can pair .find()
with .parents()
and click the edit button.
cy.get(".rt-td")
.contains("Cierra")
.parents("div.rt-tr-group")
.find("#edit-record-1")
Here’s what it looks like before the button is clicked:
Now, let’s add .click()
cy.get(".rt-td")
.contains("Cierra")
.parents("div.rt-tr-group")
.find("#edit-record-1")
.click();
💥 Boom!
There’s the registration form!
Here is what our test looks like:
beforeEach(() => {
cy.visit("https://demoqa.com/webtables");
});
describe("Find the parents", () => {
it("Select the parent element to edit the correct row", () => {
cy.get(".rt-td")
.contains("Cierra")
.parents("div.rt-tr-group")
.find("#edit-record-1")
.click();
});
});
In automation, sometimes, all we need is a little guidance. In a pinch, .parents()
can help us find our way.
Till next time…
All my posts are free to read and clicking subscribe will bring each post to your inbox. If my work brings you joy, and you’d like to support it, you can become a paid subscriber by clicking the button above. If a paid subscription is not your thing, you can support my caffeine addiction writing by clicking the button below! Thanks!
Written with Ultimate Spacefolk Banjo Compilation || 2+ Hours Ethereal Vibes Playlist playing in the background
Thank you for your patience! Let's see what we can do.
Hmm, I'm not sure exactly. Since you just created the new directory, it might be helpful to try a super simple test. It sounds like Cypress is working correctly, which is great. If you comment out the test code and use "cy.visit('google.com')", what results do you get?
If that test passes and you can see that the test is going to google.com, then we can start troubleshooting my code.
FYI, I'm no longer writing on Substack. Feel free to follow me on over at Beehiiv: https://failureisfeedback.beehiiv.com/
You've also made me realize that it would be a good idea to have a github for examples. I'll work to get that up and running. Thank you for for your comment!
I updated to Cypress 14.3.1, created a new directory, let Cpress create the initial test case. THat ran successful. Then I added a new spec and pasted your code there. I ran it and it brought this message:
(uncaught exception)Error: Script error. Cypress detected that an uncaught error was thrown from a cross origin script. We cannot provide you the stack trace, line number, or file where this error occurred. Check your Developer Tools Console for the actual error - it should be printed there. It's possible to enable debugging these scripts by adding the crossorigin attribute and setting a CORS header.
Do you know why?