简体   繁体   中英

How to pass ant parameters to junit/selenium tests?

Before i ask my question let me say, i've read through these related questions:

Here is what i'm looking to accomplish:

  1. call ant script like: ant -Dhost="ip addy" -Dbrowser="chrome"
  2. in the ant script set the properties to arg1 and arg2
  3. in the webdriver test call, System.getProperty("key"); and use those values set to determine what Driver is loaded.

here is what i have right now:

from build.xml

<!-- language: lang-xml -->

<!-- TARGET: Run JUNIT Tests  depends on remove and compile-->

    <target name="junit" depends="remove,compile">
        <!-- remove junit dir -->
        <delete dir="${junit.output.dir}"/>
        <!-- make junit dir -->
        <mkdir dir="${junit.output.dir}"/>
        <junit fork="yes">
            <formatter type="xml"/>
            <formatter type="plain" usefile="false"/>
            <sysproperty key="host" value="${arg1}"/>
            <sysproperty key="browser" value="${arg2}"/>
            <test name="${test.dir}.TestMyTest" todir="${junit.output.dir}"/>
            //.......

java code in setup():

if (System.getProperty("browser").equals("firefox")) {
    log.logInfo("Firefox driver initialized");
    driver = new FirefoxDriver();
} else if (System.getProperty("browser").equals("ie") //....

When i execute ant -Dhost=ip -Dbrowser=firefox i am getting NPE's. I assume it has to do with my build script and setting those properties. I am under the impression i set this wrong.

[junit] Testcase: testMyTest took 0.001 sec
[junit]     Caused an ERROR
[junit] null
[junit] java.lang.NullPointerException
[junit]     at tests.TestMyTest.setUp(Unknown Source)
[junit]

My assumption here is System.getProperty("browser") is return null. Any assistance would be much appreciated!

Thanks!!!!!

There are no properties named arg1 and arg2 . Instead, refer to the user properties by name:

<!-- Verify the properties exist. -->
<fail unless="host"/>
<fail unless="browser"/>

<junit fork="yes">
    <!-- ... -->
    <sysproperty key="host" value="${host}"/>
    <sysproperty key="browser" value="${browser}"/>
    <!-- ... -->
</junit>

Figured out my own coding mistake along with the help from @Chad Nouis it is now resolved.

Properties sys = new Properties();

should have been:

Properties sys = System.getProperties();

Chad Thanks again for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM