How to make an executable jar file using IntelliJ from a Selenium/TestNG java file? How to make an executable jar file using IntelliJ from a Selenium/TestNG java file? selenium selenium

How to make an executable jar file using IntelliJ from a Selenium/TestNG java file?


I finally figured it out for anyone else who happens to run into this problem, this is how I got the jar file to be created and run successfully...

I had to change my pom.xml file to the following:

<groupId>TestAutomation</groupId><artifactId>TestAutomation</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><url>http://maven.apache.org</url><build>    <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>        </plugin>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-jar-plugin</artifactId>            <configuration>                <archive>                    <manifest>                        <addClasspath>true</addClasspath>                        <mainClass>com.test.automation.Executable</mainClass>                    </manifest>                </archive>            </configuration>        </plugin>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>        </plugin>    </plugins></build><dependencies>    <dependency>        <groupId>org.seleniumhq.selenium</groupId>        <artifactId>selenium-java</artifactId>        <version>2.40.0</version>    </dependency>    <dependency>        <groupId>junit</groupId>        <artifactId>junit</artifactId>        <version>4.11</version>        <scope>test</scope>    </dependency>    <dependency>        <groupId>org.testng</groupId>        <artifactId>testng</artifactId>        <version>6.1.1</version>        <scope>test</scope>    </dependency></dependencies>

Then, I had to adjust my main method to not use any TestNG-related calls. For example, I could not use something like this for my main method:

    TestListenerAdapter tla = new TestListenerAdapter();    TestNG testng = new TestNG();    testng.setTestClasses(new Class[] {WordProfFonts2Set0.class});    testng.addListener(tla);    testng.run();

Finally, here are the steps to get the appropriate jar file created:

  1. Select File > Project Structure... from the top menu
  2. Select "Artifact" on the left menu and click the '+'
  3. Select Jar > From modules with dependencies...
  4. Select your main class using the browse button
  5. Click the radio button next to, "extract to the target jar" and click "OK"
  6. Click the '+' then select "Module Test Output"
  7. In the Available Elements pane to the right, expand the project name and select all the Maven files, then move them to the jar directory being created in the left pane
  8. Click "OK"
  9. Select Build > Build Artifacts... from the top menu
  10. Hover over the jar created and click "Build" under Actions

Notes:

  1. Be sure to add the IE or Chrome driver to your projects resources folder, and call it via the code folder, rather than the computer's hard drive. For example, do this:

    File file = new File("src\test\resources\binaries\IEDriverServer.exe");

Not this:

File file = new File("C:\\Users\\<Username>\\<Proj Name>\\src\\test\\java\\src\\  test\\resources\\binaries\\IEDriverServer.exe");

Then create the same directory with the driver in it in the same folder your jar is saved to on your computer:

srcTestAutomation.jar

2 . Be sure, if using IE, that Protected Mode is set for either all zones or none of them (in IE, go to Internet Options... > Security (tab) > Enable Protected Mode check box)