Write a Test (Python)

The Appium Python Client is an official Appium client in Python, which is available via pypi under the Appium-Python-Client package name. It inherits from the Selenium Python Binding, so installing the Appium Python Client includes the selenium binding.

pip install Appium-Python-Client

This example uses Python's built-in unittest module, though you can use any Python test framework you want. The Appium Python client adds the appium: vendor prefix automatically. You usually do not need to worry about the prefix.

test.py
import unittest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

capabilities = dict(
    platformName='Android',
    automationName='uiautomator2',
    deviceName='Android',
    appPackage='com.android.settings',
    appActivity='.Settings',
    language='en',
    locale='US'
)

appium_server_url = 'http://localhost:4723'

class TestAppium(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Remote(appium_server_url, capabilities)

    def tearDown(self) -> None:
        if self.driver:
            self.driver.quit()

    def test_find_battery(self) -> None:
        el = self.driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]')
        el.click()

if __name__ == '__main__':
    unittest.main()

Note

It's not within the scope of this guide to give a complete run-down on the Python client library or everything that's happening here, so we'll leave the code itself unexplained in detail for now. - You may want to read up particularly on Appium Capabilities. - functional test code in Python Client GitHub repository should help to find more working example. - Documentation also helps to find methods defined in the Appium Python Client.

Basically, this code is doing the following:

  1. Defining a set of "Capabilities" (parameters) to send to the Appium server so Appium knows what kind of thing you want to automate.
  2. Starting an Appium session on the built-in Android settings app.
  3. Finding the "Battery" list item and clicking it.
  4. Pausing for a moment purely for visual effect.
  5. Ending the Appium session.

That's it! Let's give it a try. Before you run the test, make sure that you have an Appium server running in another terminal session, otherwise you'll get an error about not being able to connect to one. Then, you can execute the script:

python test.py

If all goes well, you'll see the Settings app open up and navigate to the "Battery" view before the app closes again.

Congratulations, you've started your Appium journey! Read on for some next steps to explore.