【问题标题】:defined class implementing interface in groovy but can't call method在groovy中定义类实现接口但不能调用方法
【发布时间】:2012-05-30 12:54:36
【问题描述】:

我在一个 grails 项目中使用 cuke4duke。 features/support/env.groovy 有

import org.openqa.selenium.htmlunit.HtmlUnitDriver
import org.openqa.selenium.JavascriptExecutor
import com.gargoylesoftware.htmlunit.BrowserVersion
import com.gargoylesoftware.htmlunit.ConfirmHandler
import com.gargoylesoftware.htmlunit.Page
...    
this.metaClass.mixin(cuke4duke.GroovyDsl)
...
public class ConfirmationHandler implements ConfirmHandler {

   boolean handleConfirm(Page page, String message) {
      called = true
      if (text == null || text.length()==0) {
         return answer
      }
      if (message.contains(text)) {
         return answer
      } 
      throw new RuntimeException("Expected '${text}' in confirmation message but got '${message}'")    
   }
   public String text = null
   public boolean answer = false
   public boolean called = false
}
...
Before() {
...
     browser = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6)
     confirmation = new ConfirmationHandler()
     browser.setConfirmHandler((ConfirmHandler) confirmation) // ERROR !
...
}

似乎该类已正确编译,但 groovy 无法调用 setConfirmHandler,因为它需要一个 ConfirmHandler……但提供的对象的类实现了接口!我检查了“confirmation instanceof ConfirmHandler”打印为真。

注意:HtmlUnit 包是用 Java 编写的。

有什么想法吗? (这是堆栈跟踪的顶部)

[INFO] org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException:没有方法签名: org.openqa.selenium.htmlunit.HtmlUnitDriver.setConfirmHandler() 是 适用于参数类型:(ConfirmationHandler)值: [ConfirmationHandler@6c08bae7] (NativeException)

【问题讨论】:

    标签: java interface groovy cuke4duke


    【解决方案1】:

    你不需要这样做:

    browser.getWebClient().setConfirmHandler( confirmation )
    

    因为我看不到 HtmlUnitDriver 有一个setConfirmHandler 方法...

    【讨论】:

    • 是的,我错了,我在错误的类上调用了该方法。现在它可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多