一位同事,我已经尝试了几天让Selenium与groovy合作,但根本没有成功。我们可以使用java进行复杂的测试没问题......但是没有什么能在groovy下工作,甚至不是简单的事情。我们得到了可怕的编译错误.....我们尝试了各种“Grab”和“import”语法,没有任何作用。
具体做法是:
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("");
}
}
示例结果:
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("");
^
答案 0 :(得分:0)
说明:
name
的范围内查找变量,以将其插入到字符串中。要创建一个没有插值的简单字符串,java样式,请使用' hello $ name' (单引号) @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("");
答案 1 :(得分:-3)
感谢您的建议。
感谢脚本现在正在运行。 1)将双引号更改为单引号2)将by更改为By。