一位同事,我已经尝试了几天让硒与groovy一起工作,但没有成功。我们可以使用java进行复杂的测试工作没有问题......但在groovy下没有任何工作,甚至没有简单的事情。我们得到可怕的编译错误.....我们尝试了各种“抓取”和“导入”语法,没有任何工作。无法让硒与groovy一起工作
具体做法是:
包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("");
}
}
结果举例:
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("");
^
第二个错误是由于不理解Groovy插值字符串。子字符串“$ cphMainContent”正试图通过该名称查找变量。用双引号替换双引号将其关闭。无论如何,通常使用Groovy中的Selenium都是用“geb”完成的。 –
第一个是'by'需要一个大写'B' –
FYI在''''里面'''用于处理代码或任何变量...所以如果你的元素的名字包含'$'...你应该使用'ctl00 \'$ cphMainContent \“$ txtUserName' ...第二件事是你使用'by.name'这是'By.name' .. –