如何触发htmlunit中的keyup事件
1个回答
2016-01-03 · 知道合伙人互联网行家
关注
展开全部
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)
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)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询