Using Spock and Geb testing a page with a Frame

1,857 views
Skip to first unread message

Brett Bergquist

unread,
Mar 11, 2014, 9:23:19 PM3/11/14
to spockfr...@googlegroups.com
I am pretty new to Spock and Geb but am really enjoying both.  I have a Web page that I am writing tests for and the page has a IFRAME with content.  Using the Geb documentation, I understand how to model the IFRAME as a Page object and interact with it using Geb's "withFrame" construct, but I don't know how this interacts with Spock's "when/then" blocks.   For example, I have a test like:

    def "Check that the Add/Edit page is available"() {
        given: "I am at the Account Configuration page"
        at AccountTableConfigPage
        
            when: "I invoke that Add action"
            withFrame(contentFrame)  {
               invokeAddAction()
             }

            then: "Check that I am at the Add/Edit page"
            withFrame(contentFrame) {
                 at AccountTableAddEditPage
                getUsernameDbValue() == "x"
           }
    }

So in the "given", the Page is the container Page with the IFRAME and the "contentFrame" is the what is targeting the IFRAME content Page.   When I do it this way, the assertion "getUsernameDbValue() == "x"' should fail but it does not.   If i change this to an explicit assert 

assert getUsernameDbValue() == "x"'

then this works.   Is this the correct way to nest the "withFrame"?   Initially I tried something like:

    def "Check that the Add/Edit page is available"() {
        given: "I am at the Account Configuration page"
        at AccountTableConfigPage
        
        withFrame(contentFrame) {
            when: "I invoke that Add action"
            invokeAddAction()
                
            then: "Check that I am at the Add/Edit page"
            at AccountTableAddEditPage
            getUsernameDbValue() == "x"
        }
    }

and that did not work either as expected either.   I could not find a good example after searching quite a while so any help will be most appreciated.

Brett

Brett Bergquist

unread,
Mar 12, 2014, 8:57:18 PM3/12/14
to spockfr...@googlegroups.com
So I am still stuck.  I try something like this:

    
    def "Check that the Add/Edit page is available"() {
        given: "I am at the Account Configuration page"
        at AccountTableConfigPage
        
        when: "I invoke that Add action"
        withFrame(contentFrame, AccountTableConfigIcePage) {
            invokeAddAction()
        }
                
        then: "Check that I am at the Add/Edit page"
        withFrame(contentFrame, AccountTableAddEditPage) {
            assert at(AccountTableAddEditPage) != null
            assert getUsernameDbValue() == ""
        }
    }

which I believe should work as reading the Spock docs it looks like that if you have any sort of block, then explicit asserts need to be used.  But when I run the test , I get this as output:

------------- ---------------- ---------------
Testcase: Check that the Add/Edit page is available(com.canoga.nms.web.pkg.pkg9145e100g.ui.app.config.accountconfig.AccountTableBurnSpec): Caused an ERROR
startup failed:
Spec expression: 1: unexpected token: assert @ line 1, column 95.
   ountTableAddEditPage) != null assert get
                                 ^

1 error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Spec expression: 1: unexpected token: assert @ line 1, column 95.
   ountTableAddEditPage) != null assert get
                                 ^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:106)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:148)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:142)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:88)
at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
at org.spockframework.runtime.extension.builtin.TestRuleInterceptor.intercept(TestRuleInterceptor.java:38)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)


Test com.canoga.nms.web.pkg.pkg9145e100g.ui.app.config.accountconfig.AccountTableBurnSpec FAILED
Testsuite: com.canoga.nms.web.pkg.pkg9145e100g.ui.device.devicebrowser.DeviceBrowserBurnSpec
Tests run: 0, Failures: 0, Errors: 0, Time elapsed: 2.753 sec

I don't have a clue as what is wrong with this and really could use some help!

Using Groovy 2.2.2, Geb 0.9.2, and Spock spock-core-1.0-groovy-2.0-20131224.084311-25.jar


Brett Bergquist

unread,
Mar 12, 2014, 9:19:47 PM3/12/14
to spockfr...@googlegroups.com
So I googled some more and found this page:


On this page, each of the "withFrame{}" in the look like

withFrame(xxx) {
     assert ...
     assert ...
} || true

Not sure why the "|| true" is needed, but I tried to do the same:

    def "Check that the Add/Edit page is available"() {
        given: "I am at the Account Configuration page"
        at AccountTableConfigPage
        
        when: "I invoke that Add action"
        withFrame(contentFrame, AccountTableConfigIcePage) {
            invokeAddAction()
        }
                
        then: "Check that I am at the Add/Edit page"
        withFrame(contentFrame, AccountTableAddEditPage) {
            assert at(AccountTableAddEditPage)
            assert ! getUsernameDbValue()
        } || true
    }


Now this appears to be working correctly.   Any ideas why the "|| true" is needed?    Just for my own knowledge.

Brett

pnie...@gmail.com

unread,
Mar 13, 2014, 1:33:01 AM3/13/14
to spockfr...@googlegroups.com
Probably `withFrame` returns a value that’s false according to Groovy truth, and so `|| true` is necessary not to fail the condition. (The whole `withFrame` expression is considered a condition because it occurs in a `then` block.)

The `unexpected token: assert` is an unrelated issue with multiline conditions. When a multiline condition fails, it sometimes cannot be rendered and instead throws a parse exception. This is a known issue that will get fixed eventually.

Cheers,
Peter

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

Luke Daley

unread,
Mar 13, 2014, 1:34:55 AM3/13/14
to spockfr...@googlegroups.com
withFrame returns the return value of the given closure, and assert is returning “void” I guess.

Not sure what we could do about this. Perhaps when Spock rewrites the assert statement it could rewrite it in such a way so that it returns the result of the assertion.

pnie...@gmail.com

unread,
Mar 13, 2014, 1:50:57 AM3/13/14
to spockfr...@googlegroups.com
That’s an interesting option, but also a somewhat scary change to make at this point. It would effectively make satisfied explicit conditions evaluate to `true` rather than `null`, which could break some (odd) code out in the wild.

Cheers,
Peter

Luke Daley

unread,
Mar 13, 2014, 4:02:15 AM3/13/14
to spockfr...@googlegroups.com
Lucky it's pre 1.0 :)

Brett Bergquist

unread,
Mar 13, 2014, 7:17:53 AM3/13/14
to spockfr...@googlegroups.com
Thanks guys for the explanation.  Both Geb and Spock are really a delight to use even with hiccup here and there!

Brett

Stelios Anastasakis

unread,
Apr 5, 2017, 10:50:02 AM4/5/17
to Spock Framework - User
Actually, you should not use `then ... || true` cause this is not testing anything. From my understanding `withFrame` should return a boolean. the code
        withFrame(....) {
            assert ...
            assert ...
        }
doesnt return a boolean, so that block fails. To resovle this you should (definatelly not use || true, but ) use either sth like :
        withFrame(....) {
            {Boolean-value} && {Boolean-value} && {Boolean-value}
        }
or better, in my opinion:
        withFrame(....) {
            assert ...
            assert ...
            true
        }
Reply all
Reply to author
Forward
0 new messages