Configuration of Appium Android environment

Table of contents

Foreword:

Environmental preparation

write a script to play


Foreword:

Before using Appium for Android automation testing, you need to configure the corresponding Android environment.

Environmental preparation

In order to avoid detours, we must first ensure three points:

  • Android SDK API >= 17 (Additional features require 18)
  • Environment variables  ANDROID_HOME and make sure  $ANDROID_HOME/platform-tools and  $ANDROID_HOME/tools included in PATH. for example:

    export ANDROID_HOME="/Applications/adt-bundle-mac-x86_64-20130729/sdk"
    export PATH=$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools
    
  • If you use a physical machine to test, please make sure that the system is  4.1 above.

Otherwise, you'll run into problems like:

2.x real device

info: "/Applications/adt-bundle-mac-x86_64-20130729/sdk/platform-tools/adb" -s S5830f63efdb6 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
info: [ADB STDOUT] uiautomator: permission denied

4.0.x real device

info: "/Applications/adt-bundle-mac-x86_64-20130729/sdk/platform-tools/adb" -s HT1B4V803001 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
info: [ADB STDOUT] /system/bin/sh: uiautomator: not found

If you are using Appium installed by Appium.dmg, you need to set custom android sdk, as shown in the figure

This allows you to use the etc. command when Appium starts  adb android .

When these three points are all done, we need to look at the situation of the devices, because before running the appium test script, we must ensure that the devices are connected. So what to do: start the emulator or connect to the phone

  • simulator

    Make sure that hw.battery=yes in your AVD's config.ini.
    For example, my AVD is called appium, so I have to edit  $HOME/.android/avd/appium.avd/config.ini the file to ensure hw.battery=yes

  • real machine

    You only need to connect the USB on the Mac, no need to install the driver. But I have encountered a problem, the Hisense phone cannot be recognized.
    This needs to  $HOME/.android/adb_usb.ini add vendor id in.

    ➜  ~  cat .android/adb_usb.ini
    # ANDROID 3RD PARTY USB VENDOR ID LIST -- DO NOT EDIT.
    # USE 'android update adb' TO GENERATE.
    # 1 USB VENDOR ID PER LINE.
    0x109b # 海信手机的 VENDOR ID + 0x
    

Then we run  adb devices -l,

➜  ~  adb devices -l
List of devices attached
emulator-5554          device product:sdk model:sdk device:generic
e912s                  device usb:24100000

Whether PS Appium supports multiple devices has not yet been determined.

We turn off the emulator, because the emulator is too slow. (The sample code has been run on the emulator, and it can run normally.)

write a script to play

In fact, the code in the sample code is already very rich. Just copy one.


# -*- coding: utf-8 -*-
import os
import glob
import unittest
from selenium import webdriver


class TestMyGame(unittest.TestCase):

    def setUp(self):
        app = os.path.abspath(glob.glob(os.path.dirname(__file__)
                                        + './*.apk')[0])
        desired_caps = {
            'device': 'appium',
            'app': app,
            'app-package': 'com.example.android.contactmanager',
            'app-activity': '.ContactManager'
        }

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub',
                                       desired_caps)

    def test(self):
        driver = self.driver
        el = driver.find_element_by_name("Add Contact")
        el.click()
        textfields = driver.find_elements_by_tag_name("textfield")
        textfields[0].send_keys("My Name")
        textfields[2].send_keys("[email protected]")
        driver.find_element_by_name("Save").click()

    def tearDown(self):
        self.driver.quit()

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


ContactManager It is the code in the android sample. You can get it when you install the adt tool.
I put the built apk and the following script in a directory, and then run it.

When using the emulator, it works very well, except it is slow. But it was a tragedy on Hisense's real machine. Because when the input box is focused, the Sohu input method pops up, which interferes with the send_key method. There are issue records on github, but unfortunately there is no good solution.

  As someone who has been here, I also hope that everyone will avoid some detours

Here I will share with you some necessities of the way forward in automated testing, hoping to help you.

(software testing related materials, automated testing related materials, technical questions and answers, etc.)

I believe it can make you better progress!

Click on the small card below

Guess you like

Origin blog.csdn.net/Free355/article/details/131785814