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!

Appium for the beginners. How to start with mobile automation?

Appium for the beginners. How to start with mobile automation?

Hello friends,

I work as a software test engineer already more than 6 years. And during these 5 years, I understood one thing – if You want to start to learn something new from the beginning, then You will Google for it. And as a result – You will find a lot of information without examples and simple explanation or You will find nothing at all, except some raw documentation.

I found that topics about mobile automation sometimes scare the people. So, I want to reveal the myths about mobile automation.

This article describes the simplest way of how to setup and run Your first test. An example is based on Appium 1.5.3 and Android system. It’s not about advanced usage of the latest version, etc.

What You need for starting with mobile automation

There are few thing that You need to have.

  1. Computer. MacBook is preferable to perform automation testing for iOS also.
  2. Pre-installed Android SDK or Android Studio is even better. Download here.
  3. Configured environment like for Selenium WEB testing.
  4. Appium desktop application latest version (1.5.3). Download here.

Base configuration

After downloading of Android Studio and setup of Android SDK You need to add the path to the folder with android SDK to the PATH of environments.

Run 1st test

  1. Create simple maven project in Eclipse or IntelliJ
  2. Add Appium dependency:
    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>2.2.0</version>
    </dependency>
  3. Download test application that is used as an example. You can find it here or on Play Store here
  4. Create test class based on JUnit test framework and past the code inside:
    public class PresentationTest {
    
        private static final String MYREACTIONS_ID = "com.denyszaiats.myreactions:id/";
        protected static DesiredCapabilities capabilities;
    
        @Test
        public void testPresentationTapGame() throws InterruptedException, MalformedURLException {
            capabilities = new DesiredCapabilities();
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("platformVersion", "6.0");
            capabilities.setCapability("deviceName", "emulator-5554");
            capabilities.setCapability("app", {PATH_TO_APK_FILE});
            AppiumDriver driver = new AndroidDriver(
                    new URL("http://0.0.0.0:4723/wd/hub"),
                    capabilities
            );
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.findElementById(MYREACTIONS_ID + "imageLangEn").click();
            driver.findElementById(MYREACTIONS_ID + "skip_login_button").click();
            driver.findElementById(MYREACTIONS_ID + "buttonGuideOk").click();
            driver.findElementById("android:id/home").click();
            driver.findElementById("android:id/home").click();
            driver.findElementByXPath("//*[@text='Crazy Fingers']").click();
            driver.findElementById(MYREACTIONS_ID + "buttonGuideOk").click();
            driver.findElementById(MYREACTIONS_ID + "handButton").click();
            driver.findElementById(MYREACTIONS_ID + "imageRightHand").click();
            driver.findElementById(MYREACTIONS_ID + "fingerButton").click();
            driver.findElementById(MYREACTIONS_ID + "imageIndexFinger").click();
            driver.findElementById(MYREACTIONS_ID + "startButton").click();
            WebElement tapButton = driver.findElementById(MYREACTIONS_ID + "imageTapButton");
            int x = tapButton.getLocation().x + 50;
            int y = tapButton.getLocation().y + 50;
            for (int i = 0; i < 350; i++) {
                driver.tap(1, x, y, 1);
            }
            String result = driver.findElementById(MYREACTIONS_ID + "resultsFasterClicker").getText();
            assertEquals(result, "350");
        }
    }
  5. Open Android simulator. Or create a new Android simulator. Or connect  Your Android test device. Do not forget to enable developer mode on Your real device.
  6. Open Appium desktop app and start it. Wait few seconds before Appium is ready to use. Run the test with JUnit.

 

This is it. Just several simple steps to run the simplest scenario with Appium.

I hope that now You’re confident with mobile automation and You know how to start with it.

Thanks for reading this article!

Advanced visual testing of Web and Mobile applications with Python

Advanced visual testing of Web and Mobile applications with Python

Who said that Python is not friendly with Front-end?

ITArray released the 1st version of Automotion-Python framework that can be used for visual validation of the web and mobile application.

You can find it by link https://github.com/ITArray/Automotion-Python

If You want to connect the library to Your project with “Tox” – simply add this dependency to Your tox.ini file:
http://github.com/ITArray/Automotion-Python/tarball/master#egg=Automotion

It’s super simply in usage:

from automotion import responsive_ui_validator
ui_validator = responsive_ui_validator.ResponsiveUIValidator(self.driver)
result = ui_validator.init("Open navigation drawer scenario").find_element(self.driver.find_element_by_name("Name"), "Name")\
    .same_size_as(self.driver.find_element_by_id("Id"), "Search Icon")\
    .same_offset_bottom_as(self.driver.find_element_by_id("Id"), "Search Icon")\
    .same_offset_top_as(self.driver.find_element_by_id("Id"), "Search Icon")\
    .not_overlap_with(self.driver.find_element_by_id("Id"), "Search Icon")\
    .inside_of(self.driver.find_element_by_id("Id"), "Toolbar")\
    .width_between(100, 200)\
    .height_between(100, 200)\
    .draw_map()\
    .validate()

ui_validator.generate_report("ReportToolbarIcons")

Leave Your feedback and comments. Also join and contribute into Automotion if You have ideas.
Thanks for reading my blog!

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 6 (Advanced verifications)

Automation testing: Selenium + Cucumber – Lesson 6 (Advanced verifications)

Hello friends!

Welcome to lesson #6!

In the previous lesson You learned how to develop simple verification methods, how to decorate them correctly to receive pretty results in case of failures. Today we will review very interesting and very useful topic regarding advanced verifications and how to implement it with Automotion.

So, we used to have the standard verifications, when we verify only that element is displayed or not. But usually we don’t do any validations that elements are aligned properly on the page. Nowadays we have desktop and mobile web application with responsive design. And correct alignment of elements on the page is super important part of business.

Let’s look this example on www.facey.top:

screen-shot-2016-10-23-at-10-50-33

We want to make sure that tabs “TOP”, “NEWEST” and “MY PHOTOS” have correct order and they are aligned properly. And also we need to validate that footer is placed somewhere under top header.

The second thing that we want to verify – that this home page has correct localisation language and the whole text is really in English. Not latino, but really in English.

You will find everything in attached video. Wish You nice watching!

Amazing!

Now we can cover our test suite that require to have some UI/UX and linguistic skills!

In this lesson You’ve learned very interesting things of how to make verification of proper alignment and orders of elements on the page and how to validate that page has correct localisation. Hope this information will help You in Your automation.

Do not miss the next lesson. We will review the implementation of action classes and some advanced usage of Automotion.

Wish You good luck! Have a nice automation!