我在grails项目中使用cuke4duke。该功能/支持/ env.groovy有在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 !
...
}
看来类才能正确编译,但常规不能称之为setConfirmHandler,因为它需要一个ConfirmHandler ...但所提供的对象的类实现接口!我检查了“ConfirmHandler的确认实例”打印为真。
注意:HtmlUnit包是用Java编写的。
任何想法? (这是堆栈跟踪的顶部)
[INFO] org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: org.openqa.selenium.htmlunit:方法的无签名。 HtmlUnitDriver.setConfirmHandler()是 适用于参数类型:(ConfirmationHandler)值: [ConfirmationHandler @ 6c08bae7(NativeException)
是的,我的错误,我是在错误的类上调用方法。现在它可以工作。 – carlosayam