如何触发htmlunit中的keyup事件

 我来答
建孤云UJ
2016-01-03 · 知道合伙人互联网行家
建孤云UJ
知道合伙人互联网行家
采纳数:1928 获赞数:12561
入职之后主要从事网站优化、网络营销。自媒体运营一类的工作。

向TA提问 私信TA
展开全部
package test;
import java.net.URL;

import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
import junit.framework.TestCase;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.host.Event;
public class CopyOfTest extends TestCase {
public void testHomePage(){
try{
// 新建一个WebClient对象,此对象相当于浏览器
final WebClient webClient = new WebClient(
BrowserVersion.FIREFOX_10);
// 构造一个URL,指向需要测试的URL,如http://www.iteye.com
System.out.println("Test1");
URL url = new URL("http://www.baidu.com/");
// 通过getPage()方法,返回相应的页面
final HtmlPage page = webClient.getPage(url);
//System.out.println(page.asXml());
HtmlElement hi=(HtmlElement) page.getDocumentElement().getElementById("kw");
hi.setAttribute("value","aa");
Event event=new Event();
event.setEventType(Event.TYPE_KEY_UP);
HtmlPage hpm=hi.click(event);
hpm.toString();
System.out.println(hpm.getDocumentElement().getElementById("st").asText());
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

异常:
Test1
java.lang.RuntimeException: Unable to find window associated with Event keyup (Current Target: null);
at com.gargoylesoftware.htmlunit.javascript.SimpleScriptable.getWindow(SimpleScriptable.java:307)
at com.gargoylesoftware.htmlunit.javascript.SimpleScriptable.getWindow(SimpleScriptable.java:293)
at com.gargoylesoftware.htmlunit.javascript.SimpleScriptable.getBrowserVersion(SimpleScriptable.java:386)
at com.gargoylesoftware.htmlunit.javascript.host.Event.isAborted(Event.java:586)
at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:869)
at com.gargoylesoftware.htmlunit.html.HtmlElement.doClickFireClickEvent(HtmlElement.java:1294)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1239)
at test.CopyOfTest.testHomePage(CopyOfTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式