Skip to content

Instantly share code, notes, and snippets.

@dennisreimann
Created September 3, 2015 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennisreimann/e1acd302e971f47df865 to your computer and use it in GitHub Desktop.
Save dennisreimann/e1acd302e971f47df865 to your computer and use it in GitHub Desktop.
Galen forEach example with nested objects
@objects
form-row-* .form__row
label .form__row__label
input .form__row__input
= Form =
@on small
@forEach [form-row-*] as row
${row}.label:
above ${row}.input
@on medium, large
@forEach [form-row-*] as row
${row}.label:
left-of ${row}.input
= Form =
form-row-1.label:
above form-row-1.input
form-row-1.label.label:
-> above form-row-1.label.input
-> : Cannot find locator for "form-row-1.label.label" in page spec
@dennisreimann
Copy link
Author

Galen (2.0.6) seems to execute forEach loops for nested child objects as well, which does not work.

Is there a way to get around this?

@ishubin
Copy link

ishubin commented Sep 3, 2015

Instead of:

 @forEach [form-row-*] as row

Try:

 @forEach [form-row-#] as row

@dennisreimann
Copy link
Author

@ishubin thanks a lot, that works!

However, the selectors seem to get sorted alphabetically, as I now get this unexpected error:

            form-row-2:
->              below form-row-13
->              :   "form-row-2" is -1233px below "form-row-13" but it should be greater than -1px

Yes, there are many form rows in the example I'm testing and it seems to get to #2 after #13 (which is the last row in the test page).

@ishubin
Copy link

ishubin commented Sep 3, 2015

@dennisreimann Wow! I haven't thought of this use case but it is so obvious now. Damn! I guess I will have to figure out a better sorting mechanism in Galen.

I actually wanted to propose you a workaround but when I tried it I found a nasty bug in all v2.0.*
I will fix it later today and will post a solution. Sorry for this mess. The release of version 2 didn't go that smooth due to a lot of changes :(

@ishubin
Copy link

ishubin commented Sep 3, 2015

The solution is using an in-built JavaScript "count" function. something like this should work:

@objects
  form-row-*          .form__row
    label             .form__row__label
    input             .form__row__input

= Form =

    @for [1 - ${count("form-row-#")}] as i
      form-row-${i}.label:
        @on small
            above form-row-${i}.input
        @on medium, large
            left-of form-row-${i}.input

Right now this "count" function is not working in version 2.0.8. I have pushed a fix but I will need to release it to 2.0.9.

But to be honest I would be against this kind of specification. I would rather move these kind of checks into "component". Take a look at this paragraph http://galenframework.com/docs/reference-galen-spec-language-guide/#Component

@dennisreimann
Copy link
Author

@ishubin thanks for your help and all the information.

Concerning the component: Yes, that how we are doing it, this is just a simplified example I made up to clarify the issue. I'll check the count approach tomorrow and will report in case I come across something weird.

Thanks again for your support!

@ishubin
Copy link

ishubin commented Sep 3, 2015

@dennisreimann I have just released a new version (2.0.9). In it I have fixed the count function and also the sorting of page objects. Instead of alphabetic search it should perform a search like a human. Please let me know if you find some new weird issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment