【问题标题】:How to pass parameter to ant scripts?如何将参数传递给ant脚本?
【发布时间】:2012-06-11 21:05:50
【问题描述】:

最近我正在研究 selenium webdriver 2.0(开发自动化框架)。根据每个故障的要求,必须捕获屏幕截图(文件路径和文件名:./screenshots/testcases/ddmmyyyy/scenario_hhmmss.png)但是我已经捕获了屏幕截图。当我运行这些整个测试套件时(我想生成 JUNIT 报告,这样转发必须有截图链接。)现在的问题是截图路径是动态生成的(通过 selenium java 代码),并且在 Junit 报告中我想建立超链接到最近生成的屏幕截图(我已经使用我们可以创建链接更新了 frames-report.xslt 文件,但它是硬编码的)?请提出任何方法吗?

这是我的 build.xml 文件的一部分

<target name="exec" depends="compile">
        <delete dir="${report}" />
    <mkdir dir="${report}" />
        <mkdir dir="${report}/xml" />
    <junit printsummary="yes" haltonfailure="no">
         <classpath refid="project-classpath" />
        <classpath>
                        <pathelement location="${bin}" />
                        <fileset dir="${lib}">
                            <include name="**/*.jar" />
                        </fileset>
                    </classpath>
        <test name="com.example.tests.NormanTestSuite" haltonfailure="no" todir="${report}/xml" outfile="TEST-result">          
        <formatter type="xml" />
        </test>         
    </junit>
    <junitreport todir="${report}">
            <fileset dir="${report}/xml">
                <include name="TEST*.xml" />
            </fileset>
    <report styledir="C:\apache-ant-1.8.4\custom" format="frames" todir="${report}/html" >          
    </report>
    </junitreport>
</target>

【问题讨论】:

    标签: ant


    【解决方案1】:

    使用 Java 系统属性

    您可以将变量作为 JVM 参数传递。假设你有一个像这样定义的名为“screenShotRoot”的变量

    ant -DscreenShotRoot=/screenshots/testcases
    

    您可以像这样在 build.xml 中阅读它

    <property name="screenshot.root" value="${screenShotRoot}" />
    

    然后,您的 ANT 任务可以使用此根路径在预期的日期为您的 PNG 文件生成适当的路径。

    看到这个Apache ANT FAQ page

    使用环境变量

    您还可以通过在调用脚本之前设置它们来使用操作系统环境变量。假设您在 Windows 上定义了一个名为“screenShotRoot”的环境变量

    SET screenShotRoot=/screenshots/testcases
    

    您可以像这样在 build.xml 中阅读它

    <property environment="env"/>
    <property name="screenshot.root" value="${env.screenShotRoot}" />
    

    使用属性文件

    您还可以将链接写入 ANT 脚本加载的属性文件中,如下所示

    <property file="build.properties"/>
    

    【讨论】:

    • 第一种情况下不需要&lt;property environment="env"/&gt;env 用于使用环境变量时。但是您正在展示如何使用 Java 系统属性 (-Dkey=value) 传递值。 Ant 可以直接访问这些变量 (${screenShotRoot})。
    • 谢谢,我已经用你们的 cmets 更新了我的答案,提供了 3 个单独的选项
    【解决方案2】:

    根据JUnitReport task 的文档,您可以使用report 元素上的嵌套param 标记来传递XSL 参数。

    从 Ant 1.7 开始,report 标签支持嵌套的 param 标签。这些标签可以 将 XSL 参数传递给样式表。

    所以你可以像这样将参数值传递给样式表:

    <report styledir="C:\apache-ant-1.8.4\custom" format="frames" todir="${report}/html" >          
        <param name="screenshots_link" expression="${screenshots.link}"/>
    </report>
    

    我不清楚你的问题。我想你说你已经支持你的 XSL 样式表中的参数。无论如何,这里是您如何使用它的摘要:

    <xsl:stylesheet>
    
        <!-- declare the parameter you will pass. Could also define a default value -->
        <xsl:param name="screenshot_link"/>
    
    
        <xsl:template>
    
            <!-- use the parameter value -->
            <xsl:value-of select="$screenshot_link"/>
    

    【讨论】:

    • 嗨,我的查询是,我将在一个测试套件中执行多个测试用例,在这种情况下,我认为很难为每个链接定义属性。我认为您的方法适用于单个测试用例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多