1

我对 ant 完全陌生,我想在 Linux RedHat 机器上启动 Selenium。当我尝试使用此构建脚本编译我的 java 文件时遇到问题。为什么总是说“找不到符号”?

构建.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Mink Admin GUI Tests" default="run" basedir=".">

    <!-- Property: Selenium Server -->
    <property name="selenium.port" value="4444"/>
    <property name="report.dir" value="${basedir}\reports"/>
    <property name="testng.dir" value="${basedir}\test-output"/>
    <property name="classes.dir" value="${basedir}\bin"/>
    <property name="src.dir" value="${basedir}\src"/>

    <!-- Inlcude all libs and java classes -->
    <path id="libs">
        <pathelement path="${basedir}/libs/junit/junit-4.11.jar"/>
        <pathelement path="${basedir}/libs/selenium-client/selenium-java-2.33.0.jar"/>
        <pathelement path="${basedir}/libs/selenium-server/selenium-server-standalone-2.33.0.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/apache-mime4j-0.6.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/bsh-1.3.0.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/cglib-nodep-2.1_3.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/commons-codec-1.6.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/commons-collections-3.2.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/commons-exec-1.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/commons-io-2.2.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/commons-jxpath-1.3.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/commons-lang3-3.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/commons-logging-1.1.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/cssparser-0.9.9.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/guava-14.0.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/hamcrest-core-1.3.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/htmlunit-2.12.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/htmlunit-core-js-2.12.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/httpclient-4.2.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/httpcore-4.2.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/httpmime-4.2.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/ini4j-0.5.2.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/jcommander-1.29.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/jetty-websocket-8.1.8.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/jna-3.4.0.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/jna-platform-3.4.0.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/json-20080701.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/junit-dep-4.11.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/nekohtml-1.9.17.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/netty-3.5.7.Final.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/operadriver-1.3.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/phantomjsdriver-1.0.3.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/protobuf-java-2.4.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/sac-1.3.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/serializer-2.7.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/testng-6.8.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/xalan-2.7.1.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/xercesImpl-2.10.0.jar"/>
        <pathelement path="${basedir}/libs/selenium-libs/xml-apis-1.4.01.jar"/>
    </path>

    <!-- Initial start target - run the test -->
    <target name="run" description="Initial target of this build script">
        <echo>Let's start!</echo>
        <sequential>
            <antcall target="init"/>
            <echo taskname="waitfor" message="(...waiting for selenium server launch...)"/>
            <waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
                <http url="http://localhost:${selenium.port}/selenium-server/driver/?cmd=testComplete"/>
            </waitfor>
            <antcall target="tests"/>
            <antcall target="stop-server"/>
        </sequential>
    </target>

    <!-- Start server and compile Java files -->
    <target name="init">
        <echo>Setting up environment...</echo>
        <delete dir="logs"/>
        <mkdir dir="logs"/>
        <delete dir="${classes.dir}"/>
        <mkdir dir="${classes.dir}"/>
        <delete dir="${report.dir}"/>
        <mkdir dir="${report.dir}"/>
        <delete dir="${testng.dir}"/>
        <java jar="${basedir}/libs/selenium-server/selenium-server-standalone-2.33.0.jar" fork="true" spawn="true">
            <arg line="-timeout 30 -port ${selenium.port} -log logs/seleniumStart.log -browserSideLog"/>
        </java>
        <echo>Compiling java files...</echo>
        <javac debug="true" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="libs" encoding="UTF-8"></javac>
    </target>

    <!-- Run the testng.xml -->
    <target name="tests" description="Run TestNG">
        <echo>Let's run the Test Cases...</echo>
        <testng outputDir="${report.dir}" haltonfailure="false" classpathref="libs">
            <xmlfileset dir="." includes="tests.xml"/>
        </testng>
    </target>

    <!-- Stop the Selenium server -->
    <target name="stop-server">
        <echo>Stopping the Selenium Server...</echo>
        <get taskname="selenium-shutdown" src="http://localhost:${selenium.port}/selenium-server/driver/?cmd=shutDownSeleniumServer" dest="logs/seleniumStop.log" ignoreerrors="true"/>
    </target>
</project>

输出:

Buildfile: build.xml

run:
     [echo] Let's start!

init:
     [echo] Setting up environment...
   [delete] Deleting directory /opt/scp/pkg/logs
    [mkdir] Created dir: /opt/scp/pkg/logs
   [delete] Deleting directory /opt/scp/pkg/bin
    [mkdir] Created dir: /opt/scp/pkg/bin
   [delete] Deleting directory /opt/scp/pkg/reports
    [mkdir] Created dir: /opt/scp/pkg/reports
     [echo] Compiling java files...
    [javac] Compiling 1 source file to /opt/scp/pkg/bin
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:23: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isTextPresent("Sign in to AdminGUI"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:69: cannot find symbol
    [javac] symbol  : method verifyEquals(java.lang.String,java.lang.String)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyEquals("e2e", selenium.getText("css=div.rf-cp-lbl-colps"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:70: cannot find symbol
    [javac] symbol  : method verifyEquals(java.lang.String,java.lang.String)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyEquals("Customers", selenium.getText("css=span.menu-item.menu-item-selected"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:71: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isElementPresent("link=Mobile Users"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:72: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isElementPresent("link=Certificates"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:73: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isElementPresent("link=Provisioning Adapter"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:74: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isElementPresent("link=Admin Users"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:75: cannot find symbol
    [javac] symbol  : method verifyEquals(java.lang.String,java.lang.String)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyEquals("Customer List", selenium.getText("id=breadcrumbs_body"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:90: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isTextPresent("e2e"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:93: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isTextPresent("Sign in to AdminGUI"));
    [javac]             ^
    [javac] /opt/scp/pkg/src/com/xxx/yyy/test/Login.java:94: cannot find symbol
    [javac] symbol  : method verifyTrue(boolean)
    [javac] location: class com.xxx.yyy.test.Login
    [javac]             verifyTrue(selenium.isTextPresent("Please authenticate based on your username"));
    [javac]             ^
    [javac] 11 errors

BUILD FAILED
/opt/scp/pkg/build.xml:58: The following error occurred while executing this line:
/opt/scp/pkg/build.xml:82: Compile failed; see the compiler error output for details.
4

1 回答 1

0

从这个页面引用:

在编译 Java 程序时,编译器会创建一个所有正在使用的标识符的列表。如果它找不到标识符所指的内容(例如,没有变量的声明语句),它就无法完成编译。这就是cannot find symbol错误消息的意思,它没有足够的信息来拼凑 Java 代码想要执行的内容。

可能的原因:
- 尝试使用变量而不声明它。
- 拼写错误的类或方法名(记住,Java 区分大小写)。
- 使用的参数与方法的签名不匹配。
- 未使用导入声明正确引用打包的类。


在您的情况下,verifyTrue(..)andverifyEquals(..)方法可能:
- 未正确定义(方法名称冲突),或
- 定义在classimport编辑的 a 中;相关jar未包含在类路径中(很可能

于 2014-06-20T20:09:36.027 回答