5

I am a noob in Java and automation so please bear with me if I get the terminology wrong.

I have a class file that can open a browser and navigate to google.com and then closes the browser. There’s another class file that does the same but goes to yahoo.com.

I can run this from eclipse and everything works fine.

However,  I need to know if I can run both the test cases one after the other without opening Eclipse and only by using batch files (.bat files) on Windows.

I would appreciate a clear step by step instruction. Thanks in advance.

3

5 Answers 5

3

I would suggest just exporting a jar file for your project. All the libraries will be packaged together in the jar files(including TestNG) and you can simply double click on the jar file to start your tests. Make a runner file that calls all the tests you have to run one by one.

External resources (if any), will have to be available for the jar file though. The external resources might include your test data (if any) or Portable browsers (example portable firefox).

Steps:

  • Right click on Project -> Export -> Runnable jar file
  • Give a name and file path for the jar file
  • Select option - Extract required libraries into generated JAR
  • And Finish

Troubleshooting:

Check the java version for the machines that you will be running your jar file on. Programs compiled with java 7 will mostly not run if the machine has java 6. Either compile with java 6 or update the jre on the target machines.

If the jar file does not launch, try using Jarfix.

2

I'm not sure what testing library (if any) you are using to execute the tests; I'm going to assume jUnit here, because that's what most people use for testing in Java. Similarly, I'm not sure if you're using ant or maven with this project.

Using JUnit

You can invoke the JUnit test runner from the command line using the following:

java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [classname] for JUnit 3.x, or

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [classname] for 4.x

However, this will only run one class full of tests, and you indicated wanting to run multiple. The way you string together multiple test classes into a single unit to execute in JUnit is by using a suite,like follows:

/**
 * All smoke tests in one easy suite!
 *
 */
@RunWith(Suite.class)
@SuiteClasses({ FooSmokeTests.class, 
    BarSmokeTests.class,
    BazSmokeTests.class})
public class AllSmokeTests {

}

Then you can run AllSmokeTests (which is the name of my test suite) with either of the above commands (depending on your JUnit version), and it will execute FooSmokeTests, BarSmokeTests, and BazSmokeTests in turn.

Using Maven

If your project is built with Maven, you can also execute the tests using maven itself. First, ensure that your pom.xml indicates which tests to run:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>   
        </executions>
        <configuration>
          <includes>
            <include>smokeTests/AllSmokeTests.java</include>
          </includes>
        </configuration>
      </plugin>

That "include" line tells it to run AllSmokeTests (the suite I made above) by default, and nothing else. You can have multiple includes here to run multiple files, or if you make a suite like I did, you can put the suite here. Or you can put multiple suites. The sky is the limit!

Then, from the command line, inside the directory where your pom.xml is, just run mvn test. This will build and run your tests.

Using ant

I always use Maven for my test projects, but ant should have similar capabilities using the jUnit task. I found the following on StackOverflow:

<property name="target.dir"   value="${basedir}/target"/>
<property name="main.destdir" value="${target.dir}/classes"/>
<property name="test.srcdir"  value="${basedir}/src/test/java"/>
<property name="test.destdir" value="${target.dir}/test-classes"/>
<!-- Execute all classfiles you've compiled in test-classes directory -->
<junit fork="true"
    maxmemory="128m"
    includeAntRuntime="true">
    <classpath>
        <pathelement path="${main.destdir}"/>
        <pathelement path="${test.destdir}"/>
    </classpath>
    <classpath refid="test.classpath"/>
    <formatter type="plain"/>
    <batchtest
        todir="${junit.batchtest.todir}">
        <fileset dir="${test.destdir}"/>
    </batchtest>
</junit>

This is set up to run every test class, which is a common setup; presumably, you could also target that to a specific test class and run that, or run multiple.

Using vanilla Java

If you're not using jUnit at all, but just have your code in a method, you can set up a main() method with the signature public static void main(String [] args). Then you can execute the code with something like java -jar myJar to execute the main method inside the compiled jar myJar. You might need to edit the manifest if eclipse didn't update it; see this documentation for more details.

1

From your question I'm assuming that you want to run your automation tests built using java one after the other using a batch file.

Yes, you can run the tests using a batch file. Open a notepad and add your tests (binaries) like below and save is as YourTestFilename.bat

echo off
% java C:/Test/Test1
% java C:/Test/Test2
% java C:/Test/Test3

Or

% java C:/Test/Test1
% java C:/Test/Test2
% java C:/Test/Test3

Double click the batch file. If you want to schedule it, you can use Task Scheduler in windows to run your batch file. Checkout the below links to see how to schedule a task.

http://windows.microsoft.com/en-au/windows/schedule-task#1TC=windows-7 http://www.7tutorials.com/how-create-task-basic-task-wizard

3
  • This answer is a little bit confusing; you don't run the Java command against .java files. There is a javax command to compile the Java files into class files, and then a java command to run the class files.
    – user246
    Commented May 14, 2015 at 11:40
  • @user246 thanks for pointing out. Modified answer.
    – PS86
    Commented May 15, 2015 at 1:08
  • The syntax is java -cp C:/TEST Test1.
    – user246
    Commented May 15, 2015 at 11:39
0

I have also create step by step guide video: I have created video how to run selenium webdriver test cases from command line using batch file.. please look in this link: https://www.youtube.com/watch?v=jpzI_-z3eQM Also you can visit my blog for step by step documentation: http://software-testing-easy.blogspot.in/

0

In eclipse you can export your test cases as runnable java jars. and using the following commands create a batch file to run them sequentially/simultaneously-

Sequentially:

java -jar pathToRunnableJars/test1.jar
java -jar pathToRunnableJars/test2.jar
java -jar pathToRunnableJars/test3.jar
java -jar pathToRunnableJars/test4.jar

Simultaneously:

start java -jar pathToRunnableJars/test1.jar
start java -jar pathToRunnableJars/test2.jar
start java -jar pathToRunnableJars/test3.jar
start java -jar pathToRunnableJars/test4.jar

Not the answer you're looking for? Browse other questions tagged or ask your own question.