2

a colleague and I have tried for a couple of days to get Selenium to work with groovy, with no success at all. We can get complex tests work with java no problem ... but nothing works under groovy, not even simple things. We get terrible compile errors..... we have tried all kinds of "Grab" and "import" syntax, nothing works.

Specifically:

package test_groovy_project

 @Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
 import org.springframework.jdbc.core.JdbcTemplate
 import groovy.grape.Grape
 // @Grab(group="org.seleniumhq.selenium", module="selenium-java", version="2.53.0")
 @Grab(group="org.seleniumhq.selenium", module="selenium-java", version="2.53.0")
 @Grab(group="org.seleniumhq.selenium", module="selenium-firefox-driver", version="2.53.0")
 @Grab(group="org.seleniumhq.selenium", module="selenium-support", version="2.53.0")
 //@Grab(group:"org.seleniumhq.selenium", module:"selenium-firefox-driver", version:"2.53.0")
 //@Grab(group:"org.seleniumhq.selenium", module:"selenium-support", version:"2.53.0")
 @Grab('org.seleniumhq.selenium:selenium-java:2.53.0')

 import org.openqa.selenium.*
 import org.openqa.selenium.WebDriver
 import org.openqa.selenium.WebDriver.*
 import org.openqa.selenium.By
 import org.openqa.selenium.firefox.FirefoxDriver
 import org.openqa.selenium.firefox.FirefoxDriver.*


 class Groovy_test_class {
    static main(args) {



        System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
 //     System.setProperty("webdriver.firefox.bin","C:\\Users\\Shamsur.Masum\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
         WebDriver driver= new FirefoxDriver();
         driver.get("http://www.google.com/");
        driver.findElement(by.name("ctl00$cphMainContent$txtUserName")).sendKeys("");


    }

 }

Example Result:

 org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
 C:\Users\charles\workspace\test_groovy_project\src\test_groovy_project\Groovy_test_class.groovy: 32: Apparent variable 'by' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
 You attempted to reference a variable in the binding or an instance variable from a static context.
 You misspelled a classname or statically imported field. Please check the spelling.
 You attempted to use a method 'by' but left out brackets in a place not allowed by the grammar.
  @ line 32, column 22.
            driver.findElement(by.name("ctl00$cphMainContent$txtUserName")).sendKeys("");
                         ^

 C:\Users\charles\workspace\test_groovy_project\src\test_groovy_project\Groovy_test_class.groovy: 32: Apparent variable 'cphMainContent' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
 You attempted to reference a variable in the binding or an instance variable from a static context.
 You misspelled a classname or statically imported field. Please check the spelling.
 You attempted to use a method 'cphMainContent' but left out brackets in a place not allowed by the grammar.
  @ line 32, column 37.
            driver.findElement(by.name("ctl00$cphMainContent$txtUserName")).sendKeys("");
                                        ^
M. Justin
  • 14,487
  • 7
  • 91
  • 130
Charles F Radley
  • 163
  • 1
  • 2
  • 11
  • The second error is due to not understanding Groovy interpolated strings. The substring "$cphMainContent" is trying to find a variable by that name. Replace the double quotes with single quotes to turn that off. In any case, typically using Selenium from Groovy is done with "geb". – David M. Karr Jun 09 '16 at 18:49
  • The first is that `by` needs a capital `B` – tim_yates Jun 09 '16 at 19:21
  • FYI in groovy `$` inside `""` use to adress the code or any variable...so if your element's name contains `$`...you should use as `ctl00\"$cphMainContent\"$txtUserName`...second thing you are using `by.name` which to be `By.name`.. – Saurabh Gaur Jun 09 '16 at 19:27

2 Answers2

0

Explanations:

  1. You don't need a static void main in a Groovy script. Just script on.
  2. In Groovy, "hello $name" is a GString, not a String: it does string interpolation. Groovy tries to find a variable in the scope called name to insert it into the string. To create a simple string without interpolation, java style, use 'hello $name' (with single quotes)

Explanations:

  1. You don't need a static void main in a Groovy script. Just script on.
  2. In Groovy, "hello $name" is a GString, not a String: it does string interpolation. Groovy tries to find a variable in the scope called name to insert it into the string. To create a simple string without interpolation, java style, use 'hello $name' (with single quotes)
@Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
import groovy.grape.Grape

// @Grab(group="org.seleniumhq.selenium", module="selenium-java", version="2.53.0")
@Grab(group="org.seleniumhq.selenium", module="selenium-java", version="2.53.0")
@Grab(group="org.seleniumhq.selenium", module="selenium-firefox-driver", version="2.53.0")
@Grab(group="org.seleniumhq.selenium", module="selenium-support", version="2.53.0")
//@Grab(group:"org.seleniumhq.selenium", module:"selenium-firefox-driver", version:"2.53.0")
//@Grab(group:"org.seleniumhq.selenium", module:"selenium-support", version:"2.53.0")
@Grab('org.seleniumhq.selenium:selenium-java:2.53.0')

import org.openqa.selenium.*
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebDriver.*
import org.openqa.selenium.By
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxDriver.*

System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver= new FirefoxDriver();
driver.get("http://www.google.com/");
driver.findElement(By.name('ctl00$cphMainContent$txtUserName')).sendKeys("");
M. Justin
  • 14,487
  • 7
  • 91
  • 130
Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
-3

Thanks for the advice.

Thanks the script is now working. 1) changed the double quotes to single quotes 2) changed the by to By.

Charles F Radley
  • 163
  • 1
  • 2
  • 11