How to create a . JAR for test automation?

Asked

Viewed 505 times

1

Hello, I am creating automated scripts using Selenium Webdriver and I am running within the Intellij IDE. I would like to know how it is possible to create a . JAR file from my scripts, so it would not be necessary to open the IDE and run the script.

I researched a lot and found no solution, the closest I got was that they said that it is necessary to have the class "main", but in my scripts there is no main class.

My scripts were created in the : src/test/java path

Below follows the very basic script, just to open the browser, as I can generate a . JAR of this script ?

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

public class AbrirNavegador {
    private WebDriver navegador;

    @Test
    public void abrirNavegador() {

    //Abrindo o navegador

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Cesar\\Documents\\ChromeDriver\\chromedriver.exe");
    WebDriver navegador = new ChromeDriver();
    navegador.manage().window().maximize();
    navegador.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        navegador.get("http://www.google.com");
    }

}
  • You want to run the generated jar, or you want to use this jar in other tests?

  • Or you want to always run this test, regardless of the module in which it is?

  • I always want to run this test, is that I have several scripts that will never be changed, so they will always be executed the same way.

1 answer

1

I recommend you use the strategy of implementing the tests in a common module, and run a standard suite per module using @Suite:

// Teste padrão - NOTE que a classe é abstrata
public abstract class AbstractAbrirNavegadorTest {

    private WebDriver navegador;

    @Test
    public void abrirNavegador() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Cesar\\Documents\\ChromeDriver\\chromedriver.exe");
        WebDriver navegador = new ChromeDriver();
        navegador.manage().window().maximize();
        navegador.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        navegador.get("http://www.google.com");
    }
}

In the submodules you want to always run the test, include the common jar as dependency and create a concrete class for the test:

// Implementação do teste padrão em todos os submódulos necessários
public class AbrirNavegadorTest extends AbstractAbrirNavegadorTest {

}

Still in the sub-module, create a test suite by running the concrete class(s) (s):

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
  AbrirNavegadorTest.class
  // Inclua aqui os testes padrões a serem executados
})
public class TestesPadroes {
    // Executa a suite com todos os testes padrões
}
  • But this procedure will generate the file . JAR ?

  • I figured it might be something a bit simple, looking for the IDE I saw that has the option of "Build", which by what I researched would be to create the correct executable ?

  • No, this procedure creates a reusable test to be reused in N modules. Creating JAR depends on how you are packaging today (Maven, Gradle...)

  • My packaging is being in Maven.

  • Then you must generate one test-jar of this module

Browser other questions tagged

You are not signed in. Login or sign up in order to post.