Create Test for Android Calendar App with python and appium

import time
from appium import webdriver

def test_select_date():
    # Define desired capabilities for the Appium session
    desired_caps = {
        'platformName': 'Android',
        'platformVersion': '11.0',
        'deviceName': 'Android Emulator',
        'appPackage': 'com.android.calendar',
        'appActivity': '.AllInOneActivity',
        'automationName': 'UiAutomator2'
    }

    # Start an Appium session with the desired capabilities
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    # Wait for the app to load
    time.sleep(5)

    # Click on the "Create new event" button
    new_event_button = driver.find_element_by_id("com.android.calendar:id/floating_action_button")
    new_event_button.click()

    # Wait for the new event screen to load
    time.sleep(2)

    # Click on the "Start date" field
    start_date_field = driver.find_element_by_id("com.android.calendar:id/start_date")
    start_date_field.click()

    # Get the current date and time
    current_time = time.localtime()
    current_year = current_time.tm_year
    current_month = current_time.tm_mon
    current_day = current_time.tm_mday

    # Calculate the date for next week
    next_week = time.mktime((current_year, current_month, current_day + 7, 0, 0, 0, 0, 0, 0))
    next_week_year = time.localtime(next_week).tm_year
    next_week_month = time.localtime(next_week).tm_mon
    next_week_day = time.localtime(next_week).tm_mday

    # Select the date for next week
    year_selector = driver.find_element_by_id("android:id/date_picker_header_year")
    year_selector.click()
    driver.find_element_by_xpath(f"//android.widget.TextView[@text='{next_week_year}']")\
          .click()

    month_selector = driver.find_element_by_id("android:id/date_picker_header_date")
    month_selector.click()
    driver.find_element_by_xpath(f"//android.widget.TextView[@text='{next_week_month}']")\
          .click()

    day_selector = driver.find_element_by_xpath("//android.view.View[@content-desc='Day of the week']" \
                                                f"[@text='{next_week_day}']")
    day_selector.click()

    # Verify that the selected date is correct
    start_date_field_text = start_date_field.text
    expected_date_text = f"{next_week_month}/{next_week_day}/{next_week_year}"
    assert start_date_field_text == expected_date_text, \
           f"Expected date {expected_date_text}, but got {start_date_field_text}"

    # Quit the Appium session
    driver.quit()

Last updated