Search code examples
javaeclipsejartestngprogram-entry-point

Run a testNG test suite with a jar / no runnable method


I need your help ! At work I'll need to automate tests to test an entire application. Point is, I am beginner at Java and at automation.

I am trying different tutorial and it works good. But now, I wanna progress. I will need to deliver my tests but as .jar. And it doesn't work, because i need a main class. I am trying to add it, but it doesn't work.

I UPDATED my scripts to correct and simplified them but I am still having some problems. Now I have :

A main class to launch the test :

package nouguierc.selenium;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;

public class TestRunner {

public static void main(String[] args) {
    System.out.println("Running tests!");
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] { RegistrationTest.class });
    testng.addListener(tla);
    testng.run();
}
}

I have the warning message : "The method addListener from the type testNG is deprecated"

I have also the test I want to exec (and put as a runnable jar also) :

package nouguierc.selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.annotations.Test;


public class RegistrationTest{

public  ExtentReports extent;
public  ExtentTest test;
public  int a = 1;
WebDriver driver=null;

@Test //This is TestNG annotation

public void testRegister(){

        extent = ExtentManager.Instance();


                    try{

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\nouguierc\\Desktop\\Projets\\Testing\\Airbus\\Automatisation\\testCélia\\WD_Automation\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://newtours.demoaut.com/");

        test = extent.startTest("VerificationReporting", "AlorsAlors");

        driver.findElement(By.linkText("REGISTER")).click();
        driver.findElement(By.name("firstName")).sendKeys("User1");
        driver.findElement(By.name("lastName")).sendKeys("Surname1");
        driver.findElement(By.name("phone")).sendKeys("123456789");
        driver.findElement(By.name("userName")).sendKeys("user1@test.com");
        driver.findElement(By.name("address1")).sendKeys("Test Address");
        driver.findElement(By.name("city")).sendKeys("Test City");
        Select select = new Select(driver.findElement(By.name("country")));
        select.selectByVisibleText("ANGOLA");
        driver.findElement(By.name("email")).sendKeys("user1@test.com");
        driver.findElement(By.name("password")).sendKeys("user1");
        driver.findElement(By.name("confirmPassword")).sendKeys("user1");
        driver.findElement(By.name("register")).click();

        if(a == 1)
            test.log(LogStatus.PASS, "OK" );
        else
            test.log(LogStatus.FAIL, "NOT OK" );

        driver.quit();

        }

        catch (Exception e) {
            System.out.println(e.getMessage());
            driver.quit();
        }

    extent.endTest(test);
    extent.flush();
    extent.close();
}   

}

And my POM.xml is :

<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>StackOverFlow</groupId>
<artifactId>StackOverFlow</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
    <maven.compiler.version>1.7</maven.compiler.version>
    <maven.compiler.target>1.7</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <sourceDirectory>src/test/java</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>nouguierc.selenium.TestRunner</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>nouguierc.selenium.TestRunner</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>2.41.2</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.9.13.6</version>
    </dependency>
</dependencies>
</project>

When I exec the test on Eclipse, I have this result :

Running tests!
[TestNG] Running:
  Command line suite


===============================================
Command line suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

And when I export the jar thanks to Eclipse and run it with the cmd I have :

no manifest attribute in ExecutableJAR.jar

The test is opening google chrome to the adress "data;" and doing nothing. Then I have the following failed result :

testRegister java.lang.NullPointerException
at nouguierc.selenium.RegistrationTest.testRegister(RegistrationTest.java:61)
at nouguierc.selenium.TestRunner.main(TestRunner.java:13)
... Removed 22 stack frames

When I remove the line "driver.quit();" in the catch. The test is passed successfully but that's not normal because it happend nothing in google chrome. It should be K.O.

Maybe someone can help me ?


Solution

  • You have to set the property for chromedriver and put chromedriver.exe at your project level.

    To run it as a jar you have to create a main class also :

    package com.stack.JarCreation;
    
    import org.testng.TestListenerAdapter;
    import org.testng.TestNG;
    
    public class Main {
    
        public static void main(String[] args) {
            TestListenerAdapter tla = new TestListenerAdapter();
            TestNG testng = new TestNG();
            testng.setTestClasses(new Class[] { RunnableJar.class });
            testng.addListener(tla);
            testng.run();
        }
    }
    

    enter image description here

    package com.stack.JarCreation;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.Select;
    import org.testng.annotations.Test;
    
    public class RunnableJar {
        public int a = 1;
    
        @Test
        public void testRegister() {
            WebDriver driver=null;
            try {
                System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
                driver = new ChromeDriver();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.manage().window().maximize();
                driver.get("http://newtours.demoaut.com/");
                driver.findElement(By.linkText("REGISTER")).click();
                driver.findElement(By.name("firstName")).sendKeys("User1");
                driver.findElement(By.name("lastName")).sendKeys("Surname1");
                driver.findElement(By.name("phone")).sendKeys("123456789");
                driver.findElement(By.name("userName")).sendKeys("user1@test.com");
                driver.findElement(By.name("address1")).sendKeys("Test Address");
                driver.findElement(By.name("city")).sendKeys("Test City");
                Select select = new Select(driver.findElement(By.name("country")));
                select.selectByVisibleText("ANGOLA");
                driver.findElement(By.name("email")).sendKeys("user1@test.com");
                driver.findElement(By.name("password")).sendKeys("user1");
                driver.findElement(By.name("confirmPassword")).sendKeys("user1");
                driver.findElement(By.name("register")).click();
    
                if (a == 1)
                    System.out.println("OK");
                else
                    System.out.println("NOT OK");
                driver.quit();
            }
    
            catch (Exception e) {
                System.out.println(e.getMessage());
                driver.quit();
            }
        }
    }
    

    If it is not a maven project , convert it into maven project by Right clicking on your project and go to configure->convert to Maven project.

    And replace your pom.xml with below file.

        <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>StackOverFlow</groupId>
        <artifactId>StackOverFlow</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.stack.JarCreation.Main</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>attached</goal>
                            </goals>
                            <phase>package</phase>
                            <configuration>
                                <descriptorRefs>
                                    <descriptorRef>jar-with-dependencies</descriptorRef>
                                </descriptorRefs>
                                <archive>
                                    <manifest>
                                        <mainClass>com.stack.JarCreation.Main</mainClass>
                                    </manifest>
                                </archive>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.9.13.6</version>
            </dependency>
        </dependencies>
    </project>
    

    Create the Jar for Launch Configuration as Main-StackOverflow and put jar and chromedriver.exe at same location.