groovygeb

How to use siblings() and closest() in Geb test


I am trying to write a script to click an icon which is a part of the table header. Each column in the table has this icon in it (ascending order and descending order sorting icons). I am using Geb to do this. Here is how I am trying to do it:

In my SortingSpec.groovy file:

header.closest("div.customSortDownLabel").click()

I also tried

header.siblings('div.customSortDownLabel').first().click()

In the SortingPage.groovy file:

header {
    grid.$(class: 'div.customHeaderLabel', text: 'Country')
}

In my html:

<div>
    <div class="customHeaderLabel">{{params.displayName}}</div>
    <div *ngIf="params.enableSorting" (click)="onSortRequested('asc', $event)" [ngClass]="ascSort" class="customSortDownLabel">
        <i class="fa fa-long-arrow-alt-down"></i></div>
    <div *ngIf="params.enableSorting" (click)="onSortRequested('desc', $event)" [ngClass]="descSort" class="customSortUpLabel">
</div>

None of them worked for me. It is not able to find the selector. Any suggestions are appreciated.

Error I see is:

geb.error.RequiredPageContentNotPresent: The required page content 'header - SimplePageContent (owner: SortingGrid, args: [], value: null)' is not present


Solution

  • siblings() didnt work for me but next() worked for me. next() grabs the next sibling elements of the current context elements.

    Example: 1. header.next().click() clicks the very next sibling

    1. header.next("div.customSortDownLabel").click() looks for the very next sibling with the matching selector of 'div.customSortDownLabel' and then clicks it.