Я пытаюсь удалить указанного c пользователя и предпочитаю, чтобы тест не прошел, а затем удалил другого пользователя, если нужный мне пользователь не существует.
Проблема, я не могу Кажется, я нашел способ проверить, стоит ли флажок рядом с нужным мне пользователем. Я использовал гнездо .each (), чтобы сначала найти пользователя Мэнди Смит, а затем установить флажок с тем же индексом.
HTML:
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
html изображение
Спасибо за все сообщения. Я собираюсь возиться с этим сегодня, протестировать различные комментарии и посмотреть, смогу ли я найти решение. Не стесняйтесь оставлять отзывы, так как я буду проверять в течение дня.
Решено Спасибо всем, вот ответ, я поговорил по телефону со старшим разработчиком и, увидев, что я делал он меня через несколько вещей. Спасибо @oldschooled за его потрясающий ответ и последующему @ eric99 за то, что он предложил немного подробнее для меня. Вы двое - рок-звезды.
Ответ
it(`should select Mandy Smith with contains only`, () => {
cy.contains('td', 'Mandy Smith').siblings().eq(0).children().eq(0).click();
});