How to pass parameter to ant scripts?

ant
36,078

Solution 1

Using Java System Property

You can pass a variable as a JVM argument. Assuming you have a variable named "screenShotRoot" defined like this

ant -DscreenShotRoot=/screenshots/testcases

you can read it in your build.xml like this

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

Your ANT task can then use this root path to generate appropriate paths to your PNG files on the date expected.

See this Apache ANT FAQ page

Using Environment Variables

You can also use Operating System environment variables, by setting them before calling your script. Assuming you have an environment variable named "screenShotRoot" defined like this on Windows

SET screenShotRoot=/screenshots/testcases

you can read it in your build.xml like this

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

Using Properties Files

You could also write your links into a properties file that your ANT script loads, like this

<property file="build.properties"/>

Solution 2

According to the documentation of the JUnitReport task, you can pass XSL paramemeters using a nested param tag on the report element.

Since Ant 1.7 the report tag supports nested param tags. These tags can pass XSL parameters to the stylesheet.

So you could pass the parameter value to the stylesheet something like this:

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

I wasn't clear from your question. I think you said you have already supported the parameter in your XSL stylesheet. Anyway, here's a summary of how you can use it:

<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"/>
Share:
36,078
Priyank Shah
Author by

Priyank Shah

Updated on June 12, 2020

Comments

  • Priyank Shah
    Priyank Shah almost 4 years

    Recently I am working on selenium webdriver 2.0 (developing automation framework). As per requirement for every faiulre the screenshot must be capture (file path and filename: ./screenshots/testcases/ddmmyyyy/scenario_hhmmss.png) however I already capture screenshots. when i run these entire test suite (I want to generate JUNIT report such that the repost must have screenshot link.)Now problem is that the screenshot path is generate dynamically (By selenium java code), and in Junit report i want to establish hyperlink to recently generated screenshots (i already updated frames-report.xslt file using we can create link but it was hardcoded)? Please suggest any way to do so?

    Here is some part of my build.xml file

    <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>
    
  • ewan.chalmers
    ewan.chalmers almost 12 years
    No need for <property environment="env"/> in your first case. env is for when you use environment variables. But you are showing how to pass values using Java System Property (-Dkey=value). Ant can access such variables directly (${screenShotRoot}).
  • Priyank Shah
    Priyank Shah almost 12 years
    hi, my query is, i am going to execute multiple test cases within one test suite,in that case i think it is difficult to define property for each links.i think your approach will fine for single test case.
  • Brad
    Brad almost 12 years
    Thanks, I have updated my answer with your comments to provide 3 separate options