Warning, file /plasma/plasma-workspace/appiumtests/digitalclocktest.py was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #!/usr/bin/env python3
0002 
0003 # SPDX-License-Identifier: MIT
0004 # SPDX-FileCopyrightText: 2021-2022 Harald Sitter <sitter@kde.org>
0005 # SPDX-FileCopyrightText: 2023 Marco Martin <mart@kde.org>
0006 
0007 import unittest
0008 from datetime import date
0009 from typing import Final
0010 
0011 from appium import webdriver
0012 from appium.options.common.base import AppiumOptions
0013 from appium.webdriver.common.appiumby import AppiumBy
0014 from dateutil.relativedelta import relativedelta
0015 from selenium.webdriver.support.ui import WebDriverWait
0016 
0017 WIDGET_ID: Final = "org.kde.plasma.digitalclock"
0018 
0019 
0020 class DigitalClockTests(unittest.TestCase):
0021 
0022     @classmethod
0023     def setUpClass(cls):
0024         options = AppiumOptions()
0025         options.set_capability("app", f"plasmawindowed -p org.kde.plasma.nano {WIDGET_ID}")
0026         options.set_capability("timeouts", {'implicit': 10000})
0027         cls.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', options=options)
0028         # Open Applet
0029         cls.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="expandApplet").click()
0030 
0031     def setUp(self):
0032         self.driver.find_element(by=AppiumBy.NAME, value="Today").click()
0033         self.assertEqual(self.compareMonthLabel(date.today()), True)
0034 
0035     def tearDown(self):
0036         if not self._outcome.result.wasSuccessful():
0037             self.driver.get_screenshot_as_file(f"failed_test_shot_digitalclocktest_{self.id()}.png")
0038 
0039     @classmethod
0040     def tearDownClass(cls) -> None:
0041         """
0042         Make sure to terminate the driver again, lest it dangles.
0043         """
0044         cls.driver.quit()
0045 
0046     def assertResult(self, actual, expected):
0047         wait = WebDriverWait(self.driver, 20)
0048         wait.until(lambda x: self.getresults() == expected)
0049         self.assertEqual(self.getresults(), expected)
0050 
0051     def compareMonthLabel(self, dateToTest):
0052         monthLabel = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="monthHeader")
0053         today = date.today()
0054         monthString = ""
0055         if dateToTest.year == today.year:
0056             monthString = dateToTest.strftime("%B")
0057         else:
0058             monthString = dateToTest.strftime("%B %Y")
0059         return monthLabel.text == monthString
0060 
0061     def test_next_month(self):
0062         nextMonthDate = date.today() + relativedelta(months=1)
0063 
0064         self.driver.find_element(by=AppiumBy.NAME, value="Next Month").click()
0065 
0066         wait = WebDriverWait(self.driver, 50)
0067         wait.until(lambda x: self.compareMonthLabel(nextMonthDate))
0068         self.assertEqual(self.compareMonthLabel(nextMonthDate), True)
0069 
0070     def test_prev_month(self):
0071         lastMonthDate = date.today() - relativedelta(months=1)
0072 
0073         self.driver.find_element(by=AppiumBy.NAME, value="Previous Month").click()
0074 
0075         wait = WebDriverWait(self.driver, 50)
0076         wait.until(lambda x: self.compareMonthLabel(lastMonthDate))
0077         self.assertEqual(self.compareMonthLabel(lastMonthDate), True)
0078 
0079     def test_months_view(self):
0080         dateAugust = date.today()
0081         dateAugust = dateAugust.replace(month=8)
0082 
0083         self.driver.find_element(by=AppiumBy.NAME, value="Months").click()
0084 
0085         self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="calendarCell-{}-{}".format(dateAugust.year, dateAugust.month)).click()
0086         wait = WebDriverWait(self.driver, 50)
0087         wait.until(lambda x: self.compareMonthLabel(dateAugust))
0088         self.assertEqual(self.compareMonthLabel(dateAugust), True)
0089 
0090     def test_years_view(self):
0091         dateFuture = date.today() + relativedelta(years=2)
0092 
0093         self.driver.find_element(by=AppiumBy.NAME, value="Years").click()
0094 
0095         self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="calendarCell-{}".format(dateFuture.year)).click()
0096         wait = WebDriverWait(self.driver, 50)
0097         wait.until(lambda x: self.compareMonthLabel(dateFuture))
0098         self.assertEqual(self.compareMonthLabel(dateFuture), True)
0099 
0100 
0101 if __name__ == '__main__':
0102     suite = unittest.TestLoader().loadTestsFromTestCase(DigitalClockTests)
0103     unittest.TextTestRunner(verbosity=2).run(suite)