Groovy can i dynamically execute a string as groovy command?
Asked Answered
S

2

5

I would like to dynamically execute a groovy statement from my database.

I'm currently using geb (www.gebish.org) to automate my browser and i would like the use "css selectors" from my database.

For example:

Browser.drive {
   go "www.test.com"

   $("form", name: "password").value("Test")
}

In this example i would like to move "$("form", name: "password").value("Test")" completely to the database and just call it dynamically in my code. In such a thing possible?

I'm new to Groovy and Java and maybe i have a error in reasoning and there is a simpler solution for such a problem...please help me :)

Specular answered 11/7, 2011 at 19:39 Comment(0)
B
2

Groovy can be concise and expressive, so you can quickly jump into the scripts to read or make changes, which means you might be able to just put your code into the scripts instead of into the database -- this is why you often see configurations done in code instead of properties or databases.

Anyway...

GroovyShell will allow you to evaluate any String you build as code, so you could write code to build one big string from your database, then pass it to GroovyShell.evaluate(String) to execute it.

Here's a sample:

#!/usr/bin/env groovy
new GroovyShell().evaluate("""
    @Grapes([
        @Grab("org.codehaus.geb:geb-core:0.6.0"),
        @Grab("org.seleniumhq.selenium:selenium-htmlunit-driver:2.0rc3")
    ])
    import geb.Browser

    Browser.drive {
        go "http://www.test.com/"
        $("form", name: "password").value("Test")
    }
""")

More simply, you could skip using GroovyShell within your own scripts and write the bit of code that reads the DB and generates the code and have it just dump the code to a file, then execute the new file any time you want. That file can serve as a sort of snapshot of what really got executed.

Britneybritni answered 12/7, 2011 at 3:2 Comment(1)
thanks this solution works pretty good, the only problem is that i have to bind each variable/property which wish to use in the script by myself with the binding method. I wonder if there isn't a solution where i can execute the string as it would be directly in the code without the binding things etc..Specular
P
16

If you have some Groovy code in a String you can use the Eval class to execute it. Here's a simple example you can try out in the Groovy console:

def code = "2 + 2"
assert Eval.me(code) == 4
Peraza answered 12/7, 2011 at 8:30 Comment(2)
Works for me and with much less code than the answer above.Orthogonal
Imagine coming across this answer from Google and seeing a comment from yourself two years prior, SO is awesome. Still the better answer IMO!Orthogonal
B
2

Groovy can be concise and expressive, so you can quickly jump into the scripts to read or make changes, which means you might be able to just put your code into the scripts instead of into the database -- this is why you often see configurations done in code instead of properties or databases.

Anyway...

GroovyShell will allow you to evaluate any String you build as code, so you could write code to build one big string from your database, then pass it to GroovyShell.evaluate(String) to execute it.

Here's a sample:

#!/usr/bin/env groovy
new GroovyShell().evaluate("""
    @Grapes([
        @Grab("org.codehaus.geb:geb-core:0.6.0"),
        @Grab("org.seleniumhq.selenium:selenium-htmlunit-driver:2.0rc3")
    ])
    import geb.Browser

    Browser.drive {
        go "http://www.test.com/"
        $("form", name: "password").value("Test")
    }
""")

More simply, you could skip using GroovyShell within your own scripts and write the bit of code that reads the DB and generates the code and have it just dump the code to a file, then execute the new file any time you want. That file can serve as a sort of snapshot of what really got executed.

Britneybritni answered 12/7, 2011 at 3:2 Comment(1)
thanks this solution works pretty good, the only problem is that i have to bind each variable/property which wish to use in the script by myself with the binding method. I wonder if there isn't a solution where i can execute the string as it would be directly in the code without the binding things etc..Specular

© 2022 - 2024 — McMap. All rights reserved.