Table of Contents

Extensions (Static Methods)

Due to the way we structure our framework, we can create static methods that we can use as extensions on methods involving different object types, such as tapping or long-pressing on a MobileElement, swiping from one element to another, explicitly waiting for an AppiumDriver, or waiting for a MobileElement to be displayed or clickable etc.

MobileElement Extensions

We will begin each method name with ‘me’ (for MobileElement).

meWaitForSeconds() Static Method

  1. Open up the ‘MobileElementExtensions’ class in the ‘utils.extensions’ package and add the following static method for making the Driver wait for a specified number of seconds…
  2. Make sure the import for ‘WebDriverWait’ and the static import for appDriver() method have been added above the class…
  3. Add a static integer variable set to a value of 10 for ‘sec’, and make it private to the class it is in, just above the weWaitForSeconds() method…

meElementIsDisplayed() Static Method

  1. Add the following static method in the ‘MobileElementExtensions’ class to check that a given MobileElement is displayed, after waiting the specified amount of time from the weWaitForSeconds() method …
  2. Also make sure the import for ‘ExpectedConditions’ is added above the class, as well as an import for ‘WebElement’…

meElementToBeClickable() Static Method

  1. Add the following static method in the ‘MobileElementExtensions’ class to check that a given MobileElement is clickable, after waiting the specified amount of time from the meWaitForSeconds() method…

meTap() Static Method

  1. Add the following static method in the ‘MobileElementExtensions’ class to wait for a given MobileElement to be clickable/tappable, highlight the clickable/tappable MobileElement, and then tap the MobileElement (this is better than simply clicking as on mobile the more realistic action would actually be tapping)…

    For the above to work, we need to add the following static imports…

    We also need to make sure we import the TouchAction class…

    We then need to add a a private static TouchAction object in the top of our class (this way we can reuse the TouchAction class in other similar methods)…

meLongPress() Static Method

  1. Add the following method for long pressing on a Mobile element for two seconds, and then releasing your finger from it…

    For the above to work, we need to add two more static imports for all Duration methods (* is wildcard for all) and LongPressOptions longPressOptions method

meSwipeFromElementToElement() Static Method

  1. Add the following static method in the ‘MobileElementExtensions’ class to check that the elements to swipe to and from are clickable and highlighted, and then press down on one element and swipe it to another element and then release the finger / touch…

meSwipeDirection() Static Methods

  1. Add the following DIRECTION enum to the MobileExtensions class…
  2. Add the following method which uses a switch case with the enum we made to swipe in any given direction…

meSendKeys() Static Method

  1. Add the following static method in the ‘MobileElementExtensions’ class to wait for a given textfield MobileElement to be displayed, clear the textfield if necessary (if ‘clearFirst’ is true), and then input a given String into the textfield…

meElementIsInvisible() Static Method

  1. Add the following static method in the ‘MobileElementExtensions’ class to wait for a given MobileElement to not be visible anymore…

meGetAttribute() Static Method

  1. Add the following static method in the ‘MobileElementExtensions’ class to find a specified attribute of a given element…

Espresso Specific Mobile Commands

Espresso is an Android test automation library maintained by Google. It has a number of advantages, for example built-in view synchronisation that ensures element finding happens during idle periods in your app.

We can use Espresso framework with our Appium driver when running tests. Espresso contains certain mobile commands / gestures which are specific to the framework. The ‘EspressoExtensions’ class will contain static methods for each of these Espresso specific mobile commands.

First things first, let’s make this ‘EspressoExtensions’ class extend ‘MobileElementExtensions’ class and also add a private static AndroidDriver variable and set it to what’s returned from our ‘appDriver()’ method (making sure we add the correct static import), like below…

We will being each method name with ‘es’ (as it is specific to Espresso mobile gestures).

esSwipe() Static Methods

esSwipe(String direction) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class in the ‘utils.extensions’ package to swipe the whole screen in a given direction…
  2. Also make sure the following imports are added…

esSwipe(String direction, MobileElement element) Static Method

This is an overloaded version of the above method that also takes a given element that can be swiped on specifically.

  1. Add the following overloaded static method in the ‘EspressoExtensions’ class to swipe a given element in a given direction…

    Also make sure the following import is added for MobileElement…

esIsToastVisible(String text, boolean isRegEx) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to check whether a toast message containing given text (and whether it is Regular Expression or not) is visible…

esOpenDrawer() Static Methods

esOpenDrawer(MobileElement element) Static Method

  1. Add the following overloaded static method in the ‘EspressoExtensions’ class to open the drawer for a given element…

esOpenDrawer(MobileElement element, int gravity) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to open the drawer for a given element with a given amount of gravity

esCloseDrawer() Static Methods

esCloseDrawer(MobileElement element) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to close the drawer for a given element…

esCloseDrawer(MobileElement element, int gravity) Static Method

  1. Add the following overloaded static method in the ‘EspressoExtensions’ class to close the drawer for a given element with a given amount of gravity

esSetDate(MobileElement element, int year, int monthOfYear, int dayOfMonth) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to set the date on a given DatePicker element…

esSetTime(MobileElement element, int hours, int minutes) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to set the time on a given TimePicker element…

esNavigateTo(MobileElement element, int menuItemId) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to navigate to a given menu item in a given navigation element (the navigation element must be a child of a DrawerLayout, of type NavigationView, visible on screen and displayed on screen)…

esScrollToPage() Static Methods

esScrollToPage(MobileElement element, String scrollTo, boolean smoothScroll) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to scroll to a given page (first, last, left or right) smoothly or not, using a given element…

esScrollToPage(MobileElement element, int ScrollToPage) Static Method

  1. Add the following static method in the ‘EspressoExtensions’ class to scroll to a given page number, using a given element…

Note: The whole list of Espresso mobile commands can be found at http://appium.io/docs/en/commands/mobile-command/#android-espresso-only

WebDriver Extensions

We will begin each method name with ‘wd’ (for WebDriver).

wdHighlight() Static Method

  1. Open up the ‘WebDriverExtensions’ class in the ‘utils.extensions’ package and add the following static method for highlighting a given locator using JavaScript…
  2. You will notice that an error is thrown due to not knowing what ‘wdHighlightedColour’ is, so let’s add that in the ‘Settings’ class of the ‘utils.selenium’ package (of course you can also customise this to change the colour and size of the border etc.)…
  3. Next, go back to the ‘WebDriverExtensions’ class and add a static import for ‘wdHighlightedColour’ in the ‘Settings’ class…
  4. Of course, also check that the imports for ‘WebDriver’, ‘WebElement’, ‘JavaScriptExecutor’ and ‘By’ have been added above the class, as well as a static import again for the browser() method in the ‘Drivers’ class of the ‘utils.selenium’ package…

wdElementIsDisplayed() Static Method

  1. Open up the ‘WebDriverExtensions’ class in the ‘utils.extensions’ package and add the following static method for checking that a highlighted locator is displayed…
  2. Make sure the import for ‘WebDriverWait’ and ‘ExpectedConditions’ have been added above the class…
  3. Add a static integer variable set to a value of 10 for ‘sec’, and make it private to the class it is in, just above the weWaitForSeconds() method…

wdElementIsInvisible() Static Method

  1. Add the following static method in the ‘WebDriverExtensions’ class to wait for a given locator to not be visible…

wdFindElement() Static Method

  1. Add the following static method in the ‘WebDriverExtensions’ class to find a displayed element by its locator…

wdClick() Static Method

  1. Add the following static method in the ‘WebDriverExtensions’ class to click on an element which has been found by its locator…

wdSendKeys() Static Method

  1. Add the following static method in the ‘WebDriverExtensions’ class to wait for a given textfield element (which has been found by its locator) to be displayed, clear the textfield if necessary (if ‘clearFirst’ is true), and then input a given String into the textfield…

Refactoring our Test Scenario to use the Extensions

We can now use our extensions (static methods) to replace previous lines of code in our test methods.  Over time, this helps us follow DRY principles (Don’t Repeat Yourself) as well as make our code easier to read 🙂

‘skipWelcomeScreen()’ method

  1. Open up the ‘NavigationScenarios.feature’ file, right-click on the ‘And the welcome screen is skipped without logging in’ step, and select ‘Go To’ –> ‘Declaration’
  2. Right-click on the ‘.skipWelcomeScreen()’ method call and select ‘Go To’ –> ‘Declaration’ again
  3. Edit the ‘skipWelcomeScreen()’ method so it uses the meTap() static method from the ‘MobileElementExtensions’ class (you will have to add a static import to access all the static methods in the MobileElementExtensions class)…

‘navToHomeTab()’ method

  1. Open up the ‘NavigationScenarios.feature’ file again, right-click on the ‘When I view the Home tab’ step, and select ‘Go To’ –> ‘Declaration’
  2. Right-click on the ‘.navToHomeTab()’ method call and select ‘Go To’ –> ‘Declaration’ again
  3. Edit the ‘navToHomeTab()’ method so it uses the meTap() static method from the ‘MobileElementExtensions’ class again (you will have to add a static import to access all the static methods in the MobileElementExtensions class)…

Running the Test Scenario

Running the Test

  1. Open up ‘NavigationScenarios.feature’ file and make sure you have the ‘@Nexus5xOreo’ tag at the top of the feature file or above each test scenario you want to run
  2. Right-click on the test scenario and run it

It should pass 😀

Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.