3

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!

Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51

3 Answers3

3

Just by eyeballing your example, what you're getting back is 3 DIVs. It looks like you need to go a bit deeper, adding a .find('span') just before your asterisk.

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

becomes

assert $(
   'UL.select2-results LI.select2-results-dept-0.select2-result.select2-result-selectable',
    0..3).find('span')*.text() == ["GBP", "KPW", "USD"]
inanutshellus
  • 9,683
  • 9
  • 53
  • 71
1

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.

Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51
  • Well at least you found a solution! Be sure to mark it as the right answer so others know not to try and solve it for you. – inanutshellus Feb 27 '14 at 17:42
  • I will do that for sure. I also found the solution for the question you asked and deleted yesterday! I was about to paste it as answer and your question was gone! – Sharif Mamun Feb 27 '14 at 18:19
  • Hah, really? Yeah, I just needed to upgrade Geb to the next teeny rev. Funny. – inanutshellus Feb 27 '14 at 21:25
  • Really, you had a problem with string. – Sharif Mamun Feb 27 '14 at 21:28
  • Ah, there may have been a typo in my example, but the base issue was that Geb added support for `has()` to have multiple parameters in 0.9.1 (e.g. `.has('p', foo:'bar')`. In 0.9.0 it would only support the tag name, e.g. `.has('p')`. – inanutshellus Feb 27 '14 at 21:34
  • yes, you are correct! You can not use multiple parameters/properties as you were trying to do so :) – Sharif Mamun Feb 27 '14 at 21:45
0

I'm thinking you might be able to simplify how you get the currencies by doing the following:

$("div.select2-result-label").children("span")*.text() == ["GBP", "KPW", "USD"]

Because you already have classes on the divs, you can easily access them using the above selector, then you can get the children span elements and get the text of each span. I hope this helps!