Pricing
Appium Local
Help Documents menu

Appium Local

Updated on: 2022-09-26 16:13

Table of Contents

Introduction

  1. Setup
  2. Upload your app
  3. Select a device
  4. Configure Appium script
  5. Run Appium test and view result analysis

Running Appium Test on Local Environment with WeTest Cloud Devices

WeTest provides ‘Appium Local’ feature that allows you to run Appium test on your local environment with WeTest cloud mobile devices and get Test Analysis after you started your test session locally.
More than that, you can also submit and run Appium test on WeTest cloud, which is a more stable environment and allows you run your Appium tests in parallel against a wide range of real devices. For more information regarding running on WeTest Cloud, please refer to Running Appium Test on WeTest Cloud Servers.

The following are steps to run Appium Test on client side.

1. Setup

1) Install Appium client

python3 --version # make sure python3 is installed.
pip3 --version # make sure pip3 is installed.
git clone https://github.com/WeTestQuality/WeTest-Automated-Testing.git
#For Android
pip3 install -r samples/Android/Appium_local/requirements.txt 
#For iOS
pip3 install -r samples/iOS/Appium_local/requirements.txt 

2) Obtain Secret ID and Secret Key

  • Access to Account Settings to obtain your user Secret ID and Secret Key that will be used in the capabilities.

3) Obtain Project ID

4) Prepare your app under test

WeTest supports .apk and .aab for Android and .ipa for iOS.

2. Upload your app

Option a) Upload an app through UI

You can also access to Automation - Create Test page to upload app files(.apk, .aab, .ipa) from the “App Upload” panel.
Upload App

After you uploaded, you should see the “App id” field in the apps table on the same page. We will use the App id field value as the wetest_app_id capability value.

Option b) Upload an app by using the REST API

You can upload your app(.apk, aab, or .ipa) file by using API /v1/platform/upload/storage

Please note that the ‘fid’ field will be the value of the wetest_app_id capability.

3. Select a device

Note: If you are a free-trial user, you can only choose real devices in Android Trial Device Farm and iOS Trial Device Farm.

Option a) Obtain device info through UI

  1. Access to Real Device - Select Device page.
  2. Select your preferred filters
  3. Click the Information Icon prefixed to the Name field in the device list
  4. Click Appium Capabilities tab.
    Click Appium Capabilities

Option b) Obtain device info via REST API

  1. Retrieve cloud list by using API /v1/users/clouds.
  2. Retrieve device list in a specific cloud by using API /v1/clouds/<cloud_id>/devices
  3. Get device_id from the response device list.

4. Configure Appium script

WeTest support Appium desired capabilities along with WeTest desired capabilities.

Most WeTest desired capabilities are required or recommended. Please follow the descriptions below to set the capabilities.

WeTest Desired Capabilities

Capability Description
wetest_secret_id Required. A String value. Your WeTest account Secret ID. This is available in the Account Settings.
wetest_secret_key Required. A String value. Your WeTest account Secret Key. This is available in the Account Settings.
wetest_app_id Recommended. A String value. A unique identifier to an app you uploaded. Upload an app either through the Web-UI or using the REST API /v1/platform/upload/storage. The ‘fid’ field in the ‘data’ field of the response json is the wetest_app_id. When wetest_app_id is set, the specified app would be installed before running test.
wetest_device_id Required. An Integer value. The Device ID of a WeTest mobile device (either Android device or iOS device). You can retrieve all devices in a specified cloud from the REST API /v1/clouds/<cloud_id>/devices or Device Appium Capabilities view.
wetest_project_id Required. A String value. The Project ID. This is available in the Device Appium Capabilities and Project Management view. You can also retrieve project list from the REST API /v1/users/projects.
wetest_test_timeout Required. An Integer value. The timeout for the whole test execution(in seconds). WeTest would terminate a session if it run more than wetest_test_timeout seconds. This capability must be greater than 300 seconds.
wetest_ios_resign Optional. A Boolean value. Default value is True. WeTest would resign the iOS app if wetest_ios_resign is True.

Appium Desired Capabilities

The following capabilities are required for Android
  • platformName, it’s Android.
  • automationName, it’s either Appium, UiAutomator1, or UiAutomator2.
  • deviceName, it’s Android Phone.
The following capabilities are required for iOS
  • platformName, it’s iOS.
  • automationName, it’s XCUITest.
  • deviceName, it’s iPhone.
  • bundleId, it’s your app’s bundle ID.

For more information, please refer to Appium Desired Capabilities.

WeTest Appium Server URL

In order to initialize an Appium driver, use a remote WeTest Appium server URL with your configured capabilities:

  https://api.paas.wetest.net/wd/hub

Script Example

Here is an python script example to run Appium test with WeTest cloud device. You can also get it from the WeTest Github
repository appium_local_android.py
and appium_local_ios.py

Android Sample Script
# -*- coding: UTF-8 -*-
import os
import time
import unittest

from appium import webdriver
from datetime import datetime


class WeTestAppiumTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        desired_caps = {

            # Appium native capabilities
            'platformName': 'Android',  # Which mobile OS platform to use
            'deviceName': 'Android Phone',  # The kind of mobile device or emulator to use
            'automationName': 'UiAutomator2',  # Which automation engine to use
            'newCommandTimeout': 300,
            # How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session

            # WeTest capabilities
            'wetest_secret_id': 'YourSecretId',  # Replace with your secret id. This is available in the Accounts view.
            'wetest_secret_key': 'YourSecretKey',  # Replace with your secret key. This is available in the Accounts view.
            'wetest_app_id': 'YourWetestAppId',  # Replace with your app's WeTest app id. Specifies the Application file (.apk /.aab) to be installed on the device.
            'wetest_device_id': 1234,  # Replace with the WeTest device id you chosen.
            'wetest_project_id': 'YourProjectId',  # Replace with your WeTest project id.
            'wetest_test_timeout': 1200,  # The timeout for the whole test execution (in seconds)
        }

        # WeTest Appium WebDriver address
        # When running Appium locally, the web driver address is running on a localhost (http://localhost:4723/wd/hub).
        # When running the test from your local machine against a WeTest cloud device, you need to change the Appium server location.
        print("Waiting for response, the process includes WebDriver and Device initialization,App installation, this "
              "typically takes a few mins. Creating at", datetime.now())
        WeTestAppiumTest.driver = webdriver.Remote("https://api.paas.wetest.net/wd/hub", desired_caps)
        print("Connecting to WeTest WebDriver successfully with Session ID:", WeTestAppiumTest.driver.session_id,
              ". Finished at ", datetime.now())

    @classmethod
    def tearDownClass(cls):
        print('Quiting')
        WeTestAppiumTest.driver.quit()

    def setUp(self):
        print('case setup')

    def tearDown(self):
        print('case tear down')

    # your test cases start here
    # this function is just a sample test case for com.tencent.wetestdemo package
    def test_login_success(self):
        print('test_login_success start.')
        username = self.driver.find_element_by_id("com.tencent.wetestdemo:id/username")
        username.send_keys("wetestname")
        pwd = self.driver.find_element_by_id("com.tencent.wetestdemo:id/password")
        pwd.send_keys("wetestpwd")
        time.sleep(5)
        login = self.driver.find_element_by_id("com.tencent.wetestdemo:id/login")
        login.click()
        time.sleep(5)
        print('test_login_success end.')
    # your test cases end here


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


iOS Sample Script
# -*- coding: UTF-8 -*-

import os
import time
import unittest

from appium import webdriver
from datetime import datetime


class WeTestAppiumTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        desired_caps = {

            # Appium native capabilities
            'bundleId': 'YouAppBundleID',  # Replace YouAppBundleID with your app bundle id(e.g. com.wetest.demo.db).
            'platformName': 'iOS',  # Which mobile OS platform to use
            'deviceName': 'iPhone',  # The kind of mobile device to use
            'automationName': 'XCUITest',  # Which automation engine to use
            'newCommandTimeout': 300, # How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session

            # WeTest capabilities
            'wetest_secret_id': 'YourSecretId', # Replace with your secret id. This is available in the Accounts view.
            'wetest_secret_key': 'YourSecretKey', # Replace with your secret key. This is available in the Accounts view.
            'wetest_app_id': 'YourWetestAppId', # Replace with your app's WeTest app id. Specifies the Application file (.apk /.aab) to be installed on the device.
            'wetest_device_id': 1234,  # Replace with the WeTest device id you chosen.
            'wetest_project_id': 'YourProjectId',  # Replace with your WeTest project id.
            'wetest_test_timeout': 1200,  # The timeout for the whole test execution (in seconds)
            'wetest_ios_resign': True, # Indicate whether to resign the iOS app specified in wetest_app_id before installing it. If not specified, the default value is True.
        }

        # WeTest Appium WebDriver address
        # When running Appium locally, the web driver address is running on a localhost (http://localhost:4723/wd/hub).
        # When running the test from your local machine against a WeTest cloud device, you need to change the Appium server location.
        print("Waiting for response, the process includes WebDriver and Device initialization,App installation, this "
              "typically takes a few mins. Creating at", datetime.now())
        WeTestAppiumTest.driver = webdriver.Remote("https://api.paas.wetest.net/wd/hub", desired_caps)
        print("Connecting to WeTest WebDriver successfully with Session ID:", WeTestAppiumTest.driver.session_id,
              ". Finished at ", datetime.now())

    @classmethod
    def tearDownClass(cls):
        print('Quiting')
        WeTestAppiumTest.driver.quit()

    def setUp(self):
        print('case setup')

    def tearDown(self):
        print('case tear down')

    # your test cases start here
    # this function is just a sample test case for com.tencent.wetestdemo package
    def test_login_success(self):
        print('test_login_success start.')
        username = self.driver.find_element_by_xpath("//XCUIElementTypeTextField")
        username.send_keys("WeTest@wetest.net")
        password = self.driver.find_element_by_xpath("//XCUIElementTypeSecureTextField")
        password.send_keys("123456")
        time.sleep(6)
        self.driver.find_element_by_name("Sign In").click()
        time.sleep(6)
        print('test_login_success end.')
    # your test cases end here


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


5. Run Appium test and view result analysis

Run the script configured in above steps and get result analysis from Test Analysis page.