Run the sample web app Python test

Step 1: Setup environment

Requirements

  • Python 3
  • Pytest framework
  • Appium client
  • Access key
    • You can generate an Access key using the following instruction: Access key
  • Billing unit
    • The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
      More information can be found here.

Step 2: Upload and install an App

Before running tests, several preparations are required:

  • Upload the app (only once).
  • Find and take a Device
  • Install the App

All of these actions can be performed automatically via API.
For more details, please refer to the Upload and Install document.

Step 3: Run sample test

After setup up the environment and installing the native App, you can run your first test

import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Constants
PROJECT_NAME = "<TEAM_CODE>"
ACCESS_KEY = "mobitru_ak_..."
APPIUM_HUB = "app.mobitru.com"
DEVICE_UDID = "38210DLJH0023E"
APP_PACKAGE = "com.epam.mobitru"
APP_ACTIVITY = ".MainActivity"
USER_NAME = "testuser@mobitru.com"
PASSWORD = "password1"
DEFAULT_IMPL_TIMEOUT_SEC = 3
DEFAULT_WAIT_TIMEOUT_SEC = 10


@pytest.fixture(scope="function")
def driver():
    """Fixture to set up and tear down the Appium driver."""
    options = {
        "platformName": "Android",
        "automationName": "UIAutomator2",
        "udid": DEVICE_UDID,
        "appPackage": APP_PACKAGE,
        "appActivity": APP_ACTIVITY,
        "noReset": True
    }

    driver = webdriver.Remote(
        command_executor=f"https://{PROJECT_NAME}:{ACCESS_KEY}@{APPIUM_HUB}/wd/hub",
        desired_capabilities=options
    )

    # Implicit wait for devices with low performance
    driver.implicitly_wait(DEFAULT_IMPL_TIMEOUT_SEC)

    yield driver

    # Teardown
    driver.quit()


def test_demo(driver):
    """Test case for demo."""
    driver.find_element(AppiumBy.XPATH, "//*[@resource-id='com.epam.mobitru:id/login_email']//android.widget"
                                        ".EditText").send_keys(
        USER_NAME)
    driver.find_element(AppiumBy.XPATH,
                        "//*[@resource-id='com.epam.mobitru:id/login_password']//android.widget.EditText").send_keys(
        PASSWORD)
    driver.find_element(AppiumBy.XPATH, "//*[@resource-id='com.epam.mobitru:id/login_signin']").click()

    WebDriverWait(driver, DEFAULT_WAIT_TIMEOUT_SEC).until(
        EC.presence_of_element_located((MobileBy.XPATH, "//*[@resource-id='com.epam.mobitru:id/category']"))
    )
import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Constants
PROJECT_NAME = "<TEAM_CODE>"
ACCESS_KEY = "mobitru_ak_..."
APPIUM_HUB = "app.mobitru.com"
DEVICE_UDID = "00008101-001608482602001E"
BUNDLE_ID = "com.epam.mobitru.demoapp"
USER_NAME = "testuser@mobitru.com"
PASSWORD = "password1"
DEFAULT_IMPL_TIMEOUT_SEC = 3
DEFAULT_WAIT_TIMEOUT_SEC = 10


@pytest.fixture(scope="function")
def driver():
    """Fixture to set up and tear down the Appium driver."""
    options = {
        "platformName": "iOS",
        "automationName": "XCUITest",
        "udid": DEVICE_UDID,
        "bundleId": BUNDLE_ID
    }

    driver = webdriver.Remote(
        command_executor=f"https://{PROJECT_NAME}:{ACCESS_KEY}@{APPIUM_HUB}/wd/hub",
        desired_capabilities=options
    )

    # Implicit wait for devices with low performance
    driver.implicitly_wait(DEFAULT_IMPL_TIMEOUT_SEC)

    yield driver

    # Teardown
    driver.quit()


def test_demo(driver):
    """Test case for demo."""
    driver.find_element(AppiumBy.XPATH, "//XCUIElementTypeTextField[starts-with(@name,'Login')]").send_keys(
        USER_NAME)
    driver.find_element(AppiumBy.XPATH,
                        "//XCUIElementTypeSecureTextField[starts-with(@name,'Password')]").send_keys(
        PASSWORD)
    driver.find_element(AppiumBy.XPATH, "//XCUIElementTypeButton[starts-with(@name,'Sign in')]").click()

    WebDriverWait(driver, DEFAULT_WAIT_TIMEOUT_SEC).until(
        EC.presence_of_element_located((MobileBy.XPATH, "//*[starts-with(@name,'productHeaderViewLabel')]"))
    )
Scroll to Top