Skip to content

Python Example 2

Goal to Achieve

Do login with invalid credentials on AgileTV app on an Android Smartphone using Python and take a screenshot.

AgileTV

Steps to reproduce:

  • Unlock the Device using PIN.
  • Install AgileTV app from apk file.
  • Open AgileTV app.
  • Wait until username input appears on screen.
  • Fill username, password and click login button.
  • Take a screenshot

Prerequisites

Project Setup

  1. Create a project folder named android-python-2 with two files, as follows:

    └── android-python-2/
       ├── agiletv-login.py
       └── requirements.txt
  2. Copy the following content to the requirements.txt file:

    txt
    Appium-Python-Client
  3. Copy the following content to the agiletv-login.py file:

    python
    import time
    import unittest
    from appium.webdriver.webdriver import WebDriver
    from appium.options.android.uiautomator2.base import UiAutomator2Options
    from appium.webdriver.common.appiumby import AppiumBy
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    capabilities = dict(
        platformName='Android',
        automationName='uiautomator2',
        udid='000000000', # <-- Change this to your device's ID
        autoGrantPermissions=True,
        autoUnlock=True,
        unlockType='pin',
        unlockKey='123456', # <-- Change this to your device's PIN
        app='C:/Users/agile/Downloads/Agile_TV_mmMobileProdStd_release_21541.apk', # <-- Change this to your APK file path
    )
    
    appium_server_url = 'http://localhost:4723'
    
    class TestAppium(unittest.TestCase):
        def setUp(self) -> None:
            self.driver = WebDriver(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))
            self.driver.implicitly_wait(30)
    
        def tearDown(self) -> None:
            if self.driver:
                self.driver.quit()
    
        def test_login_false_credentials(self) -> None:
            id_prefix = 'com.agilecontent.agiletv.mobile:id'
    
            self.driver.find_element(by=AppiumBy.ID, value=f'{id_prefix}/login_user_input').send_keys('testuser')
            self.driver.find_element(by=AppiumBy.ID, value=f'{id_prefix}/login_password_input').send_keys('p4ssw0rd')
            self.driver.find_element(by=AppiumBy.ID, value=f'{id_prefix}/primary_button_view').click()   
    
            time.sleep(8)
            self.driver.save_screenshot('screenshot.png')
    
    if __name__ == '__main__':
        unittest.main()
  4. Initialize the Python virtual environment. Follow the instructions here.

  5. Install the dependencies:

    shell
    pip install -r requirements.txt

Capabilities

Before running, you need to adjust the capabilities dictionary:

  • udid:
    Replace '000000000' with the ID of your connected device.
    You can find this ID as explained here.
  • unlockKey:
    Replace '123456' with the PIN of the device you are using for the test.

Running the script

To run the script, execute the following command in your terminal (with the virtual environment activated):

shell
python agiletv-login.py