Appium

Q: How Appium works ?

Ans:

Appium is an ‘HTTP Server’ written using a Node.js platform and drives iOS and an Android session using Webdriver JSON wire protocol.

Hence, before initializing the Appium Server, Node.js must be pre-installed on the system.

When Appium is downloaded and installed, then a server is set up on our machine that exposes a REST API.

It receives connection and command request from the client and executes that command on mobile devices (Android / iOS).

It responds back with HTTP responses. Again, to execute this request, it uses the mobile test automation frameworks to drive the user interface of the apps



Q: What are main locator used in Appium ?

Ans:

  1. Xpath
  2. ID
  3. accessibilityID
  4. className
  5. androidUIAutomator

Q: How to install any application on Emulator?

Ans:
First move to platform-tool folder and then open cmd
Secondly run below command
adb install <path of apk file>
Ex:
C:\Users\dheer\AppData\Local\Android\Sdk\platform-tools>adb install <C:\Users\dheer\Desktop\Appium\Appium_New\google-chrome-111.apk>

Q: How to set chromedriver path in Appium ?

Ans:
UiAutomator2Options options = new UiAutomator2Options();
options.setDeviceName("DheerajEmulater"); //emulator
options.setChromedriverExecutable("C:\\Users\\dheer\\Desktop\\Appium\\Appium_New\\chromedriver\\chromedriver_83.exe");

Q: What is use of UiAutomator2Options in Appium 8 ?

Ans:
// From Appium Java Client version 8.0.0 Beta
                        UiAutomator2Options options = new UiAutomator2Options();
options.setDeviceName("DheerajEmulater"); //emulator
options.setChromedriverExecutable("C:\\Users\\dheer\\Desktop\\Appium\\Appium_New\\chromedriver\\chromedriver_83.exe");
options.setApp("C:\\Users\\dheer\\Desktop\\Appium\\Appium_New\\src\\test\\java\\resources\\General-Store.apk");
//options.setApp("C:\\Users\\dheer\\Desktop\\Appium\\Appium_New\\google-chrome-111.apk");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

// Older approach: Java Client version 7.X.X
                        DesiredCapabilities capabilities = new DesiredCapabilities();
                        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
                        capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 700000);

Q:List out the limitations of using Appium?

Ans:
  • Appium does not support testing of Android Version lower than 4.2
  • Limited support for hybrid app testing. E.g., not possible to test the switching action of application from the web app to native and vice-versa
  • No support to run Appium Inspector on Microsoft Windows

Q:Explain how to find DOM element or xPath in a mobile application?

Ans:
To find the DOM element use “UIAutomateviewer” to find DOM element for Android application.

Q:What language does Appium support?

Ans:
Appium support any language that support HTTP request like Java, JavaScript with Node.js, Python, Ruby, PHP, Perl, etc.

Q:Explain the pros and cons of Appium?

Pros:
  • For programmer irrespective of the platform, he is automating ( Android or iOS) all the complexities will remain under single Appium server
  • It opens the door to cross-platform mobile testing which means the same test would work on multiple platforms
  • Appium does not require extra components in your App to make it automation friendly
  • It can automate Hybrid, Web and Native mobile applications

Cons:
  • Running scripts on multiple iOS simulators at the same time is possible with Appium
  • It uses UIAutomator for Android Automation which supports only Android SDK platform, API 16 or higher and to support the older API’s they have used another open source library called Selendroid

Q: Explain what is APPIUM INSPECTOR?

Ans:
Similar to Selenium IDE record and Playback tool, Appium has an “Inspector” to record and playback. It records and plays native application behavior by inspecting DOM and generates the test scripts in any desired language. However, Appium Inspector does not support Windows and use UIAutomator viewer in its option.

Q:Mention what are the basic requirement for writing Appium tests?

Ans:
For writing Appium tests you require,

  • Driver Client: Appium drives mobile applications as though it were a user. Using a client library you write your Appium tests which wrap your test steps and sends to the Appium server over HTTP.
  • Appium Session: You have to first initialize a session, as such Appium test takes place in the session. Once the Automation is done for one session, it can be ended and wait for another session
  • Desired Capabilities: To initialize an Appium session you need to define certain parameters known as “desired capabilities” like PlatformName, PlatformVersion, Device Name and so on. It specifies the kind of automation one requires from the Appium server.
  • Driver Commands: You can write your test steps using a large and expressive vocabulary of commands.

Q: Mention what are the possible errors one might encounter using Appium?

Ans:
The possible errors one might face in Appium includes:

  • Error 1: The following desired capabilities are needed but not provided: Device Name, platformName
  • Error 2: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path
  • Error 3: openqa.selenium.SessionNotCreatedException: A new session could not be created
  • Error 4: How to find DOM element or XPath in a mobile application?
  • SessionNotFoundException: On disconnection of Appium connection or Session not established, display this exception
  • ElementNotFoundException: When Appium does not find the element it is looking for, this exception is displayed
  • WebDriverException: When Driver does not exist, this exception is displayed
  • NoSuchContextException: is thrown when the context target that is to be switched to does not exist.

Q: What are native Apps/Mobile Web Apps/hybrid apps ?

Ans: 
  • Those Apps are written by using Android SDKs and IOS are known as Native Apps.
  •  There are mobile web pages that are those web apps that are accessed with mobile. browsers. In the case of the IOS platform, Appium supports Safari and for the Android platform, Chrome or any other built-in browser is used.
  •  Hybrid app are a mix of native and web applications, like native applications are available on the mobile app stores, and have features similar to native applications, like web applications rely on web browsers to be rendered. Online eCommerce sites and Netflix are some of the best examples of hybrid applications.

Q: How can you inspect elements that are present in the Native Android App?

Ans: 

1 .With the help of the UI Automator tool that is present in Android SDK, you will be able to access those object locators that are part of the Android Native app.

Steps to use UIAutomator Viewer:

  1. Launch UIAutomator Viewer: You can find it in the tools/bin directory of your Android SDK installation. Run uiautomatorviewer from the command line or through Android Studio.
  2. Capture a Screenshot: Click the "Device Screenshot" button in the UIAutomator Viewer to capture the current screen of your connected Android device.
  3. Inspect Elements: After capturing, you can inspect elements on the screenshot. UIAutomator Viewer will display information about the selected UI element, including its resource ID, class, and text.

2 . Appium Inspector is a feature of Appium that lets you inspect and interact with the elements of your mobile app.

  • Steps to use Appium Inspector:
    1. Start Appium Server: Launch the Appium server on your machine.
    2. Open Appium Inspector: In Appium Desktop, click the "Start Inspector Session" button.
    3. Configure Desired Capabilities: Set up the desired capabilities for your test, such as platformName, deviceName, appPackage, and appActivity.
    4. Start the Session: Click "Start Session" to open the app on the device or emulator.
    5. Inspect Elements: Use the Inspector's UI to interact with and inspect the elements of the app. You can view element properties and hierarchies.

Q: Mention the method with which you can scroll down in App?

Ans: With the help of the scrollTo () method, you will be able to scroll down in App. Also, such a method will help you to automatically scroll until the specific text is not match.

For Android

TouchAction action = new TouchAction(driver);

action.press(PointOption.point(500, 1000))

      .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))

      .moveTo(PointOption.point(500, 500))

      .release()

      .perform();


For iOS

In iOS, scrolling is often performed using the scroll method from the MobileElement class, or by executing mobile-specific commands like mobile:scroll.


public class ScrollDownExample {

    public static void main(String[] args) {

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("platformName", "iOS");

        capabilities.setCapability("deviceName", "MyDevice");

        capabilities.setCapability("app", "/path/to/your/app.app");

        IOSDriver<MobileElement> driver = new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);

        // Scroll down to find a specific element

        driver.executeScript("mobile: scroll", ImmutableMap.of("direction", "down"));

        // Perform operations on the element if needed

        MobileElement element = driver.findElementByAccessibilityId("elementId");

        element.click(); // Example action

        driver.quit();

    }

}

Explanation

Android:

findElementByAndroidUIAutomator allows you to use UIAutomator for scrolling actions.

new UiScrollable(new UiSelector().scrollable(true)).scrollForward() scrolls down in a scrollable view.

iOS:

executeScript("mobile: scroll", ImmutableMap.of("direction", "down")) scrolls the screen in the specified direction.

Q: Is it possible to start an Appium server programmatically?

Ans: 

It is possible to start an appium server programmatically. Generally, the commands that are being entered to the command prompt are written in note pad and it is saved with .bat extension and you can click the bat file. 

public class AppiumServerManager {

    public static void main(String[] args) {

        // Create and configure the Appium service

        AppiumDriverLocalService appiumService = AppiumDriverLocalService.buildService(

            new AppiumServiceBuilder()

                .usingAnyFreePort() // Automatically select a free port

                .withArgument(ServerFlag.LOG_LEVEL, "info") // Set the log level to info

                .withStartUpTimeInSeconds(60) // Adjust startup time if needed

        );

        // Start the Appium server

        appiumService.start();

        System.out.println("Appium server started at: " + appiumService.getUrl());

        // Add your test automation code here

        // Stop the Appium server

        appiumService.stop();

        System.out.println("Appium server stopped.");

    }

}


Q: How to handle hybrid Apps?

Ans:

Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {
    if (context.contains("WEBVIEW")) {
        driver.context(context);
        break;
    }
}

Q: How can you inspect elements on the IOS apps?

Ans: 

With the help of using an Appium inspector that is a GUI-based tool, you can identify elements on IOS apps. These GUI-based tools are quite similar to that of selenium IDE.

Q: How is it possible to identify Mobile browser objects?

Ans: 

you can make use of User-Agent in order to identify objects in the Mobile browser. It is done by using the user agent and then changing the browser as the mobile proxy and thus gets an object.

Q: Write the command that will allow you to identify objects uniquely when there are multiple objects within the same class name using Appium?

Ans: 

With the help of the command driver. find element (By.className) it is possible to identify the elements uniquely. 

Q: Give the difference between emulator and simulator?

Ans: 

The simulator is used for calling IOS virtual devices that will launch from Xcode in MAC. The emulator is used for calling Android virtual devices.

Q: What is Appium’s most considerable limitation?

Ans: 

Hand down my chin starting to think and mumbling. If I had to provide one single thing you should be aware of about Appium before starting using it, it would surely be multiple session handling. Since Appium is a server, it serves HTTP requests; you might have two different computers running a test each against the same Appium server:

what happens? As for now, Appium does not support this scenario and the second test will be aborted. This is a considerable limitation because no queuing system comes with Appium. If you need to support multiple sessions, you will need to implement this feature by yourself.

Q: What data exchange is?

Ans: 

When we say “data exchange” I am not referring to scenarios like getting or setting the value of a textbox. I am also not referring to getting or setting the value of an element’s attribute. All these things are easy to achieve in Appium as Selenium provides commands just for those. 

By “data exchange” I mean exchanging information hosted by complex objects stored in different parts of your review-based app like the window object.

Consider when you dispatch and capture events, your app can possibly do many things and the ways data flows can be handled are many. Some objects might also have a state and the state machine behind some scenarios in your app can be large and articulated. For all these reasons you might experience problems when testing.

Q:List difference between Selenium WebDriver and Appium WebDriver

Ans: Selenium and Appium both use

*JSON wire protocol

*Methods like

sendKeys(),

findByClassName(),

findByElementId(),

findByXPath()

There is a difference between Selenium WebDriver and Appium WebDriver as below.

  • Selenium WebDriver Supports mobile web applications using browsers like Firefox, Chrome, Opera, Safari and Appium WebDriver Supports native mobile application installed on iOS and Android devices
  • Selenium WebDriver has become standard for automation testing of browser based web applications while RemoteWebDriver class is inherited by Appium WebDriver class. This class with help of Appium server provides additional functions useful in context of mobile test automation.

Q: Describe ways to debug Appium tests.

Ans: Following are some ways we can do:
  1. Any error that occurred during mobile tests can be traced from the logs generated at the Appium server.
  2. Test scripts created can be debugged with the help of the programming language used in writing these tests.

Q: How can we retrieve the test status of Appium test execution?

Ans:
There are various ways to display the test status:
  1. Integrating TestNG with Appium
  2. ExtentReports that give test status reports in the dashboard
  3. REST API of BrowserStack App Automate for .apk (Android app) or .ipa (iOS app).
  4. Built-in reporting tools like Maven and Allure


Q: Difficult Scenarios to automate in Appium

Ans
Testing with Appium can be challenging for several reasons, particularly in complex scenarios. Here are some of the most difficult scenarios to test with Appium:
  1. Dynamic Content: Apps with dynamic content that changes frequently (e.g., live feeds, real-time updates) can be difficult to test because the elements you're trying to interact with may not be stable or consistent.

  2. Complex Gestures: Testing complex gestures, such as multi-finger swipes or pinches, can be tricky. These gestures often require precise timing and can be sensitive to variations in device performance or screen sensitivity.

  3. Hybrid and Web Views: Apps that contain a mix of native and web views (hybrid apps) can be challenging. Switching contexts between native and web views, and ensuring actions and assertions work across both, requires careful handling of context switching.

  4. Performance and Load Testing: Evaluating app performance under load or high-stress conditions is difficult. Appium primarily focuses on functional testing, so simulating and accurately testing performance metrics can be complex and may require additional tools.

  5. Device Fragmentation: Testing on a wide range of devices with different screen sizes, OS versions, and hardware configurations introduces variability that can make it difficult to ensure consistent test results.

  6. Handling Alerts and Pop-ups: Interacting with and handling system alerts, permission dialogs, or pop-ups can be cumbersome, especially if they appear unpredictably or require specific timing to handle correctly.

  7. App State and Background Testing: Testing scenarios where the app is put in the background or interacts with other apps (e.g., handling push notifications, interruptions) can be challenging due to the need to simulate and verify app behavior accurately.

  8. Real-Time Communication: Apps that rely on real-time communication (e.g., messaging apps, VoIP apps) can be difficult to test because they involve real-time interactions and dependencies on network conditions.

  9. Localization and Internationalization: Testing an app in multiple languages and locales, ensuring all text and UI elements are displayed correctly and are functional, adds another layer of complexity.

  10. Device-Specific Features: Testing features that are unique to specific devices or OS versions, like biometric authentication or device-specific sensors, can be challenging if you don’t have access to all the required hardware.

  11. Authentication Flows: Handling and automating authentication flows (e.g., OAuth, 2FA) can be complex, especially when dealing with multi-step processes or integration with external services.

  12. Complex Data Inputs: Testing forms or inputs that require complex data sets, such as large files or multiple data fields, can be cumbersome and error-prone


Q: What are the operation that TouchAction can perform?

Ans:

1. tap()
Usage: Simulates a single tap on a specific element or at specific coordinates.
Example:

new TouchAction<>(driver)
    .tap(PointOption.point(x, y)) // Tap at specific coordinates
    .perform();


new TouchAction<>(driver)
    .tap(TapOptions.tapOptions().withElement(ElementOption.element(element))) // Tap on an element
    .perform();

2. longPress()
Usage: Simulates a long press (tap and hold) on an element or at specific coordinates.
Example:

new TouchAction<>(driver)
    .longPress(PointOption.point(x, y))
    .release()
    .perform();

new TouchAction<>(driver)
    .longPress(LongPressOptions.longPressOptions().withElement(ElementOption.element(element)).withDuration(Duration.ofSeconds(2)))
    .release()
    .perform();

3. press()
Usage: Presses on an element or at specific coordinates. This is typically followed by a move action.
Example:

new TouchAction<>(driver)
    .press(PointOption.point(x, y))
    .moveTo(PointOption.point(newX, newY)) // Move to another position
    .release()
    .perform();

4. moveTo()
Usage: Moves the touch from one point to another without releasing. Often used in conjunction with press() to perform drag-and-drop actions.
Example:

new TouchAction<>(driver)
    .press(PointOption.point(startX, startY))
    .moveTo(PointOption.point(endX, endY))
    .release()
    .perform();

5. release()
Usage: Releases the touch action at the current position. Usually paired with press() or longPress().
Example:

new TouchAction<>(driver)
    .press(PointOption.point(x, y))
    .release()
    .perform();

6. waitAction()
Usage: Waits for a specified duration before continuing the action. Useful for creating pauses in gesture sequences.
Example:

new TouchAction<>(driver)
    .press(PointOption.point(x, y))
    .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))) // Wait for 2 seconds
    .release()
    .perform();

7. swipe()
Usage: Simulates a swipe gesture from one coordinate to another. This is typically done using a combination of press(), moveTo(), and release().
Example:

new TouchAction<>(driver)
    .press(PointOption.point(startX, startY))
    .moveTo(PointOption.point(endX, endY))
    .release()
    .perform();

8. perform()
Usage: Executes the touch action sequence.
Example:

new TouchAction<>(driver)
    .tap(PointOption.point(x, y))
    .perform();

9. multiTouchAction()
Usage: Allows multiple touch actions to be performed simultaneously. Useful for gestures like pinch or zoom.
Example:

MultiTouchAction multiTouch = new MultiTouchAction(driver);
TouchAction<?> action1 = new TouchAction<>(driver).tap(PointOption.point(x1, y1));
TouchAction<?> action2 = new TouchAction<>(driver).tap(PointOption.point(x2, y2));
multiTouch.add(action1).add(action2).perform();


Q: Real time scenarios which can be automated in Appium

Below are example Appium automation scripts written in Java for each of the specified real-time scenarios using the TouchAction and MultiTouchAction classes.

1. Swipe to Refresh

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;
import io.appium.java_client.touch.WaitOptions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class SwipeToRefresh {
    public static void swipeToRefresh(AndroidDriver<MobileElement> driver) {
        int startX = 500;
        int startY = 1000;
        int endY = 300;

        new TouchAction<>(driver)
                .press(PointOption.point(startX, startY))
                .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
                .moveTo(PointOption.point(startX, endY))
                .release()
                .perform();
    }
}

2. Drag and Drop

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.ElementOption;
import org.openqa.selenium.WebDriver;

public class DragAndDrop {
    public static void dragAndDrop(AndroidDriver<MobileElement> driver, MobileElement sourceElement, MobileElement targetElement) {
        new TouchAction<>(driver)
                .press(ElementOption.element(sourceElement))
                .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
                .moveTo(ElementOption.element(targetElement))
                .release()
                .perform();
    }
}

3. Zoom In/Out (Pinch to Zoom)

import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.WebDriver;

public class ZoomInOut {
    public static void pinchToZoom(AndroidDriver<MobileElement> driver
        int centerY = 1000;
        int offsetX = 200;
        int offsetY = 200;

        TouchAction<?> finger1 = new TouchAction<>(driver)
                .press(PointOption.point(centerX - offsetX, centerY - offsetY))
                .moveTo(PointOption.point(centerX, centerY))
                .release();

        TouchAction<?> finger2 = new TouchAction<>(driver)
                .press(PointOption.point(centerX + offsetX, centerY + offsetY))
                .moveTo(PointOption.point(centerX, centerY))
                .release();

        new MultiTouchAction(driver)
                .add(finger1)
                .add(finger2)
                .perform();
    }
}

4. Tap and Hold

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.LongPressOptions;
import org.openqa.selenium.WebDriver;

import java.time.Duration;

public class TapAndHold {
    public static void tapAndHold(AndroidDriver<MobileElement> driver, MobileElement element) {
        new TouchAction<>(driver)
                .longPress(LongPressOptions.longPressOptions()
                        .withElement(ElementOption.element(element))
                        .withDuration(Duration.ofSeconds(3)))
                .release()
                .perform();
    }
}

5. Double Tap

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.TapOptions;
import org.openqa.selenium.WebDriver;

public class DoubleTap {
    public static void doubleTap(AndroidDriver<MobileElement> driver, MobileElement element) {
        new TouchAction<>(driver)
                .tap(TapOptions.tapOptions().withElement(ElementOption.element(element)))
                .perform()
                .tap(TapOptions.tapOptions().withElement(ElementOption.element(element)))
                .perform();
    }
}

Comments