有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

运行jwebunit测试程序时出现java获取语法错误

这是我的密码

package net.sourceforge.jwebunit.sample;

import net.sourceforge.jwebunit.WebTestCase;


public class JWebUnitSearchExample extends WebTestCase {

    public JWebUnitSearchExample(String name) {
        super(name);
    }

    public void setUp() {
        getTestContext().setBaseUrl("http://www.google.com");
        getTestContext().setProxyName("some.proxyname.com.au");
        getTestContext().setProxyPort(8080);
    }

    public void testSearch() {
        beginAt("/");
        setFormElement("q", "httpunit");
        submit("btnG");
        clickLinkWithText("HttpUnit");
        assertTitleEquals("HttpUnit");
        assertLinkPresentWithText("User's Manual");
    }
}

运行它时,我发现以下语法错误:

SyntaxError: syntax error (httpunit; line 155)
    at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:597)
    at org.mozilla.javascript.TokenStream.reportSyntaxError(TokenStream.java:1324)
    at org.mozilla.javascript.Parser.reportError(Parser.java:73)
    at org.mozilla.javascript.Parser.primaryExpr(Parser.java:1411)
    at org.mozilla.javascript.Parser.memberExpr(Parser.java:1201)
    at org.mozilla.javascript.Parser.unaryExpr(Parser.java:1115)
    at org.mozilla.javascript.Parser.mulExpr(Parser.java:1062)
    at org.mozilla.javascript.Parser.addExpr(Parser.java:1045)
    at org.mozilla.javascript.Parser.shiftExpr(Parser.java:1032)
    at org.mozilla.javascript.Parser.relExpr(Parser.java:1015)
    at org.mozilla.javascript.Parser.eqExpr(Parser.java:1002)
    at org.mozilla.javascript.Parser.bitAndExpr(Parser.java:991)
    at org.mozilla.javascript.Parser.bitXorExpr(Parser.java:980)
    at org.mozilla.javascript.Parser.bitOrExpr(Parser.java:969)
    at org.mozilla.javascript.Parser.andExpr(Parser.java:957)
    at org.mozilla.javascript.Parser.orExpr(Parser.java:945)
    at org.mozilla.javascript.Parser.condExpr(Parser.java:928)
    at org.mozilla.javascript.Parser.assignExpr(Parser.java:909)
    at org.mozilla.javascript.Parser.expr(Parser.java:898)
    at org.mozilla.javascript.Parser.statementHelper(Parser.java:791)
    at org.mozilla.javascript.Parser.statement(Parser.java:387)
    at org.mozilla.javascript.Parser.parseFunctionBody(Parser.java:167)
    at org.mozilla.javascript.Parser.function(Parser.java:271)
    at org.mozilla.javascript.Parser.parseFunctionBody(Parser.java:165)
    at org.mozilla.javascript.Parser.function(Parser.java:271)
    at org.mozilla.javascript.Parser.primaryExpr(Parser.java:1265)
    at org.mozilla.javascript.Parser.memberExpr(Parser.java:1201)
    at org.mozilla.javascript.Parser.unaryExpr(Parser.java:1115)
    at org.mozilla.javascript.Parser.mulExpr(Parser.java:1062)
    at org.mozilla.javascript.Parser.addExpr(Parser.java:1045)
    at org.mozilla.javascript.Parser.shiftExpr(Parser.java:1032)
    at org.mozilla.javascript.Parser.relExpr(Parser.java:1015)
    at org.mozilla.javascript.Parser.eqExpr(Parser.java:1002)
    at org.mozilla.javascript.Parser.bitAndExpr(Parser.java:991)
    at org.mozilla.javascript.Parser.bitXorExpr(Parser.java:980)
    at org.mozilla.javascript.Parser.bitOrExpr(Parser.java:969)
    at org.mozilla.javascript.Parser.andExpr(Parser.java:957)
    at org.mozilla.javascript.Parser.orExpr(Parser.java:945)
    at org.mozilla.javascript.Parser.condExpr(Parser.java:928)
    at org.mozilla.javascript.Parser.assignExpr(Parser.java:909)
    at org.mozilla.javascript.Parser.argumentList(Parser.java:1152)
    at org.mozilla.javascript.Parser.memberExprTail(Parser.java:1240)
    at org.mozilla.javascript.Parser.memberExpr(Parser.java:1204)
    at org.mozilla.javascript.Parser.unaryExpr(Parser.java:1115)
    at org.mozilla.javascript.Parser.mulExpr(Parser.java:1062)
    at org.mozilla.javascript.Parser.addExpr(Parser.java:1045)
    at org.mozilla.javascript.Parser.shiftExpr(Parser.java:1032)
    at org.mozilla.javascript.Parser.relExpr(Parser.java:1015)
    at org.mozilla.javascript.Parser.eqExpr(Parser.java:1002)
    at org.mozilla.javascript.Parser.bitAndExpr(Parser.java:991)
    at org.mozilla.javascript.Parser.bitXorExpr(Parser.java:980)
    at org.mozilla.javascript.Parser.bitOrExpr(Parser.java:969)
    at org.mozilla.javascript.Parser.andExpr(Parser.java:957)
    at org.mozilla.javascript.Parser.orExpr(Parser.java:945)
    at org.mozilla.javascript.Parser.condExpr(Parser.java:928)
    at org.mozilla.javascript.Parser.assignExpr(Parser.java:909)
    at org.mozilla.javascript.Parser.expr(Parser.java:898)
    at org.mozilla.javascript.Parser.statementHelper(Parser.java:791)
    at org.mozilla.javascript.Parser.statement(Parser.java:387)
    at org.mozilla.javascript.Parser.parseFunctionBody(Parser.java:167)
    at org.mozilla.javascript.Parser.function(Parser.java:271)
    at org.mozilla.javascript.Parser.primaryExpr(Parser.java:1265)
    at org.mozilla.javascript.Parser.memberExpr(Parser.java:1201)
    at org.mozilla.javascript.Parser.unaryExpr(Parser.java:1115)
    at org.mozilla.javascript.Parser.mulExpr(Parser.java:1062)
    at org.mozilla.javascript.Parser.addExpr(Parser.java:1045)
    at org.mozilla.javascript.Parser.shiftExpr(Parser.java:1032)
    at org.mozilla.javascript.Parser.relExpr(Parser.java:1015)
    at org.mozilla.javascript.Parser.eqExpr(Parser.java:1002)
    at org.mozilla.javascript.Parser.bitAndExpr(Parser.java:991)
    at org.mozilla.javascript.Parser.bitXorExpr(Parser.java:980)
    at org.mozilla.javascript.Parser.bitOrExpr(Parser.java:969)
    at org.mozilla.javascript.Parser.andExpr(Parser.java:957)
    at org.mozilla.javascript.Parser.orExpr(Parser.java:945)
    at org.mozilla.javascript.Parser.condExpr(Parser.java:928)
    at org.mozilla.javascript.Parser.assignExpr(Parser.java:909)
    at org.mozilla.javascript.Parser.expr(Parser.java:898)
    at org.mozilla.javascript.Parser.primaryExpr(Parser.java:1368)
    at org.mozilla.javascript.Parser.memberExpr(Parser.java:1201)
    at org.mozilla.javascript.Parser.unaryExpr(Parser.java:1115)
    at org.mozilla.javascript.Parser.mulExpr(Parser.java:1062)
    at org.mozilla.javascript.Parser.addExpr(Parser.java:1045)
    at org.mozilla.javascript.Parser.shiftExpr(Parser.java:1032)
    at org.mozilla.javascript.Parser.relExpr(Parser.java:1015)
    at org.mozilla.javascript.Parser.eqExpr(Parser.java:1002)
    at org.mozilla.javascript.Parser.bitAndExpr(Parser.java:991)
    at org.mozilla.javascript.Parser.bitXorExpr(Parser.java:980)
    at org.mozilla.javascript.Parser.bitOrExpr(Parser.java:969)
    at org.mozilla.javascript.Parser.andExpr(Parser.java:957)
    at org.mozilla.javascript.Parser.orExpr(Parser.java:945)
    at org.mozilla.javascript.Parser.condExpr(Parser.java:928)
    at org.mozilla.javascript.Parser.assignExpr(Parser.java:909)
    at org.mozilla.javascript.Parser.expr(Parser.java:898)
    at org.mozilla.javascript.Parser.statementHelper(Parser.java:791)
    at org.mozilla.javascript.Parser.statement(Parser.java:387)
    at org.mozilla.javascript.Parser.parse(Parser.java:128)
    at org.mozilla.javascript.Context.compile(Context.java:2053)
    at org.mozilla.javascript.Context.compile(Context.java:2006)
    at org.mozilla.javascript.Context.compileReader(Context.java:899)
    at org.mozilla.javascript.Context.evaluateReader(Context.java:817)
    at org.mozilla.javascript.Context.evaluateString(Context.java:784)
    at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.executeScript(JavaScript.java:122)
    at com.meterware.httpunit.scripting.ScriptableDelegate.runScript(ScriptableDelegate.java:64)
    at com.meterware.httpunit.parsing.ScriptFilter.getTranslatedScript(ScriptFilter.java:150)
    at com.meterware.httpunit.parsing.ScriptFilter.endElement(ScriptFilter.java:131)
    at org.cyberneko.html.HTMLTagBalancer.callEndElement(Unknown Source)
    at org.cyberneko.html.HTMLTagBalancer.endElement(Unknown Source)
    at org.cyberneko.html.HTMLScanner$SpecialScanner.scan(Unknown Source)
    at org.cyberneko.html.HTMLScanner.scanDocument(Unknown Source)
    at org.cyberneko.html.HTMLConfiguration.parse(Unknown Source)
    at org.cyberneko.html.HTMLConfiguration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at com.meterware.httpunit.parsing.NekoHTMLParser.parse(NekoHTMLParser.java:41)
    at com.meterware.httpunit.HTMLPage.parse(HTMLPage.java:244)
    at com.meterware.httpunit.WebResponse.getReceivedPage(WebResponse.java:1014)
    at com.meterware.httpunit.WebResponse$Scriptable.load(WebResponse.java:611)
    at com.meterware.httpunit.javascript.JavaScript$Window.initialize(JavaScript.java:424)
    at com.meterware.httpunit.javascript.JavaScript.run(JavaScript.java:80)
    at com.meterware.httpunit.javascript.JavaScriptEngineFactory.associate(JavaScriptEngineFactory.java:46)
    at com.meterware.httpunit.RequestContext.runScripts(RequestContext.java:44)
    at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:119)
    at com.meterware.httpunit.WebWindow.sendRequest(WebWindow.java:107)
    at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:245)
    at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:224)
    at com.meterware.httpunit.WebForm.doFormSubmit(WebForm.java:75)
    at com.meterware.httpunit.SubmitButton.doButtonAction(SubmitButton.java:47)
    at com.meterware.httpunit.Button.click(Button.java:68)
    at net.sourceforge.jwebunit.HttpUnitDialog.submit(HttpUnitDialog.java:557)
    at net.sourceforge.jwebunit.WebTester.submit(WebTester.java:868)
    at net.sourceforge.jwebunit.WebTestCase.submit(WebTestCase.java:351)
    at net.sourceforge.jwebunit.sample.JWebUnitSearchExample.testSearch(JWebUnitSearchExample.java:32)
    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:585)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    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:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)

共 (2) 个答案

  1. # 1 楼答案

    import org.junit.Test;
    
    import static net.sourceforge.jwebunit.junit.JWebUnit.*;
    import net.sourceforge.jwebunit.util.TestingEngineRegistry;
    
    import org.junit.Before;
    
    
    public class JWebUnitSearchExample  {
    
    @Before
        public void setUp() {
    
            setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT);
    
            System.setProperty("proxyHost", "some.proxyname.com.au");
            System.setProperty("proxyPort", "8080");
            System.setProperty("proxySet", "true");
    
            setBaseUrl("http://www.google.com");
    
    
        }
    
    @Test
        public void testSearch() {
            beginAt("/");
            setFormElement("q", "httpunit");
            submit("btnG");
            clickLinkWithText("HttpUnit");
            assertTitleEquals("HttpUnit");
            assertLinkPresentWithText("User's Manual");
        }
    }
    
  2. # 2 楼答案

    我认为你需要进口

    import net.sourceforge.jwebunit.junit.*;
    

    而不是

    import net.sourceforge.jwebunit.WebTestCase;