简体   繁体   English

在多个驱动程序中执行Selenium和JUnit并行测试方法

[英]Selenium and JUnit parallel test method execution in multiple drivers

NOW: I have test project. 现在:我有测试项目。 Using maven-failsave-plugin I run jetty, then deploy there war and execute test scripts. 我使用maven-failsave-plugin运行jetty,然后在那里部署war并执行测试脚本。 TODO: I need to run in parallel in 5 webdrivers one method same time. TODO:我需要同时在5个Webdriver中并行运行一种方法。 Or execute the same test class in 5 drivers same time. 或者在5个驱动程序中同时执行相同的测试类。 It's like performance testing using Selenium ( bad choice, but it's requirement). 这就像使用Selenium进行性能测试(不好的选择,但这是必需的)。

Thats how looks my ParrallelParametrized class. 那就是我的ParrallelParametrized类的外观。

public class ParallelParametrized extends Parameterized {
    private static class ThreadPoolScheduler implements RunnerScheduler {

        private ExecutorService executor;

        public ThreadPoolScheduler() {
            String threads = System.getProperty("web.driver.amount");
            int numThreads = Integer.parseInt(threads);
            executor = Executors.newFixedThreadPool(numThreads);
        }

        @Override
        public void finished() {
            executor.shutdown();
            try {
                executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
            } catch (InterruptedException exc) {
                throw new RuntimeException(exc);
            }
        }

        @Override
        public void schedule(Runnable childStatement) {
            executor.submit(childStatement);
        }
    }

    public ParallelParametrized(Class klass) throws Throwable {
        super(klass);
        setScheduler(new ThreadPoolScheduler());
    }

    public void filter(Filter filter) throws NoTestsRemainException {
        super.filter(new FilterDecorator(filter));
    }

    private static class FilterDecorator extends Filter {
        private final Filter delegate;

        private FilterDecorator(Filter delegate) {
            this.delegate = delegate;
        }

        @Override
        public boolean shouldRun(Description description) {
            return delegate.shouldRun(wrap(description));
        }

        @Override
        public String describe() {
            return delegate.describe();
        }
    }

    private static Description wrap(Description description) {
        String name = description.getDisplayName();
        String fixedName = deparametrizedName(name);
        Description clonedDescription =
                Description.createSuiteDescription(fixedName,description.getAnnotations().toArray(new Annotation[0]));
        for(Description child : description.getChildren()){
            clonedDescription.addChild(wrap(child));
        }
        return clonedDescription;
    }

    private static String deparametrizedName(String name) {
        if(name.startsWith("[")){
            return name;
        }
        int indexOfOpenBracket = name.indexOf('[');
        int indexOfCloseBracket = name.indexOf(']')+1;
        return name.substring(0,indexOfOpenBracket).concat(name.substring(indexOfCloseBracket));
    }
}

And that is my Abstact Test Class 那是我的抽象测验班

public abstract class AbstractParallelBaseTest {

    public static ThreadLocal<WebDriver> CURRENT_DRIVER = new ThreadLocal<WebDriver>();

    public static List<WebDriver> DRIVERS_TO_CLEANUP = Collections.synchronizedList(new ArrayList<WebDriver>());
    public static final long TIMEOUT = 100;


    protected static final Log LOGGER = LogFactory.getLog(AbstractParallelBaseTest.class);

    static class WebDriverFactory {
        static WebDriver create() {
            WebDriver driver = null;
            try {
                LoggingPreferences logs = new LoggingPreferences();
                logs.enable(LogType.BROWSER, Level.OFF);
                logs.enable(LogType.CLIENT, Level.OFF);
                logs.enable(LogType.DRIVER, Level.OFF);
                logs.enable(LogType.PERFORMANCE, Level.OFF);
                logs.enable(LogType.PROFILER, Level.OFF);
                logs.enable(LogType.SERVER, Level.OFF);

                DesiredCapabilities desiredCapabilities;
                String webDriver = System.getProperty("web.driver");

                if ("chrome".equals(webDriver)) {
                    desiredCapabilities = DesiredCapabilities.chrome();
                    desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
                    desiredCapabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxy));
                    driver = new ChromeDriver(desiredCapabilities);
                } else if ("ie".equals(webDriver)) {
                    desiredCapabilities = DesiredCapabilities.internetExplorer();
                    desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                    desiredCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
                    desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
                    desiredCapabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxy));
                    driver = new InternetExplorerDriver(desiredCapabilities);
                } else if ("firefox".equals(webDriver)) {
                    desiredCapabilities = DesiredCapabilities.firefox();
                    desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
                    driver = new FirefoxDriver(desiredCapabilities);
                } else {
                    desiredCapabilities = DesiredCapabilities.htmlUnitWithJs();
                    desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);
                    driver = new HtmlUnitDriver(desiredCapabilities);
                }

                driver.manage().window().setPosition(new Point(0, 0));
                driver.manage().window().setSize(new Dimension(1650, 1080));
                driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
                driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return driver;
        }
    }

    @Parameterized.Parameter
    public int currentDriverIndex;

    @Parameterized.Parameters(name = "{0}")
    public static ArrayList<Object> driverTypes() {
        return Lists.newArrayList(getParams());
    }
}

On each implementation of tests I paste @RunWith(ParallelParametrized.class) and it works! 在测试的每个实现中,我都粘贴@RunWith(ParallelParametrized.class),它可以工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 JUnit 5 并行执行测试用例 - Parallel test case execution with JUnit 5 getGauge:如何在并行方案执行中管理单独的硒驱动器? - getGauge: how to manage separate selenium drivers in parallel scenario execution? Selenium Grid 2并行测试用例执行 - Selenium Grid 2 parallel Test Case execution 使用 Junit 5 Selenium 并行执行的失败测试的屏幕截图 - Screenshot on failed tests using Junit 5 Selenium parallel execution 单一静态方法是否会导致并行执行中的测试失败 - selenium - Does single static method can cause the test failure in parallel execution - selenium Gradle 工具 API + JUnit 5 并行测试执行异常 (LocationAwareException) - Gradle tooling API + JUnit 5 parallel test execution exceptions (LocationAwareException) Cucumber JVM 4.0.0和Junit测试运行程序不会发生并行执行 - Parallel Execution not happening with Cucumber JVM 4.0.0 and Junit test runner JUnit 5 + Cucumber 并行测试执行不适用于文件安全(Maven) - JUnit 5 + Cucumber parallel test execution doesn't work with filesafe (Maven) 在多个环境中执行 JUnit 测试的配置 - Configuration for JUnit test execution on multiple environments Selenium webdriver:多个驱动程序 - Selenium webdriver : Multiple drivers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM