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!

Automotion – simple solution for complicated problems

Automotion – simple solution for complicated problems

In this article I decided review simple Java library for automation testing that is successfully implemented already for many different projects.

So, let’s start.

What are the main problems that we have when we want to start automation testing on a new project? Usually there are:

  1. Which technologies to choose? Java, Python, C#, Ruby or maybe try with JavaScript? Yeah, each of technologies has benefits and limitations. And usually we don’t know what we will need in the future.
  2. What kind of application do we want to test? Mobile or Web or maybe Mobile-Web? Maybe we will require to test RESTful services or mail services?
  3. Need to write many validators from scratch. Need to organise the structure of capabilities, drivers, getters and setters for configurations.

googley-eye-birdie-has-questions

So, I decided that need to write something that will help me to avoid such questions in the future. This “something” must be universal framework that can be easy connected to each project.

Now Automotion library is available on GitHub here

The main most useful functions that available in Automotion are:

  1. Super easy method for connection of WebDriver to the maven project. Support for Remote Web Driver and Appium Driver as well. Also it allows to run the tests in mobile simulation Chrome mode. You can update Your capabilities in the way You want. Also here is implemented methods for connection with SauceLabs.
  2. Powerful feature for performing HTTP(s) requests with attachments, authorisation, etc.
  3. Functions that allow to perform connection to IMAP mail services and verify mailboxes.
  4. Functions that allow to identify which of the languages are on the web and mobile pages. Support up to 70 languages.
  5. Smart text finder that allows to find even broken text. For example, You need to find the phrase “Try Again”, but You have recognised text like ” gh try gh 6%^7 hjgasd 7^& dfg!44d T@y GaIN#%hjk in”. People are able to find something similar to “Try Again”. It’s “T@y GaIN”. But machine cannot find it. So, this algorithm allows to find such phrases.
  6. Lots of useful helpers for the web and mobile automation testing:
    • Different variations of scrolling and swiping for iOS, Android and Web
    • Function that allow to hide keyboard on Android and iOS devices
    • Different smart functions for clicking on the web or mobile elements. For example, if You need to click on the right top corner of element
    • Functions for manipulating of network and settings on Android devices
  7. Smart functions for validation that elements on the page are aligned correctly. These functions are super useful for the validation of UI, especially header, footer and different static content.

All these functions could easy cover the most complicated cases in automation testing. With Automotion You don’t need anymore to look for the solutions. Everything is ready.

If You’re interesting to know more about Automotion library – feel free to contact with me directly. My email is available on the page About Me. Also, if You know another cases that could be added to Automotion – let me know or feel free to contribute on Github.

Thank You for reading this article and have a good automation!

10 tips and tricks in usage of Appium with iOS and Python

10 tips and tricks in usage of Appium with iOS and Python

Here is collected the list of tips and tricks how to solve some issue during automation of mobile iOS app with Appium.

1. Hide keyboard. It’s a simple method that easy work for Android but not so easy for iOS. So, the ways how to hide keyboard for iOS devices:

– self.driver.hide_keyboard(‘return’) – for iPhone will simulate the clicking on Return button. But sometimes this method is not hiding the keyboard but just switching to another text fields.

– Also for iPhone possible to simulate quick swipe from the middle of the screen to the bottom. I will close keyboard. This method work for 95% of cases.

– For iPad need to tap by coordinates on the bottom right side of the screen (minus 30 pixels for height and width).

2. How to send some keys without using of the known methods like set_value or send_keys using UI Automation calls? You need to click on the TextField to open a keyboard and then execute:

– self.driver.execute_script(

‘var vKeyboard = target.frontMostApp().keyboard(); vKeyboard.setInterKeyDelay(0.1);vKeyboard.typeString(“some string”);’

)

3. How to set the date in the PickerWheel. You don’t need to swipe nothing. Just take the location of the correct PickerWheel and perform the method send_keys(‘value’) to this Picker element. After setting the date to this element – question: how to close it? Just click somewhere on the screen (not on the Picker).

4. Everybody knows that method send_keys() works very slow. So, You need to use method set_value(). It will set the value of Text Field immediately. Or, You can use trick from the list-item #2, just set keyDelay very short.

5. How to verify 10 elements on the page (let’s say just simple static text or labels)? Usually  – we call find_element_by_..() for each of elements. But it will perform the call to Appium server, getting XML for each element. It’s very long. So, You need to grab the page source using self.driver.page_source and look for each element in the already received XML document.

6. How not to fight with popup on iOS app. Usually popups are very painful question. Especially – how to close them if it’s unpredictable. If You don’t need to to verify the text on popup or something like this – You need to add the next capabilities:

‘autoAcceptAlerts’: True, ‘acceptAllAlerts’: True

7. But how to deal with popup if You don’t want to close everything automatically? You need to wrap such 2 methods:

self.driver.switch_to_alert().accept()
self.event.accept_alert()

The accepting of alerts is very unstable, so be ready to wrap it into try-except block.

8. How to handle some of alerts using direct call to UI Automation:

self.driver.execute_script(‘var target = UIATarget.localTarget();’)
self.driver.execute_script(‘target.frontMostApp().alert().cancelButton().tap();’)

9. How to minimise the time of searching elements for Your framework? For Android everything is obviously – need to use ID. But what to do with iOS. The better way to decrease the time of element’s searching is the using of accurate xpaths. It’s not very clear from the perspective of  clean code, but it’s faster. So, if You don’t care about quality but about speed use the full xpaths (e.g. //UIAApplication[1]/UIAWindow[1]/…)

10. How to use the last version of Appium every time? You need to pull the Appium code from GitHub, run the reset.sh file to build it and enable in the Developer Settings of Appium. Now You have the latest version of Appium.

 

Sikuppium – how to combine Appium and Sikuli

Sikuppium – how to combine Appium and Sikuli

Sikuppium

Open source project which allows to mix Appium and Sikuli under one framework and easy manipulate by content and recognition of images. This is just a very nice helper in the automation testing to help test something that was left for manual visual testing. Now You can check without any problem in Your mobile automation project that correct Logo appears or correct photo is showed etc.

How to run

To run the sample test need to have pre-installed Maven 3 and Java 1.8 and need to execute next command from command line (example):

  • PLATFORM_NAME=”android” APPIUM_VERSION=”1.4.13″ NAME=”MyReaction” PLATFORM_VERSION=”4.4″ DEVICE=”Nexus 5″ APP=”/Developing/Android/SignedApp/MyReaction.apk” mvn clean install

Please, specify correct path to the application which You can find in the folder “app” under the main parent folder of the project.

Current example is adapted for the devices with resolution of display: 768×1280, 1080×1920, 1200×1920.

How to take a correct images

To take a correct screenshots need to use a tool SikuliX. You cannot take a screenshot directly from simulator screen. Only crop from the screenshot provided by WebDriver API in class DriverScreen.java (line 22)

Help and contact

Also You can find the application My Reaction on Play Store https://play.google.com/store/apps/details?id=com.denyszaiats.myreactions

Link to GitHub project – http://dzaiats.github.io/appium-sikuli-driver/

Smart text finder

This small but smart algorithm allows You to perform intelligent search for the words and phrases in the text. Imagine that You have recognised the text from the image. Usually there are many corrupted characters and it’s impossible to find something directly.

For example, You need to find phrase “Try Again”, but You have recognised text like ” gh try gh 6%^7 hjgasd 7^& dfg!44d T@y GaIN#%hjk in”. People are able to find something similar to “Try Again”. It’s “T@y GaIN”. But machine cannot find it. So, this algorithm allows to find such phrases.

The source code is here on GitHub

Here is algorithm:

public static boolean textIsFound(String pattern, String text) {
    pattern = pattern.toLowerCase();
    text = text.toLowerCase();

    if (text.contains(pattern)) {
        return true;
    } else if (text.replace(" ", "").contains(pattern.replace(" ", ""))) {
        return true;
    } else {
        String[] patternArr = pattern.split("\\W|\\s");
        String[] textArr = text.split("\\W|\\s");

        int matchTime = 0;
        int positionPrev = 0;
        for (String sp : patternArr) {
            for (String st : textArr) {
                if (st.equals(sp)) {
                    int positionNext = Arrays.asList(textArr).indexOf(st);
                    if (positionNext > positionPrev) {
                        positionPrev = positionNext;
                        matchTime++;
                        break;
                    }
                } else if (st.contains(sp)) {
                    matchTime++;
                    break;
                }
            }
        }

        boolean found = false;
        if (matchTime != patternArr.length) {
            String textJoined = String.join("", textArr);
            String patternJoined = String.join("", patternArr);
            String[] patternJoinedArr = patternJoined.split("");

            int position = 0;
            Map<Integer, String> positions = new TreeMap<Integer, String>();

            for (String s : patternJoinedArr) {
                while (position >= 0) {
                    position = textJoined.indexOf(s, position + 1);
                    if (position > -1) {
                        positions.put(position, s);
                    }
                }
                position = 0;
            }

            int count = 0;
            int countInLine = 1;

            for (Map.Entry entry : positions.entrySet()) {
                Integer i = (Integer) entry.getKey();
                if (positions.get(i + 1) != null && count < positions.size()) {
                    countInLine++;
                } else {
                    countInLine = 1;
                }
                if (countInLine >= patternJoined.length() * (1 - DERIVATION)) {
                    found = true;
                    break;
                }
                count++;
            }
        }

        return matchTime == patternArr.length || found;
    }
}

Enjoy it!