0

I have created a testng test using selenium webdriver and java. Now I don't want to share my code to different users but I want my code to run by different users using jar or war file. Can anyone help me out with this. Is it possible to run test without sharing testNG java code?

zishan paya
  • 503
  • 1
  • 6
  • 29
  • Test code does not belong in an executable package like a JAR or WAR. – duffymo Oct 05 '15 at 12:28
  • Would this be of some use: http://www.toolsqa.com/selenium-webdriver/testng-testcase/? – Luke Melaia Oct 05 '15 at 12:29
  • Duffymo, so I cannot run my test without codes? Is there any way out, I don't want to share my complete framework. Luke, I am aware of testng test case and but want to know if I can run my test using jar or war file. – zishan paya Oct 05 '15 at 12:55
  • compile your testng tests using build tools like ant and then delete the src files(java files) and use ant to run to the class files generated during compile.you can now send the project to the client for which only .class files will be available.Kindly get back if u need more details – Vicky Oct 05 '15 at 13:24
  • Sounds interesting Vicky, can give more detail on this ant part? is it also possible with maven, if yes please give more details on it? – zishan paya Oct 05 '15 at 13:58

2 Answers2

0

I haven't got an opportunity to work with maven but it should also be possible with maven

Let me tell you the idea by taking an example using ant

Assume you are running your tests using ant with the below build.xml(sample file change according to your needs) file

<project name="TestNG Demo" default="clean" basedir=".">  

<!-- ========== Initialize Properties =================================== -->
    <property environment="env"/>

    <property name="ws.home" value="${basedir}"/>
    <property name="ws.jars" value="${ws.home}/Jars"/>
    <property name="test.dest" value="${ws.home}/build"/>
    <property name="test.src" value="${ws.home}/src"/>
    <property name="ng.result" value="test-output"/>

    <target name="setClassPath" unless="test.classpath">
        <path id="classpath_jars">
            <fileset dir="${ws.jars}" includes="**/*.jar"/>
            <pathelement path="${ws.jars}"/>
        </path>
        <pathconvert pathsep=":" 
            property="test.classpath" 
            refid="classpath_jars"/>
    </target>

    <target name="init" depends="setClassPath">
        <tstamp>
            <format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
        </tstamp>
        <condition property="ANT" 
            value="${env.ANT_HOME}/bin/ant.bat" 
            else="${env.ANT_HOME}/bin/ant">
                    <os family="windows" />
        </condition>
        <taskdef name="testng" classpath="${test.classpath}"
               classname="org.testng.TestNGAntTask" />

    </target>

    <!-- all -->
    <target name="all">
    </target>

    <!-- clean -->
    <target name="clean">
        <delete dir="${test.dest}"/>
    </target>

    <!-- compile -->
    <target name="compile" depends="init, clean" > 
        <delete includeemptydirs="true" quiet="true">
            <fileset dir="${test.dest}" includes="**/*"/>
        </delete>
        <echo message="making directory..."/>
        <mkdir dir="${test.dest}"/>
        <echo message="classpath------: ${test.classpath}"/>
        <echo message="compiling..."/>
        <javac 
            includeantruntime="false"
            debug="true" 
            destdir="${test.dest}" 
            srcdir="${test.src}" 
            target="1.6" 
            classpath="${test.classpath}"
        >
        </javac>
      </target>

    <!-- build -->
    <target name="build" depends="init">
    </target>

    <!-- run -->
    <target name="run" >
        <testng classpath="${test.classpath}:${test.dest}" suitename="suite">   
            <xmlfileset dir="${ws.home}" includes="testng.xml"/>
        </testng>
    </target>

    <target name="usage">
        <echo>
            ant run will execute the test
        </echo>
    </target>

    <path id="test.c">
            <fileset dir="${ws.jars}" includes="*.jar"/>
    </path>

      <target name="makexsltreports">
            <mkdir dir="${ws.home}/XSLT_Reports/output"/>

            <xslt in="${ng.result}/testng-results.xml" style="src/demo/testng-results.xsl"
                  out="${ws.home}/XSLT_Reports/output/index.html" classpathref="test.c" processor="SaxonLiaison">
                <param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
                <param name="testNgXslt.showRuntimeTotals" expression="true"/>
            </xslt>
        </target>

    <!-- ****************** targets not used ****************** -->

</project>

Now after test compiles(use target ant compile to compile your tests) you will get the class files in build folder inside your project folder and now you can delete the src folder(your java files) and to use ant run to execute the tests(use target ant run) to run your tests.If your planning to give it your client then you can make a simple bat(windows) or shellscript(linux) to execute command ant run and on clicking on it the test will run

Hope it helps you..Kindly get back if you have any queries

Vicky
  • 2,999
  • 2
  • 21
  • 36
0

As @vicky suggests, look at Maven, as it'll allow you to package up your project with both a production WAR/JAR and also the tests packaged up into a test JAR. Specifically at the test-jar JAR type you can label JARs with.

So add this as a plugin to the pom.xml:

<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-jar-plugin</artifactId>
           <version>2.2</version>

           <executions>
               <execution>
                   <goals>
                       <goal>test-jar</goal>
                   </goals>
               </execution>
           </executions>
       </plugin>
    </plugins>
</build>

And then others can pull it as a dependency using:

 <dependency>
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>1.2.3</version>
<type>test-jar</type>
<scope>test</scope>

A bit more information here: How do I install a test-jar in maven?

Community
  • 1
  • 1