File indexing completed on 2024-04-21 16:29:27

0001 #!/usr/bin/env python3
0002 
0003 # SPDX-License-Identifier: MIT
0004 # SPDX-FileCopyrightText: 2021-2023 Harald Sitter <sitter@kde.org>
0005 
0006 import os
0007 import time
0008 import unittest
0009 from datetime import datetime
0010 
0011 from appium import webdriver
0012 from appium.options.common.base import AppiumOptions
0013 from appium.webdriver.common.appiumby import AppiumBy
0014 from selenium.webdriver.common.action_chains import ActionChains
0015 
0016 
0017 class TextInputTest(unittest.TestCase):
0018 
0019     @classmethod
0020     def setUpClass(self):
0021         options = AppiumOptions()
0022         # The app capability may be a command line or a desktop file id.
0023         options.set_capability("app", f"{os.getenv('QML_EXEC')} {os.path.dirname(os.path.realpath(__file__))}/textinput.qml")
0024         # Boilerplate, always the same
0025         self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', options=options)
0026         # Set a timeout for waiting to find elements. If elements cannot be found
0027         # in time we'll get a test failure. This should be somewhat long so as to
0028         # not fall over when the system is under load, but also not too long that
0029         # the test takes forever.
0030         self.driver.implicitly_wait = 10
0031 
0032     @classmethod
0033     def tearDownClass(self):
0034         # Make sure to terminate the driver again, lest it dangles.
0035         self.driver.quit()
0036 
0037     def test_initialize(self):
0038         element = self.driver.find_element(AppiumBy.NAME, "input")
0039         element.send_keys("1;{)!#@")
0040         self.assertEqual(element.text, "1;{)!#@")
0041         element.clear()
0042         time.sleep(1)
0043         self.assertEqual(element.text, "")
0044 
0045         # element implicitly has focus right now, test that we can just type globally
0046         ActionChains(self.driver).send_keys("1;{)!#@").perform()
0047         self.assertEqual(element.text, "1;{)!#@")
0048         element.clear()
0049         time.sleep(1)
0050         self.assertEqual(element.text, "")
0051 
0052     def test_pause(self):
0053         element = self.driver.find_element(AppiumBy.NAME, "input")
0054         before_time = datetime.now().timestamp()
0055 
0056         ActionChains(self.driver).send_keys("123").pause(5).send_keys("456").perform()
0057 
0058         after_time = datetime.now().timestamp()
0059         self.assertEqual(element.text, "123456")
0060         self.assertGreaterEqual(after_time - before_time, 5.0)
0061         element.clear()
0062 
0063 
0064 if __name__ == '__main__':
0065     unittest.main()