File indexing completed on 2024-04-21 05:44:58

0001 #!/usr/bin/env python3
0002 
0003 # SPDX-License-Identifier: MIT
0004 # SPDX-FileCopyrightText: 2021-2022 Harald Sitter <sitter@kde.org>
0005 
0006 import os
0007 import unittest
0008 
0009 from appium import webdriver
0010 from appium.options.common.base import AppiumOptions
0011 from appium.webdriver.common.appiumby import AppiumBy
0012 from appium.webdriver.webdriver import ExtensionBase
0013 from appium.webdriver.webelement import WebElement
0014 
0015 
0016 class SetValueCommand(ExtensionBase):
0017 
0018     def method_name(self):
0019         return 'set_value'
0020 
0021     def set_value(self, element: WebElement, value: str):
0022         """
0023         Set the value on this element in the application
0024         Args:
0025             value: The value to be set
0026         """
0027         data = {
0028             'id': element.id,
0029             'text': value,
0030         }
0031         return self.execute(data)['value']
0032 
0033     def add_command(self):
0034         return 'post', '/session/$sessionId/appium/element/$id/value'
0035 
0036 
0037 class ValueTest(unittest.TestCase):
0038 
0039     @classmethod
0040     def setUpClass(self):
0041         options = AppiumOptions()
0042         # The app capability may be a command line or a desktop file id.
0043         options.set_capability("app", f"{os.getenv('QML_EXEC')} {os.path.dirname(os.path.realpath(__file__))}/value.qml")
0044         # Boilerplate, always the same
0045         self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', extensions=[SetValueCommand], options=options)
0046         # Set a timeout for waiting to find elements. If elements cannot be found
0047         # in time we'll get a test failure. This should be somewhat long so as to
0048         # not fall over when the system is under load, but also not too long that
0049         # the test takes forever.
0050         self.driver.implicitly_wait = 10
0051 
0052     @classmethod
0053     def tearDownClass(self):
0054         # Make sure to terminate the driver again, lest it dangles.
0055         self.driver.quit()
0056 
0057     def test_initialize(self):
0058         slider = self.driver.find_element(AppiumBy.NAME, "slider")
0059         self.assertEqual(float(slider.get_attribute('value')), 25.0)
0060         self.driver.set_value(slider, 100)
0061         self.assertEqual(float(slider.get_attribute('value')), 100.0)
0062 
0063 
0064 if __name__ == '__main__':
0065     unittest.main()