Python Example 2
Goal to Achieve
Do login with invalid credentials on AgileTV app on an Android Smartphone using Python and take a screenshot.

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
- Common environment steps to be completed.
- Android environment steps to be completed.
- Python installed on the computer.
- A connected Smartphone, as described here.
- Appium server running, as described here.
- An AgileTV app APK file.
Project Setup
Create a project folder named
android-python-2with two files, as follows:└── android-python-2/ ├── agiletv-login.py └── requirements.txtCopy the following content to the
requirements.txtfile:txtAppium-Python-ClientCopy the following content to the
agiletv-login.pyfile:pythonimport 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()Initialize the Python virtual environment. Follow the instructions here.
Install the dependencies:
shellpip 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