0

I run my tests through either feature file or runner class. I am not able to create a Jar as i dont have a main method in my project . Please let me know how to create a jar file using this or if I need to add anything .

I want to execute feature scenarios in server so to achieve that I want to create an exe of my current project (Cucumber with selenium webdriver, Java using eclipse IDE and Maven as Build tool) , but unable to since I do not have a main method in my project . Please let me know how to create a jar file using this or if I need to add any other plugins and also how to add main method here .

    This is my POM.xml from the project :

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.cucumber</groupId>
        <artifactId>BasePolicy</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>

        <name>BasePolicy</name>
        <url>http://maven.apache.org</url>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-java</artifactId>
                <version>1.2.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-picocontainer</artifactId>
                <version>1.2.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>1.2.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>jdom</groupId>
                <artifactId>jdom</artifactId>
                <version>1.1</version>
            </dependency>
        </dependencies>

    </project>

    --------------------------------------------------------------------
    and My java runner class : 

    package com.cucumber.testcases;

    import java.io.File;
    import java.util.concurrent.TimeUnit;

    import org.junit.Before;
    import org.junit.runner.RunWith;
    import org.openqa.selenium.phantomjs.PhantomJSDriver;

    import com.cucumber.Base.BaseDriver;

    import cucumber.api.CucumberOptions;

    import cucumber.api.junit.Cucumber;

    @RunWith(Cucumber.class)
    @CucumberOptions(
            // format = {"pretty","html:target/html/" } ,
            features = "src/test/java")
    public class RunnerClass {

        }
user3548850
  • 73
  • 1
  • 2
  • 12

1 Answers1

0

You can create a main method to run cucumber by calling the static main method of Main.class (Lots of mains in one sentence).

Create a new class and add the following main method to it.

public static void main(String[] args) throws Throwable {

            Main.main(new String[]{"-g", "classpath to step definition file", "Full path to feature file"});

          // My stepdefinition is inside java package at cucumber.sample.test
          // My feature file is inside src/test/resources/features/sample.feature

        }

For additional parameters like tags or plugin use "-t","@Tags". Important the feature file path has to be the last option.

You can configure this further to make it more flexible by passing these parameters to this class when you run it. You will need to play around with the args argument in your code.

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • i created the class file , as below and it runs fine , but on converting to Jar file it does not run at all , I have also added the dependent jars . Please let me know what can be done . public class NewMain { public static void main(String[] args) throws Throwable{ Main.main(new String[]{"-g", "com.cucumber.testcases", "src/com/cucumber/Features/PolicyFeatures.feature"}); }} – user3548850 Jun 08 '17 at 07:40
  • What is the exception you are getting? – Grasshopper Jun 08 '17 at 08:49
  • not getting any exception . It works fine in eclipse but on creating jar , doesn't launch the test cases. – user3548850 Jun 08 '17 at 10:11
  • You may have an issue with your classpath or feature files are not found. - https://stackoverflow.com/questions/18413014/run-a-jar-file-from-the-command-line-and-specify-classpath. – Grasshopper Jun 08 '17 at 10:39