Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Appium Tutorial

Introduction to Appium

Appium is an open-source test automation framework that allows you to write tests for mobile applications on various platforms like Android and iOS. It uses WebDriver protocol to communicate with the mobile application, which makes it easier to automate mobile apps just like web applications.

Why Use Appium?

Appium supports a wide range of programming languages, tools, and frameworks, which makes it a versatile choice for test automation. Here are some reasons to use Appium:

  • Cross-platform support: Write tests for both Android and iOS.
  • Supports native, hybrid, and mobile web applications.
  • Language agnostic: Use your preferred programming language.
  • Active community and rich ecosystem.

Setting Up Appium

To get started with Appium, you need to set up a few tools on your machine:

  1. Install Node.js.
  2. Install Appium using npm (Node Package Manager).
  3. Set up Android Studio and Xcode (for iOS development).
  4. Install the Appium Desktop for a GUI interface.

To install Appium via npm, you can run the following command:

npm install -g appium

Writing Your First Test

Once you have Appium set up, you can start writing your first test. Below is an example written in Java using JUnit:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;

public class FirstTest {
    private AppiumDriver driver;

    @Before
    public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("app", "path/to/your/app.apk");
        
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @Test
    public void testExample() {
        // Your test logic goes here
    }

    @After
    public void tearDown() {
        driver.quit();
    }
}
                

Running Your Test

Before running your test, ensure that Appium server is running. You can start it via command line or using Appium Desktop.

To run the test, execute the following command in your terminal:

mvn test

Common Appium Commands

Below are some common commands used in Appium testing:

  • driver.findElement(By.id("elementId")): Finds an element by its ID.
  • driver.click(): Clicks on the found element.
  • driver.sendKeys("input text"): Sends keys to the found element.
  • driver.quit(): Closes the app and ends the session.

Conclusion

Appium is a powerful tool for automating mobile applications. By following this tutorial, you should have a solid understanding of how to set up Appium, write your first test, and run it. Continue exploring the capabilities of Appium to enhance your test automation skills!