2016-06-09 129 views
2

一位同事,我已經嘗試了幾天讓硒與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(""); 
             ^
+0

第二個錯誤是由於不理解Groovy插值字符串。子字符串「$ cphMainContent」正試圖通過該名稱查找變量。用雙引號替換雙引號將其關閉。無論如何,通常使用Groovy中的Selenium都是用「geb」完成的。 –

+0

第一個是'by'需要一個大寫'B' –

+0

FYI在''''裏面'''用於處理代碼或任何變量...所以如果你的元素的名字包含'$'...你應該使用'ctl00 \'$ cphMainContent \「$ txtUserName' ...第二件事是你使用'by.name'這是'By.name' .. –

回答

0

說明:

  1. 你並不需要一個靜態無效的主要在Groovy腳本。只是腳本。
  2. 在groovy中,「hello $ name」是一個GString,而不是一個String:它進行字符串插值。 Groovy試圖在名爲name的作用域中找到一個變量,將其插入到字符串中。要在不插,Java的方式創建一個簡單的字符串,用「你好$名稱」(單引號)
@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(""); 
+0

那麼'by.name'呢? –

+0

你是什麼意思? – loteq

+0

新增資本By.name – loteq

-3

謝謝你的建議。

謝謝腳本現在正在工作。 1)將雙引號更改爲單引號2)將by更改爲By。

+0

這不是一個完整的答案。 – loteq

+0

你期望什麼額外的信息? –

+0

哇,我想沒有好的行爲不受懲罰。下一次,低聲譽的人發佈一個問題,我會避免回答 – loteq

相關問題