'unable to get selenium to work with groovy
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("");
^
Solution 1:[1]
Explanations:
- You don't need a static void main in a Groovy script. Just script on.
- In Groovy,
"hello $name"is a GString, not a String: it does string interpolation. Groovy tries to find a variable in the scope callednameto insert it into the string. To create a simple string without interpolation, java style, use'hello $name'(with single quotes)
Explanations:
- You don't need a static void main in a Groovy script. Just script on.
- In Groovy,
"hello $name"is a GString, not a String: it does string interpolation. Groovy tries to find a variable in the scope callednameto 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("");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | M. Justin |
