I'm trying to delete a specific user, and would rather the test fail then delete a different user if the user I want doesn't exist.
The issue, I can't seem to find a way to verify the checkbox is next to the user I want. I've been using a .each() nest to first find the user Mandy Smith, and then select the checkbox with the same index.
Note, I'm trying to delete the user "Mandy Smith" and while I can just use the following code to select her, depending on how quick the test gets to this point it might actually delete the first user.
Current code that deletes Mandy Smith:
// looks for the checkbox related to Mandy Smith User
cy.get('input[type="checkbox"]')
.each(($elem, index) => {
if(index === 2) {
cy.wrap($elem).click({force:true});
}
});
While this will generally grab her, depending how quickly filters are being cleared it can actually choose the Automated User for deletion. I of course don't want this, I'm trying to find a good way to verify the index Mandy Smith is at and apply that to her checkbox.
The checkbox is dynamic, so it will iterate up based on what is clicked, so i don't want to choose the exact checkbox with it's label "#mat-checkbox-11" as that's what it happens to be at while i'm looking at the code, but it was 5 earlier.
I thought this would let me find the index and apply it to the checkbox:
// looks for the checkbox related to Mandy Smith User
it(`should select Mandy, through verification that it's her index`, () => {
cy.get('.mat-column-name.ng-star-inserted')
.each(($name, i) => {
if($name === 'Mandy Smith') {
let dex = i;
cy.get('input[type="checkbox"]')
.each(($elem, index) => {
if(index === dex) {
cy.wrap($elem).click({force:true});
}
});
}
});
});
This actually just ends up deleting the Automated User instead.
I did run some tests and found the string it's returning is this, " Automated User Mandy Smith ". So, even though I'm iterating through the table, it seems to pull both text fields. Am I missing something on how to grab a single set of text, when all the class names are the same?
This means it deletes the first user as the if statement will show Mandy Smith, and then choose the first user which is Automated User instead of the second user, Mandy Smith. Maybe I need to know how to grab text from each class with the same name.
I don't know, I'm a little lost now on how to potentially do this.
EDIT: I found I can invoke text within the if statement, but it never gets within the if statement. I checked what it yielded and pasted that into my if and it still didn't work. Here is the code that is grabbing the elements innerText, and it's meant to compare to the word. Is there a reason it wouldn't evaluate true?
it(`should select Mandy, through verification that it's her`, () => {
cy.get('.mat-cell.cdk-cell.text-capitalize.cdk-column-name.mat-column-name')
.each(($name, i) => {
//let columnText = ($name).invoke('text');
if(cy.get($name).invoke('text') === ' Mandy Smith ') {
cy.log('it made it inside the if');
}
});
});
EDIT 7/15/2020: Here is an image of the entire table. Yellow arrow is her row, right above that is user 1's row. Green arrow is her checkbox, red arrow is her name from the name column
Thanks for everyone's posts. I'm going to be messing with it today and test the various comments and see if I can find a solution. Feel free to keep giving feedback, as I'll check throughout the day.
Solved Thanks everyone here is the answer, I got on the phone with a senior developer and after seeing what I was doing he ran me through a few things. Thanks to @oldschooled for his awesome answer, and the follow up @eric99 for offering breaking it down a bit more for me as well. You two are rockstars.
Answer
it(`should select Mandy Smith with contains only`, () => {
cy.contains('td', 'Mandy Smith').siblings().eq(0).children().eq(0).click();
});