Comprehensive Guide to Appium for Mobile Application Automation

Introduction to Appium: A Versatile Tool for Mobile Application Automation

Appium is an open-source automation tool for running scripts and testing native applications, mobile-web applications, and hybrid applications on Android or iOS using a webdriver. It drives iOS, Android, and Windows apps using the WebDriver protocol.

Getting Started with Appium

To start using Appium, you need to install the Appium server and client libraries.

Install Appium Server

 npm install -g appium 

Install Appium Clients

 npm install appium 

Running the Appium Server

 appium 

Once the Appium server is running, you can write test scripts in various programming languages using the Appium client libraries.

Appium APIs with Examples

1. Start a Session

You can start a session using the desired capabilities to configure the automation environment.

 const wdio = require("webdriverio");
const opts = {
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "9.0",
    deviceName: "Android Emulator",
    app: "/path/to/the/app.apk",
    automationName: "UiAutomator2"
  }
};
const client = wdio.remote(opts); 

2. Find Element

Use different strategies like accessibility id, xpath, id, class name to find elements:

 await client.$("~accessibility_id"); await client.$("//android.widget.TextView[@text='Accessibility']"); await client.$("android=new UiSelector().resourceId('android:id/text1')"); 

3. Perform Actions

Perform various actions such as tap, send keys, swipe:

 const el = await client.$("//android.widget.TextView[@text='Accessibility']"); await el.click();
const inputField = await client.$("//android.widget.EditText"); await inputField.setValue("hello world");
await client.touchAction([
  { action: 'press', x: 200, y: 200 },
  { action: 'moveTo', x: 200, y: 800 },
  'release'
]); 

4. Take Screenshot

Save the screenshot for your reference:

 const screenshot = await client.saveScreenshot("/path/to/save/screenshot.png"); 

App Example

Let’s create an example automation project to launch an app, perform some actions and verify results.

 const wdio = require("webdriverio");
const opts = {
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "9.0",
    deviceName: "Android Emulator",
    app: "/path/to/the/app.apk",
    automationName: "UiAutomator2"
  }
};
async function main() {
  const client = await wdio.remote(opts);

  // Find and click on login button
  const loginButton = await client.$("//android.widget.Button[@text='Login']");
  await loginButton.click();

  // Enter username and password
  const usernameField = await client.$("//android.widget.EditText[@resource-id='username']");
  await usernameField.setValue("testuser");

  const passwordField = await client.$("//android.widget.EditText[@resource-id='password']");
  await passwordField.setValue("testpassword");

  // Click on submit button
  const submitButton = await client.$("//android.widget.Button[@text='Submit']");
  await submitButton.click();

  // Verify login success
  const successMessage = await client.$("//android.widget.TextView[@text='Welcome']");
  const isDisplayed = await successMessage.isDisplayed();

  console.log(isDisplayed ? "Login Successful!" : "Login Failed");
  await client.deleteSession();
}
main(); 

Appium is a powerful tool for mobile application testing and with these APIs and example, you have a good starting point to automate your app’s testing process efficiently.

Hash: 13adf3e19b4630cefd2fb7a69444794d1400393baa43d3bfa2f6ab77f7c47a69

Leave a Reply

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