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!

Mobile automation of native Android application with WebView container

Mobile automation of native Android application with WebView container

Hello friends!

Most of engineers started to learn mobile automation with Appium. This is the most popular framework for mobile automation testing and I think that everybody agree with this. But sometimes it’s not enough to have only Appium, especially if we want to test native Android application with only WebView container inside. Appium cannot recognise elements in WebView container.

What to do in this case? Some test engineers decided to postpone with automation. But in reality this automation is even much easier than testing of native application with native controls.

How WebView container works? It’s loading mobile version of web site into custom WebView form. It means that we can easy inspect elements with Chrome or Firefox inspector.

But how to say Appium to search for elements exactly in this WebView? Many of You guys heard that need to switch context. But what if You run Appium inspector and You see only 1 context called NATIVE_APP ? In this case You need to switch Appium to Selendroid mode. For UI Appium inspector this is here:

screen-shot-2016-10-27-at-09-12-16
But if we want to run the tests in Selendroid mode, we need to specify capability “automationName”: “Selendroid”.

After this You will see that You have more contexts than only NATIVE_APP, but also 1 or more WEBVIEW contexts. The next step is switching to correct context (for example):

driver.context("WEBVIEW_0").switchTo();

And that’s all. Now You can easy use the same methods like for Selenium WebDriver to navigate and click.
Also, You can use method .get(“url”) to easy navigate to any pages without clicking on UI.

Hope it will help You to solve some issues and start automation of hybrid apps.

Thanks for reading my blog!

Have a good automation!

Automation testing: Selenium + Cucumber – Lesson 5 (Base verifications)

Automation testing: Selenium + Cucumber – Lesson 5 (Base verifications)

Hello dear friends!

Welcome to lesson #5!

Today we will review one of the most important part of automation framework – verifications. Obviously that if our tests verify nothing – they are useless. So, this lesson will give You an understanding of how to create classes for verifications and how to customise them to have nice test results.

Some engineers likes to keep verifications in the body of unit tests, some of them would like to keep it in the same class with page objects. I would prefer to create separate package “verifications” and for each of our page object class create appropriate class for verifications.

Sometimes we need to have base class for verification to keep different methods for base verifications in this class.

Each of our verifications methods will return string with error messages if such were detected during verification.

So, let’s start to implement our first verification class.

Have a nice watching of video lesson!

Well, in this lesson You’ve learned how to create verifications, how to manage the readability of results and tests. It is super important part in creation of the framework for automation testing.

In the next lesson I will show You how to work with advanced verifications, when You want to verify language of the web page, or if elements are aligned correctly, etc. This information You will find in my next video.

Thank You for watching my channel.

Have a good automation!

Automotion – final realease

Automotion – final realease

Hello,

I’m very glad to announce that Automotion library is finally released to the central Maven repository http://search.maven.org/#search|ga|1|automotion

Simply add it to the project as a maven dependency:

<dependency>

    <groupId>net.itarray</groupId>

    <artifactId>automotion</artifactId>

    <version>1.2.0</version>

</dependency>

Also You can contribute to the project on GitHub

Read about latest updates of Automotion library on ITArray

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!

 

Verification of email boxes as a part of automation testing

Verification of email boxes as a part of automation testing

Greetings!

In this article I would like to show some example of how to check email boxes in automation testing using Java instruments.
So, let’s think about how many times You had to develop test scenarios of user registration, activation using Selenium or Appium. And in 90% it’s very important to verify e-mail box that welcome message is received or something like this. Usually engineers use services like Mailinator or similar to check the mailbox. But such services are not free. And not secure.

I will show how to check even private Gmail box in automation testing using IMAP protocol. The main instrument is native java library javax.mail.*

1. Implement login

public void login(String host, int port, String username, String password) throws Exception {
            String fName = getFolderName(folderName);
            URLName url = new URLName(protocol, host, port, fName, username, password);

            if (session == null) {
                        Properties props = null;
                        try {
                                   props = System.getProperties();
                        } catch (SecurityException sex) {
                                   props = new Properties();
                        }
                        session = Session.getInstance(props, null);
             }
             store = session.getStore(url);
             store.connect();
             folder = store.getFolder(url);

            folder.open(Folder.READ_WRITE);
 }
2. Get messages

public Message[] getMessages() throws MessagingException {
             return folder.getMessages();
}

Also it’s possible to verify if user is connected, possible to logout from the session.
The wrapper for using of such mail services  is implemented in Automotion library

The methods that could be accessible from the code are:

MailService mailService = new MailService();
mailService
           .setFolder(MailService.MailFolder.INBOX) // (INBOX, SPAM, TRASH)
           .login(String IMAP_Server, int IMAP_Port, String email, String passwordToEmail);

mailService.isLoggedIn()) - boolean
mailService.getMessageCount() - integer
mailService.getMessages() - Message[]
mailService.getLastMessage() - Message - last message

Now You know how to perform the verification that email was successfully sent and received.

Thanks for reading my articles! Have a good automation!