logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Writing Your First Test Script with Appium

author
Generated by
Hitendra Singhal

21/09/2024

AI GeneratedAppium

Sign in to read full article

Introduction to Appium

Appium is an open-source tool that allows you to automate mobile applications on iOS and Android platforms. It enables you to write tests for native, hybrid, and mobile web applications. One of the major advantages of Appium is that it supports multiple programming languages, including Java, Python, Ruby, and JavaScript.

In this blog, we will walk through the process of writing a simple test script using Appium with Python. By the end, you’ll have a basic understanding of how to set up Appium and write your first automated test.

1. Prerequisites

Before we dive into writing our first test script, we need to ensure we have the necessary tools installed:

  • Python: Make sure you have Python installed on your machine. You can download it from python.org.
  • Appium: Install Appium via npm (Node Package Manager). You need to have Node.js installed. You can install Appium globally using the command:
    npm install -g appium
  • Appium Python Client: You will also need the Appium Python client. You can install this using pip:
    pip install Appium-Python-Client
  • Android Studio: For Android application testing, install Android Studio and set up the Android SDK and emulator.

2. Setting Up the Environment

After you have installed the required tools, it’s time to set up your environment. Here’s how you can do that:

  1. Start the Appium server using the command line:

    appium

    This will initialize the Appium server, which listens for commands from your test scripts.

  2. Open Android Studio, and create or run an existing Android Virtual Device (AVD) to simulate a mobile device.

3. Writing Your First Test Script

Now that your environment is set up, let’s jump into writing our first test script. We’ll create a simple test to launch the calculator app and perform an addition operation.

Here’s a basic example of what our test script will look like:

from appium import webdriver import time # Desired Capabilities desired_caps = { 'platformName': 'Android', 'platformVersion': 'YOUR_ANDROID_VERSION', # replace it with your device version 'deviceName': 'YOUR_DEVICE_NAME', # replace with your device name 'appPackage': 'com.android.calculator2', # Package name of the calculator app 'appActivity': '.Calculator', # Main activity of the calculator app } # Initialize the driver driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # Test script actions try: # Wait for the app to load time.sleep(2) # Find the buttons by their resource-id and perform click actions driver.find_element_by_id('com.android.calculator2:id/digit_2').click() driver.find_element_by_id('com.android.calculator2:id/op_add').click() driver.find_element_by_id('com.android.calculator2:id/digit_3').click() driver.find_element_by_id('com.android.calculator2:id/eq').click() # Fetch and print the result result = driver.find_element_by_id('com.android.calculator2:id/result').text print(f'Result of 2 + 3 = {result}') finally: # Close the driver driver.quit()

4. Explanation of the Code

Let's break down what each part of the script does:

  • Desired Capabilities: This section defines the required settings to tell Appium what kind of mobile device and application we’re trying to automate.

  • Initializing the Driver: We create a new session of Appium to interact with our specified mobile app running on the emulator.

  • Performing Actions: We are using the element IDs to find buttons (2, +, 3, and =), using the click() method to perform actions. The result is then retrieved and printed.

  • Clean Up: Using driver.quit() ensures that we properly close the driver and free up resources after the test is complete.

5. Running Your Test

Once you have your script ready, save it as calculator_test.py and run it using Python:

python calculator_test.py

If everything is set up correctly, your calculator app should open in the emulator, perform the addition, and print the result in the console.

6. Troubleshooting Common Issues

  • Ensure you have the correct Android version in platformVersion that matches your emulator or device.
  • Double-check the app package and activity names. You can use various tools such as adb to list the app packages on your device.
  • Make sure the Appium server is running before executing your test script.

By following these steps, you should be able to create and run your first Appium test script successfully. Appium's flexibility means that you can now modify your scripts to create various test cases for your mobile applications!

Popular Tags

AppiumMobile TestingAutomation

Share now!

Like & Bookmark!

Related Collections

  • Appium Mobile Testing Mastery

    30/09/2024 | Mobile Testing

  • Mastering Mobile Testing: End-to-End Automation and Manual Strategies

    18/09/2024 | Mobile Testing

Related Articles

  • Integrating Appium with TestNG and JUnit for Cross-Platform Mobile Testing

    21/09/2024 | Mobile Testing

  • Implementing Page Object Model in Appium Tests

    21/09/2024 | Mobile Testing

  • Mastering Element Identification with Appium Inspector

    30/09/2024 | Mobile Testing

  • Mastering Continuous Integration for Appium Tests

    30/09/2024 | Mobile Testing

  • Automating Mobile App Installation and Launch using Appium

    21/09/2024 | Mobile Testing

  • Advanced Synchronization Techniques in Appium

    21/09/2024 | Mobile Testing

  • Writing Your First Appium Test Script

    30/09/2024 | Mobile Testing

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design