Splitting and execution of JUnit test in multiple threads

Splitting and execution of JUnit test in multiple threads

In this article, I will describe and present some example of how it is possible to run JUnit or TestNG test in multiple threads.

Let’s think about the situation that, that there is the test, which needs to validate 10 locales in 10 languages. Locales and languages are always dynamically taken from somewhere.
In this case, the most common way is:

for (Locale locale: locales) {
    for (Language language: languages) {
        assertStuff(locale, language);
    }
}

The problem here is that complexity will be always On^2.

So, what we can do with this?

We have multithreading! Bingo!

So, we can modify an example to something like:

for (Locale locale: locales) {
    Thread t = new Thread(() -> {
        for (Language language: languages) {
            assertStuff(locale, language);
        }
    }

    t.start();
}

Now it looks much better. But the problem is that we will not be seeing the error, because of assertions work in the main thread only.

Let’s modify the code a little bit and collect all the threads into the list:

List threads = new ArrayList<>();
for (Locale locale: locales) {
    Thread t = new Thread(() -> {
        for (Language language: languages) {
            assertStuff(locale, language);
        }
    }

    threads.add(t);
    t.start();
}

Alright, now we have the list of triggered threads. But what to with that?

After some researching about Java concurrency, I found the way how to implement Concurrent Assertions

    
void assertConcurrent(final String message, final List<? extends Runnable> runnables, final int maxTimeoutSeconds) throws InterruptedException {
        final int numThreads = runnables.size();
        final List exceptions = Collections.synchronizedList(new ArrayList<>());
        final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
        try {
            final CountDownLatch allThreadsReady = new CountDownLatch(numThreads);
            final CountDownLatch afterInitBlocker = new CountDownLatch(1);
            final CountDownLatch allThreadsAreDone = new CountDownLatch(numThreads);
            for (final Runnable submittedTestRunnable : runnables) {
                threadPool.submit(() -> {
                    allThreadsReady.countDown();
                    try {
                        afterInitBlocker.await();
                        submittedTestRunnable.run();
                    } catch (final Throwable e) {
                        exceptions.add(e);
                    } finally {
                        allThreadsAreDone.countDown();
                    }
                });
            }
            assertTrue("Timeout during threads initializing threads.", 
                    allThreadsReady.await(runnables.size() * WAIT_MULTIPLIER, TimeUnit.MILLISECONDS));
            
            afterInitBlocker.countDown();
            assertTrue(message +" timeout! More than" + maxTimeoutSeconds + "seconds", 
                    allThreadsAreDone.await(maxTimeoutSeconds, TimeUnit.SECONDS));
        } finally {
            threadPool.shutdownNow();
        }
        assertTrue(message + "failed with errors" + exceptions, exceptions.isEmpty());
}

And then, the final test could be like:

List threads = new ArrayList<>();
for (Locale locale: locales) {
    Thread t = new Thread(() -> {
        for (Language language: languages) {
            assertStuff(locale, language);
        }
    }

    threads.add(t);
    t.start();
}
assertConcurrent("Failures are found", threads, 120); // 120 is max timeout of the test

The explanation of the Concurrent assertion is not difficult if You have an experience with Multithreading.
But even if not – feel free to use this code

Happy testing!

Automation testing: Selenium + Cucumber – Lesson 3 (First Selenium test)

Automation testing: Selenium + Cucumber – Lesson 3 (First Selenium test)

Hello friends!

Welcome to lesson 3!

Today we will review how to use Cucumber and Selenium wrapped in Automotion library. You will understand that to automate Your project is super easy.

Automotion is powerful Java library for automation testing that allows us easy start WebDriver without configuration of it. Also this library has many helpful features that will be very useful in the future.

Please, read carefully how to use this library here Automotion. It’s very easy but You need to know some instruction at least.

So, let’s start and to see how it goes!

Firstly, let’s remove not needed scenario that we created in previous lesson to keep our project clean. Also rename our feature file, feature name and scenario name.

We will automate Selenium-Cucumber scenario where we will open web-site

https://www.facey.top and verify that main logo exists.

Before the start be sure that You have installed Firefox and Chrome browsers.

To download the latest Chrome driver visit this web-site:

http://chromedriver.storage.googleapis.com/index.html

Also double check that You have in pom.xml dependency to Automotion library.

All next steps You’ll find in video. Have a nice watching!

Now You now how to create Your first Selenium scenario using Cucumber. It’s really easy, isn’t it?

We’re gonna review how to create and use Page Object in our next lesson. Each of the lessons is very important and helps to understand how everything works. Do not forget to practice.

Thanks for watching my lessons!

Have a good automation!

Automation testing: Selenium + Cucumber – Lesson 2 (Advanced usage of Cucumber)

Automation testing: Selenium + Cucumber – Lesson 2 (Advanced usage of Cucumber)

Hello guys!

Welcome to lesson 2!

It’s a very short lesson. Today we will review how to use tables inside of Cucumber scenarios and what are tags and how to use them. Please, do not skip first lessons to avoid any misunderstanding in the future.

So, what we gonna do today.

  • We will modify our scenario to operate not only values 2 and 3, but much more different numbers.
  • We will create another scenario to show You how to use tags in Cucumber scenario

Have a nice watching!

Now You know how to use Cucumber tables scenarios and how tags Your scenarios. In the next lesson we will start to learn how to use Selenium (Automotion) with Cucumber.

Thanks for watching us!

Have a good automation!

Advanced usage of Cucumber or Automotion multidriver scenarios

Advanced usage of Cucumber or Automotion multidriver scenarios

Hello guys,

How many times we had a discussion regarding what type of driver is better to use: headless PhantomJSDriver, local WebDriver or RemoteWebdriver.
Now it’s possible to make a configuration of Your project using Cucumber and Automotion library.
Here  is example of Maven project. There are 4 Cucumber feature files that will run the same test scenarios using different drivers: FirefoxDriver, Remote Chrome Driver, Remote Chrome Driver with mobile emulation of iPhone 6 and headless PhantomJSDriver.

Also there is example of how to perform the smart verification that web page has correct language using Automotion.

To run the tests – simply clone the project and run the test_run.sh file.

Thank You for Your attention and as always – have a good testing!