Friday, December 1, 2023
PLACE YOUR AD HERE - TechRecur
HomeSoftwareAdvanced Appium Techniques for Cross-Platform Mobile Automation

Advanced Appium Techniques for Cross-Platform Mobile Automation

-

Selenium Automation Testing has become a staple in the testing industry, offering a reliable and efficient framework for web application testing. As mobile app development continues to grow, so does the need for comprehensive mobile automation testing. This is where Appium, an open-source mobile automation framework, comes into play. While many testers are familiar with the basic functionalities of Appium, there are advanced techniques that can take cross-platform mobile automation testing to the next level.

In this article, we will explore the cutting-edge and advanced Appium techniques that can help elevate your mobile automation testing skills and ensure your applications meet the highest standards of quality. Get ready to take your mobile automation testing to the next level with the expert insights and guidance you need to succeed.

Advanced Appium Techniques for Cross-Platform Mobile Automation

Advanced Appium Techniques for Cross-Platform Mobile Automation are advanced strategies and tactics that enable testers to create more efficient and effective mobile automation tests. These techniques can help testers to increase the accuracy and reliability of their mobile automation tests while reducing testing time and effort.

Here are some advanced Appium techniques for cross-platform mobile automation:

1.   Use API

Mobile app testing is a complex process that requires careful consideration of the various events and actions taking place during testing. Appium, the leading mobile automation tool, uses the WebDriver agent to perform testing, and its advanced techniques include the use of API to track these events.

The process of bundling the WebDriver agent and installing it on a real device or simulator can take some time, making it difficult to understand the time taken by certain processes. Appium’s Events API is the solution to this issue, providing insight into the time required for each test system to start up. The API includes built-in worker events, such as test system start time and final recovery, as well as custom events tailored to specific applications.

For instance, if you want to understand how long it takes for an application to navigate from screen A to screen B, you can use Appium’s Events API to track this custom event. By defining the vendor and event name, you can distinguish between Appium’s default worker events and your custom events. For example, VodQA, one of the example applications that we will illustrate, is a defined set vendor, and the event name is OnLoginScreen.

Logging these events as you navigate between screens can provide you with a better understanding of the time required to automate your application under test. By leveraging Appium’s Events API and defining custom events, you can streamline your testing process and gain greater insights into the performance of your mobile application across multiple platforms.

Here is a code snippet of how to use the Appium Event API to log custom events:

// create a new instance of the EventFiringWebDriver with the DesiredCapabilities

EventFiringWebDriver driver = new EventFiringWebDriver(new RemoteWebDriver(new URL(“http://localhost:4723/wd/hub”), capabilities));

// create a new instance of the EventListener to handle the events

EventListener eventListener = new EventListener();

// register the event listener with the driver

driver.register(eventListener);

// navigate to the login screen

driver.get(“your-app-url.com/login”);

// log a custom event for navigating to the login screen

driver.executeScript(“mobile: logEvent”, ImmutableMap.of(“name”, “OnLoginScreen”));

// perform actions on the login screen

// navigate to the home screen

driver.get(“your-app-url.com/home”);

// log a custom event for navigating to the home screen

driver.executeScript(“mobile: logEvent”, ImmutableMap.of(“name”, “OnHomeScreen”));

// perform actions on the home screen

// unregister the event listener from the driver

driver.unregister(eventListener);

// quit the driver

driver.quit();

In this example, we create a new instance of the EventFiringWebDriver with the desired capabilities, register a new instance of the EventListener to handle events, navigate to the login and home screens, and log custom events using the mobile: logEvent command. Finally, we unregistered the event listener from the driver and quit the driver.

Important Note: Note that this is just an example and may need to be modified to fit your specific testing environment and application.

2.   Use Face ID

Appium has introduced an exciting feature that allows in-app verification using face ID. This feature has become increasingly important in modern-day applications as more and more apps are using face ID for login or payment verification. Appium supports in-app testing verification on iOS test systems using face ID and contact ID verification on older iOS devices. However, it is important to note that face ID support is only available on iOS simulators and not on real devices.

Face ID needs to work seamlessly, and Appium ensures this by closely adhering to the WebDriver protocol defined by W3C standards. However, some features of in-app verification using face ID are not explicitly defined in the W3C standard. Appium provides an alternate method to leverage these features specific to the platform by using mobile supporting methods.

Appium defines its API and exposes it to the outside world using mobile supporting methods. One such method is “sendBiometricMatch,” which takes two arguments: face ID and its specific value, which can be either true or false, to perform an ideal match. It can also perform a non-matching face to observe how the application behaves when there is a face mismatch.

Another mobile supporting method specific to selecting the biometric is “isEnabled.” It takes an argument of either true or false, and it is okay to enable it multiple times as the test systems are intelligent enough to handle it. However, it is advisable to enable it once per test, or at most once outright, to avoid any potential issues. Once enabled, we can send real face ID validation, either true or false, using the mobile supporting method called “sendBiometricMismatch.”

In conclusion, face ID has proven to be the most feasible Appium automation technique due to its accessibility to every layman.

An example code snippet for using the “sendBiometricMatch” method in Appium:

# import necessary libraries

from appium import webdriver

# set desired capabilities for the Appium server

desired_caps = {

  ‘platformName’: ‘iOS’,

  ‘platformVersion’: ‘14.5’,

  ‘deviceName’: ‘iPhone 12’,

  ‘app’: ‘path/to/app’,

  ‘automationName’: ‘XCUITest’,

  ‘useNewWDA’: True

}

# connect to the Appium server

driver = webdriver.Remote(‘http://localhost:4723/wd/hub’, desired_caps)

# enable face ID verification

driver.execute_script(‘mobile: sendBiometricMatch’, {‘isEnabled’: True})

# perform face ID verification with the correct value

driver.execute_script(‘mobile: sendBiometricMatch’, {‘match’: True})

# perform face ID verification with incorrect value

driver.execute_script(‘mobile: sendBiometricMatch’, {‘match’: False})

# close the session

driver.quit()

Important Note: Note that this code is only an example and may need to be modified to fit your specific use case.

3.   Stream Apps

When testing mobile applications, it can be useful to record what is happening on the device screen. With Appium, you can do this easily using a video recording feature. When you start recording, you can see what is happening on your device screen on your computer screen by accessing a specific port. This is helpful because you can see the different screens on your device as you move through your app.

Appium offers two distinct APIs for starting and stopping video recording, which provides a significant advantage for users. When recording the screen, you can easily access a particular port on your browser where the specific screen is being displayed. Even when moving from one screen to another, you can see the transitions of the screen from your device onto the browser seamlessly.

This functionality is powered by FFmpeg in the backend, which streams the video into the browser. To use this feature in your Appium version, you only need to install FFmpeg on your machine and ensure it is located in a path that Appium can access. Don’t worry, the installation process is not complicated, and Appium will guide you through it.

However, when running tests across multiple devices, you may need to configure port numbers appropriately. Appium typically starts the FFmpeg server on port 9100 when using a single device. But when running tests across several devices and you want to view the screens on the browser, you will need to provide unique and distinct ports.

Fortunately, Appium offers a function called the MJPEG server URL, which allows you to set the port number to any desired value and stream a specific device’s screen to your browser. In total, there are over 280 capabilities for both iOS and Android that users can leverage to improve their testing experience.

Unfortunately, there are no direct APIs that we can use to start and stop video recording on Android devices. However, there are some third-party streaming applications that you can install on your device to start your streaming. Once you have installed the application, you can get the port number and follow the same steps as we did for iOS devices.

For those interested in learning more about utilizing third-party streaming apps for Android devices and the various options available, Appium Pro’s blog is an excellent resource to explore.

Appium is a powerful mobile automation tool that can be further enhanced by pairing it with a cloud-testing solution like LambdaTest. With LambdaTest, you can run your Appium tests on real devices in the cloud, enabling you to scale your testing across platforms, find and fix bugs faster through advanced reporting, and seamlessly integrate test automation into your CI/CD pipeline. By combining the advanced techniques of Appium with the benefits of LambdaTest’s cloud-based automation testing platform, you can improve the efficiency of your mobile app testing. So, make sure to use these techniques to maintain the consistency of your device and have a better experience overall.

Starting the session on the LambdaTest Platform

Before connecting our tests to run on LambdaTest cloud devices, we need to set some environment variables on our machine. These environment variables will be used during the test setup.

To set the environment variables, you can add the following lines to your .zshrc or .bash_profile file if you’re on a Mac or an equivalent file on Linux. If you’re using Windows, you can manually add the variables to the Windows environment variable window.

Here are the environment variables that you need to set:

  1. LT_USERNAME: This is your LambdaTest username.
  2. LT_ACCESS_KEY: This is your LambdaTest access key.
  3. LT_APP_ANDROID: This is the URL of your app on the LambdaTest cloud.

Once you’ve set these environment variables, you can proceed with setting up the cloud capabilities.

private static final String DEVICE_NAME_KEY = “deviceName”;

private static final String DEVICE_VERSION_KEY = “deviceVersion”;

. . .

private URL getCloudUrl () {

   final var cloudUrl = “https://{0}:{1}@mobile-hub.lambdatest.com/wd/hub”;

   final var userName = Objects.requireNonNull (System.getenv (“LT_USERNAME”), “Cloud user name is required”);

   final var key = Objects.requireNonNull (System.getenv (“LT_ACCESS_KEY”), “Cloud access key is required”);

   final var path = MessageFormat.format (cloudUrl, userName, key);

   try {

              return new URL (path);

   } catch (final MalformedURLException e) {

              throw new UnsupportedOperationException (format (“URL malformed: {0}”, path));

   }

}

. . .

private Capabilities buildCloudCapabilities () {

   final var deviceName = System.getProperty (DEVICE_NAME_KEY, “Pixel_6_Pro”);

   final var deviceVersion = System.getProperty (DEVICE_VERSION_KEY, “11”);

   final var options = new UiAutomator2Options ();

   final var ltOptions = new HashMap<> ();

   ltOptions.put (“w3c”, true);

   ltOptions.put (“platformName”, “Android”);

   ltOptions.put (DEVICE_NAME_KEY, deviceName);

   ltOptions.put (“platformVersion”, deviceVersion);

   ltOptions.put (“app”, Objects.requireNonNull (System.getenv (“LT_APP_ANDROID”), “Cloud App URL is required”));

   ltOptions.put (“devicelog”, true);

   ltOptions.put (“visual”, true);

   ltOptions.put (“network”, true);

   ltOptions.put (“video”, true);

   ltOptions.put (“build”, “Appium sample Build”);

   ltOptions.put (“name”, “Android Sample”);

   ltOptions.put (“project”, “Appium Sample Project”);

   ltOptions.put (“autoGrantPermissions”, true);

   ltOptions.put (“isRealMobile”, true);

   options.setCapability (“lt:options”, ltOptions);

   return options;

}

. . .

The above code snippet sets up the cloud capabilities for your tests by building the cloud platform URL and creating the capabilities specific to the LambdaTest cloud. It also sets up some options like devicelog, visual, network, and video to gather logs, screenshots, network traffic, and videos of your tests for debugging purposes.

To connect your test to run on the cloud device, you need to pass the cloud URL and capabilities to the AndroidDriver instance, as shown below:

URL serverUrl = getCloudUrl ();

Capabilities capabilities = buildCloudCapabilities ();

this.driver = new AndroidDriver (serverUrl, capabilities);

This sets up the connection to the cloud device and starts the session. To stop the cloud session, you need to call the quit() method.

Wrap-up

Mastering advanced Appium techniques is essential for successful cross-platform mobile automation. Appium offers several useful features that can make mobile testing easier and more reliable, including the ability to record videos, manage ports, and handle deep linking scenarios. Additionally, Appium provides many capabilities that enable you to customize your test scenarios and ensure consistent results across different devices and platforms.

To become an Appium expert, you should continuously learn and experiment with the latest Appium features and capabilities. Stay up-to-date with Appium Pro, read the official Appium documentation, and engage with the Appium community to share knowledge and best practices.

By mastering advanced Appium techniques, you can become a more efficient and effective mobile test automation engineer, delivering high-quality mobile applications that delight your users.

TechRecur

We don’t spam! Read our privacy policy for more info.

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!

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

- Place Your AD Here -PLACE YOUR Educational AD HERE FREE - TechRecur
- Place Your AD Here -PLACE YOUR Educational AD HERE FREE - TechRecur
- Place Your AD Here -PLACE YOUR Educational AD HERE FREE - TechRecur