一位同事,我已經嘗試了幾天讓硒與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' .. –