Appium python android примеры

Python — Hello world with Appium on Android

Appium is an automation tool to test native, hybrid and mobile web applications. It uses the webdriver to control the applications. It is designed as a client and server application. The client will send the commands to the server and then the server executes those commands on the mobile devices.

In this tutorial, I will show you how to run your first test with Appium on Android using Python.

Setup environment variables

Make sure that your Android environment variables are set correctly. ANDROID_HOME is the path to the Android’s SDK which also contains both tools and platform-tools folders. Here is an example for my case.

export ANDROID_HOME=/root/tmp/android/sdk export PATH=$:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

After your environment variables are set, you should able to run adb command. Otherwise, your environment variables are not set properly.

Install server

  1. Download the Appium-Desktop from https://github.com/appium/appium-desktop/releases/tag/v1.13.0
  2. Run it.

Install client

pip install Appium-Python-Client

Get Android device and application informations

In order for Appium to control of your mobile device, it needs the device name, application package name and the application main activity name.

To get the device name, plug your device to your computer and then run the following.

List of devices attached VOPVZL9LLRDEFEOV device

AndroidManifest: Package & Activity

The application package name and the application main activity name can be found in your AndroidManifest.xml . Here is an example for my case.

Читайте также:  Html and xml editor

Appium in action

For my case, here is a code example where Appium will launch my application.

from appium import webdriver desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['deviceName'] = 'VOPVZL9LLRDEFEOV' desired_caps['appPackage'] = 'net.openwritings.xmtl' desired_caps['appActivity'] = '.MainActivity' desired_caps['app'] = './app-debug.apk' # This will launch your Android application. driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

Reference

  • List of client available http://appium.io/docs/en/about-appium/appium-clients/index.html
  • https://github.com/appium/python-client
  • https://github.com/appium/appium/blob/master/docs/en/drivers/android-uiautomator2.md

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.

Источник

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.

 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() 

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:

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.

Источник

Оцените статью