简体   繁体   中英

How to create runnable jar for Selenium tests

I am using JUnit 5 and Selenium for my tests. I need to create a jar file, that when executed, opens the browser and performs the test cases.

I've found from this video: How to Create Runnable Jar File From Selenium Project that using TestNG you can create an object, set the desired test class and run the test class but I have not found an equivalent way of doing this with JUnit5

I know that with JUnit platform launcher you can use the following code to run test but this does not work when executing the jar file because the JUnit task does not execute the test itself, only captures the output. at least thats what I read from JUnit Launcher Task

public class TestExecutor {
    public static void main(String args[]) {
          final LauncherDiscoveryRequest request =
                LauncherDiscoveryRequestBuilder.request()
                        .selectors(selectClass(MyTest.class))
                        .build();
        final Launcher launcher = LauncherFactory.create();
        launcher.execute(request);
    }
}

Is there any way to create an executable jar file that opens the browsers and performs the selenium tests?

1.realize a task timer.(no code,maybe use quaze)

2.realize a web to manual trigger.(use springboot webmvc)

import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;

import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;

@RestController
@RequestMapping("/excutor")
public class ExcutorContrller {

    @GetMapping("/")
    public String excute(String packageName, String className) throws FileNotFoundException {
        new Thread(() -> {
            System.out.println(packageName + "======" + className);
            LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
                    .request()
                    .selectors(
                            selectPackage(packageName)
//                        selectClass(com.wusuiwei.cases.JunitTest.class)
                    )
                    .filters(
                            includeClassNamePatterns(".*Test")
                    )
                    .build();
            Launcher launcher = LauncherFactory.create();
            // Register a listener of your choice
            SummaryGeneratingListener listener = new SummaryGeneratingListener();
            launcher.registerTestExecutionListeners(listener);
            launcher.execute(request);

            TestExecutionSummary summary = listener.getSummary();
            System.out.println(summary);
            // Do something with the TestExecutionSummary.
        });

        return "excuting....";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM