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.
Before we dive into writing our first test script, we need to ensure we have the necessary tools installed:
npm install -g appium
pip install Appium-Python-Client
After you have installed the required tools, it’s time to set up your environment. Here’s how you can do that:
Start the Appium server using the command line:
appium
This will initialize the Appium server, which listens for commands from your test scripts.
Open Android Studio, and create or run an existing Android Virtual Device (AVD) to simulate a mobile device.
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()
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.
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.
platformVersion
that matches your emulator or device.adb
to list the app packages on your device.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!
18/09/2024 | Mobile Testing
30/09/2024 | Mobile Testing
18/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing
21/09/2024 | Mobile Testing
30/09/2024 | Mobile Testing
30/09/2024 | Mobile Testing
30/09/2024 | Mobile Testing