2016-06-09 143 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