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.
- The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
Step 2: Run sample test
After setup up the environment, you can run your first web test
import pytest
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui 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"
PLATFORM_VERSION = "15"
BROWSER_NAME = "chrome"
DEVICE_NAME = "GOOGLE Pixel 8"
OPEN_URL = "https://mobitru.com/"
DEFAULT_PAGE_LOAD_TIMEOUT_SEC = 30
DEFAULT_IMPL_TIMEOUT_SEC = 3
DEFAULT_WAIT_TIMEOUT_SEC = 10
@pytest.fixture(scope="function")
def driver():
"""Fixture for setting up and tearing down the Appium driver."""
desired_capabilities = {
"platformName": "Android",
"automationName": "UIAutomator2",
"platformVersion": PLATFORM_VERSION,
"browserName": BROWSER_NAME,
"deviceName": DEVICE_NAME,
}
# Initialize the Appium driver
driver = webdriver.Remote(
command_executor=f"https://{PROJECT_NAME}:{ACCESS_KEY}@{APPIUM_HUB}/wd/hub",
desired_capabilities=desired_capabilities
)
driver.set_page_load_timeout(DEFAULT_PAGE_LOAD_TIMEOUT_SEC)
driver.implicitly_wait(DEFAULT_IMPL_TIMEOUT_SEC)
yield driver
# Cleanup
driver.quit()
def test_appium_demo(driver):
"""Test to verify navigation to Google and basic checks."""
# Ensure the app is a browser
assert driver.contexts, f"Focus is not on '{BROWSER_NAME}'"
# Navigate to the URL
driver.get(OPEN_URL)
# Wait for the Mobitru main page is opened
WebDriverWait(driver, DEFAULT_WAIT_TIMEOUT_SEC).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, "button[class*='demo']")),
message="Page was not loaded"
)
# Verify the current URL
assert driver.current_url == OPEN_URL, "Current URL is incorrect."
# Verify the page title
assert driver.title == "Mobitru: Intelligent Real Device Cloud for Mobile and Cross-Browser Testing"
import pytest
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui 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"
PLATFORM_VERSION = "18.2"
BROWSER_NAME = "safari"
DEVICE_NAME = "IPHONE iPhone17,1"
OPEN_URL = "https://mobitru.com/"
DEFAULT_PAGE_LOAD_TIMEOUT_SEC = 30
DEFAULT_IMPL_TIMEOUT_SEC = 3
DEFAULT_WAIT_TIMEOUT_SEC = 10
@pytest.fixture(scope="function")
def driver():
"""Fixture for setting up and tearing down the Appium driver."""
desired_capabilities = {
"platformName": "iOS",
"automationName": "XCUITest",
"platformVersion": PLATFORM_VERSION,
"browserName": BROWSER_NAME,
"deviceName": DEVICE_NAME,
}
# Initialize the Appium driver
driver = webdriver.Remote(
command_executor=f"https://{PROJECT_NAME}:{ACCESS_KEY}@{APPIUM_HUB}/wd/hub",
desired_capabilities=desired_capabilities
)
driver.set_page_load_timeout(DEFAULT_PAGE_LOAD_TIMEOUT_SEC)
driver.implicitly_wait(DEFAULT_IMPL_TIMEOUT_SEC)
yield driver
# Cleanup
driver.quit()
def test_appium_demo(driver):
"""Test to verify navigation to Google and basic checks."""
# Ensure the app is a browser
assert driver.contexts, f"Focus is not on '{BROWSER_NAME}'"
# Navigate to the URL
driver.get(OPEN_URL)
# Wait for the Mobitru main page is opened
WebDriverWait(driver, DEFAULT_WAIT_TIMEOUT_SEC).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, "button[class*='demo']")),
message="Page was not loaded"
)
# Verify the current URL
assert driver.current_url == OPEN_URL, "Current URL is incorrect."
# Verify the page title
assert driver.title == "Mobitru: Intelligent Real Device Cloud for Mobile and Cross-Browser Testing"