0

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!!!!!

Community
  • 1
  • 1
bcar
  • 815
  • 9
  • 19
  • Show us how you set `arg1` and arg2` – Alexander Pogrebnyak Jun 18 '13 at 13:41
  • I am trying to take the parameters from the ant command: ant param1 param2 and use those as ${arg1} or ${arg2} at this point, the code above is the only place $arg1 exists. – bcar Jun 18 '13 at 14:07
  • The Ant output shows `Unknown Source` as the origin of the NullPointerException. This means debugging information isn't being added to the TestMyTest.class file. In the `` task that compiles your test case, please add the following attribute: `debuglevel="lines,vars,source"`. Re-compile TestMyTest with Ant and update your post to reflect the new output. – Chad Nouis Jun 18 '13 at 15:19
  • The null pointer is occurring during run time. Its failing to get the system.getProperty(browser). that call is returning null. The javac task already has that info in it, but compile completes w/o error. – bcar Jun 18 '13 at 15:37
  • compile: [delete] Deleting directory /Users//automation/build [mkdir] Created dir: /Users//automation/build/classes [javac] Compiling 29 source files to /Users//automation/build/build/classes – bcar Jun 18 '13 at 15:38

2 Answers2

1

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>
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
  • Chad, thanks for the tips, however i am still getting null values when i try to retrieve the property in the java code. String browser = sys.getProperty("browser"); String host = sys.getProperty("host"); log.logInfo("The browser system property is set to: " + browser); log.logInfo("The host system property is set to: " + host);` – bcar Jun 18 '13 at 14:20
0

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!

bcar
  • 815
  • 9
  • 19