You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Mohan Radhakrishnan <ra...@gmail.com> on 2018/01/16 13:36:32 UTC

Executing dynamic DSL command

Hi,

      I am reading the book 'Groovy in Action' and this code is based on
that.

My base script stores 'DynamicXPath' like this.

abstract class AxesBaseScript extends Script {

    public DynamicXPath bootStrap(String initialPath ){
        this.binding.dxp.bootStrap initialPath
    }
}

And here I want to execute a dynamic DSL command 'dsl '

by storing it in the binding as 'DslCommand'.

    def evaluatexpath( String dsl ){
        def binding = new Binding(
                dsp: new DynamicXPath(),
                DslCommand: dsl
        )

        def shell = new GroovyShell(this.class.classLoader, binding)

        shell.evaluate '''
            @BaseScript(com.automation.script.AxesBaseScript)
            import groovy.transform.BaseScript
            DslCommand
        '''
    }

Looks like the DSL string I pass 'bootStrap somestring' is returned as it
is as a string by evaluate.

Can I not execute 'bootStrap somestring' ?

The relevant section in the book is '*Listing 19.12 Using a custom base
script class'*

*My '*DslCommand' replaces 'move left' which you see at the end of the
listing.
I just want to pass my DSL commands to the evaluate method as parameters
through the binding.


Thanks,
Mohan

Re: Executing dynamic DSL command

Posted by Mohan Radhakrishnan <ra...@gmail.com>.
I've been reading the older posts and this code which constructs a closure
works.

It evalutes this line of code.

bootStrap '//*[@id=\"pane\"]'

public Object runInContext(Object context, String script, GroovyShell
        shell) {
    Closure cl = (Closure) shell.evaluate("{->"  + script  + "}");
    cl.setDelegate(context);
    cl.setResolveStrategy(Closure.DELEGATE_FIRST);
    return cl.call();
}

Thanks,

Mohan


On 16 January 2018 at 19:35, Mohan Radhakrishnan <
radhakrishnan.mohan@gmail.com> wrote:

> I mean that this works if the DSL command is hard-coded. I was trying to
> pass it into the shell.
>
> Am I allowed to pass it into the shell ? This evaluation is generic for
> many such fluent commands.
>
> def evaluatexpath( String dsl ){
>     def binding = new Binding(
>             dxp: new DynamicXPath(),
>             DslCommand: dsl
>     )
>
>     def shell = new GroovyShell(this.class.classLoader, binding)
>
>     shell.evaluate '''
>         @BaseScript(com.automation.script.AxesBaseScript)
>         import groovy.transform.BaseScript
>         bootStrap "//*[@id='pane']"
>     '''
> }
>
> Mohan
>
>
> On 16 January 2018 at 19:06, Mohan Radhakrishnan <
> radhakrishnan.mohan@gmail.com> wrote:
>
>> Hi,
>>
>>       I am reading the book 'Groovy in Action' and this code is based on
>> that.
>>
>> My base script stores 'DynamicXPath' like this.
>>
>> abstract class AxesBaseScript extends Script {
>>
>>     public DynamicXPath bootStrap(String initialPath ){
>>         this.binding.dxp.bootStrap initialPath
>>     }
>> }
>>
>> And here I want to execute a dynamic DSL command 'dsl '
>>
>> by storing it in the binding as 'DslCommand'.
>>
>>     def evaluatexpath( String dsl ){
>>         def binding = new Binding(
>>                 dsp: new DynamicXPath(),
>>                 DslCommand: dsl
>>         )
>>
>>         def shell = new GroovyShell(this.class.classLoader, binding)
>>
>>         shell.evaluate '''
>>             @BaseScript(com.automation.script.AxesBaseScript)
>>             import groovy.transform.BaseScript
>>             DslCommand
>>         '''
>>     }
>>
>> Looks like the DSL string I pass 'bootStrap somestring' is returned as
>> it is as a string by evaluate.
>>
>> Can I not execute 'bootStrap somestring' ?
>>
>> The relevant section in the book is '*Listing 19.12 Using a custom base
>> script class'*
>>
>> *My '*DslCommand' replaces 'move left' which you see at the end of the
>> listing.
>> I just want to pass my DSL commands to the evaluate method as parameters
>> through the binding.
>>
>>
>> Thanks,
>> Mohan
>>
>
>

Re: Executing dynamic DSL command

Posted by Mohan Radhakrishnan <ra...@gmail.com>.
I mean that this works if the DSL command is hard-coded. I was trying to
pass it into the shell.

Am I allowed to pass it into the shell ? This evaluation is generic for
many such fluent commands.

def evaluatexpath( String dsl ){
    def binding = new Binding(
            dxp: new DynamicXPath(),
            DslCommand: dsl
    )

    def shell = new GroovyShell(this.class.classLoader, binding)

    shell.evaluate '''
        @BaseScript(com.automation.script.AxesBaseScript)
        import groovy.transform.BaseScript
        bootStrap "//*[@id='pane']"
    '''
}

Mohan


On 16 January 2018 at 19:06, Mohan Radhakrishnan <
radhakrishnan.mohan@gmail.com> wrote:

> Hi,
>
>       I am reading the book 'Groovy in Action' and this code is based on
> that.
>
> My base script stores 'DynamicXPath' like this.
>
> abstract class AxesBaseScript extends Script {
>
>     public DynamicXPath bootStrap(String initialPath ){
>         this.binding.dxp.bootStrap initialPath
>     }
> }
>
> And here I want to execute a dynamic DSL command 'dsl '
>
> by storing it in the binding as 'DslCommand'.
>
>     def evaluatexpath( String dsl ){
>         def binding = new Binding(
>                 dsp: new DynamicXPath(),
>                 DslCommand: dsl
>         )
>
>         def shell = new GroovyShell(this.class.classLoader, binding)
>
>         shell.evaluate '''
>             @BaseScript(com.automation.script.AxesBaseScript)
>             import groovy.transform.BaseScript
>             DslCommand
>         '''
>     }
>
> Looks like the DSL string I pass 'bootStrap somestring' is returned as it
> is as a string by evaluate.
>
> Can I not execute 'bootStrap somestring' ?
>
> The relevant section in the book is '*Listing 19.12 Using a custom base
> script class'*
>
> *My '*DslCommand' replaces 'move left' which you see at the end of the
> listing.
> I just want to pass my DSL commands to the evaluate method as parameters
> through the binding.
>
>
> Thanks,
> Mohan
>