2016-06-09 4 views
2

동료와 나는 Selenium이 groovy로 작업하기 위해 며칠 동안 노력했다. 전혀 성공하지 못했다. 우리는 자바와 아무런 문제가없는 복잡한 테스트를 할 수있다. 그러나 아무 것도 그루비 (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. 당신은 그루비 스크립트에서 정적 무효 메인 필요하지 않습니다 . 그냥 스크립트.
  2. groovy에서 "hello $ name"은 문자열이 아닌 GString이며 문자열 보간을 수행합니다. Groovy는 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(""); 
+0

그리고'by.name'은 어떨까요 ?? –

+0

무슨 뜻인가요? – loteq

+0

자본금 By.name – loteq

-3

감사를 사용, 보간, 자바 스타일없이 간단한 문자열을 만듭니다.

감사합니다. 이제 스크립트가 작동합니다. 1) 큰 따옴표를 작은 따옴표로 변경했습니다. 2) by를 By로 변경했습니다.

+0

이것은 완전한 대답은 아닙니다. – loteq

+0

추가 정보는 무엇입니까? –

+0

와우, 아무런 선행도 벌주지 않을 것 같아. 다음에는 낮은 평판을 가진 사람이 질문을 게시합니다. 나는 대답하지 않습니다. – loteq