Search code examples
htmlcssgrailsspockgeb

How to select the inner elements' text in GEB?


I have the following scenario:

<div>
    <ul class="select2-results" style="width: 400px;">
        <li class="select2-results-dept-0 select2-result select2-result-selectable">
            <div class="select2-result-label">
                <span id="12345" class="null">
                    GBP
                </span>
            </div>
        </li>
        <li class="select2-results-dept-0 select2-result select2-result-selectable">
            <div class="select2-result-label">
                <span id="45678" class="null">
                    KPW
                </span>
            </div>
        </li>
        <li class="select2-results-dept-0 select2-result select2-result-selectable">
            <div class="select2-result-label">
                <span id="12345" class="null">
                    USD
                </span>
            </div>
        </li>
    </ul>
</div>

I need to select the currencies from it. I was trying the following thing but no way:

assert $('UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable', 0..2)*.text() == ["GBP", "KPW", "USD"]

Even I could not handle this one:

assert $('UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable')*.size() == 3

Though, I can handle a single element:

assert $('UL.select2-results').find("LI.select2-results-dept-0.select2-result.select2-result-selectable", 0).text() == "GBP"

Any help or suggestion would be really beneficial for me! Thanks!


Solution

  • I don't know why @Gabriel's answer did not work! I know there was an issue with 0..3 as it would be 0..2! But instead of find it finally worked:

    assert $('UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable span')*.text() == ["GBP", "KPW", "USD"]
    

    I would love to see if anyone can explain why indexing+using find+* did not work together for this particular problem or was there any logical syntax related problem.