Run the sample web app visual 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 visual test
import os
import httpx
import requests
import pytest
import urllib3
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.client_config import ClientConfig
from selenium.webdriver.remote.remote_connection import RemoteConnection
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Constants
PROJECT_NAME = "--------- BILLING UNIT -----------"
ACCESS_KEY = "--------- YOUR ACCESS KEY -----------"
OPEN_URL = "https://mobitru.com/"
DEFAULT_PAGE_LOAD_TIMEOUT_SEC = 30
DEFAULT_IMPL_TIMEOUT_SEC = 3
DEFAULT_WAIT_TIMEOUT_SEC = 10
SUITE_NAME = "python web demo tests"
_original_client = httpx.Client
def _patched_client(*args, **kwargs):
kwargs['verify'] = False
return _original_client(*args, **kwargs)
httpx.Client = _patched_client
@pytest.fixture(scope="function")
def driver():
options = webdriver.ChromeOptions()
mobitru_options = {
'enableVideo': False,
'sessionTimeout': '1m',
'visualTesting': {
'name': "Build 1",
'project': "Python Automation Demo for JavaExecutor",
'branch': "main",
'failOnError': False
}
}
options.set_capability('mobitru:options', mobitru_options)
cloud_url = f"https://{PROJECT_NAME}:{ACCESS_TOKEN}@browserhub-us.mobitru.com/wd/hub"
print(options.capabilities)
os.environ['WDM_SSL_VERIFY'] = '0'
driver = webdriver.Remote(cloud_url, options=options)
yield driver
# Cleanup
driver.quit()
def test_selenium_demo(driver):
"""Test to verify navigation to mobitru and basic checks."""
test_name = 'demo web selenium test'
# Ensure the app is a browser
# 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: Real Devices, Browsers & AI - in One Enterprise Testing Platform"
submit_snapshot(driver, result_name='after_open_url', test_name=test_name, suite_name=SUITE_NAME)
def submit_snapshot(driver, result_name, test_name, suite_name):
snapshot_data = {
'name': result_name,
'testName': test_name,
'suiteName': suite_name,
}
driver.execute_script('mobitru:visualCheck', snapshot_data)