File indexing completed on 2024-05-12 05:36:59

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 subprocess
0008 import unittest
0009 from datetime import date
0010 from typing import Final
0011 
0012 from appium import webdriver
0013 from appium.options.common.base import AppiumOptions
0014 from appium.webdriver.common.appiumby import AppiumBy
0015 from dateutil.relativedelta import relativedelta
0016 from selenium.webdriver.remote.webelement import WebElement
0017 from selenium.webdriver.support import expected_conditions as EC
0018 from selenium.webdriver.support.ui import WebDriverWait
0019 
0020 WIDGET_ID: Final = "org.kde.plasma.digitalclock"
0021 
0022 
0023 class DigitalClockTests(unittest.TestCase):
0024 
0025     driver: webdriver.Remote
0026 
0027     @classmethod
0028     def setUpClass(cls):
0029         options = AppiumOptions()
0030         options.set_capability("app", f"plasmawindowed -p org.kde.plasma.nano {WIDGET_ID}")
0031         options.set_capability("timeouts", {'implicit': 10000})
0032         cls.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', options=options)
0033         # Open Applet
0034         cls.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "digital-clock-compactrepresentation").click()
0035 
0036     def setUp(self):
0037         self.driver.find_element(by=AppiumBy.NAME, value="Today").click()
0038         self.assertEqual(self.compareMonthLabel(date.today()), True)
0039 
0040     def tearDown(self):
0041         if not self._outcome.result.wasSuccessful():
0042             self.driver.get_screenshot_as_file(f"failed_test_shot_digitalclocktest_{self.id()}.png")
0043 
0044     @classmethod
0045     def tearDownClass(cls) -> None:
0046         """
0047         Make sure to terminate the driver again, lest it dangles.
0048         """
0049         cls.driver.quit()
0050 
0051     def assertResult(self, actual, expected):
0052         wait = WebDriverWait(self.driver, 20)
0053         wait.until(lambda x: self.getresults() == expected)
0054         self.assertEqual(self.getresults(), expected)
0055 
0056     def compareMonthLabel(self, dateToTest):
0057         monthLabel = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="monthHeader")
0058         today = date.today()
0059         monthString = ""
0060         if dateToTest.year == today.year:
0061             monthString = dateToTest.strftime("%B")
0062         else:
0063             monthString = dateToTest.strftime("%B %Y")
0064         return monthLabel.text == monthString
0065 
0066     def test_1_next_month(self):
0067         nextMonthDate = date.today() + relativedelta(months=1)
0068 
0069         self.driver.find_element(by=AppiumBy.NAME, value="Next Month").click()
0070 
0071         wait = WebDriverWait(self.driver, 50)
0072         wait.until(lambda x: self.compareMonthLabel(nextMonthDate))
0073         self.assertEqual(self.compareMonthLabel(nextMonthDate), True)
0074 
0075     def test_1_prev_month(self):
0076         lastMonthDate = date.today() - relativedelta(months=1)
0077 
0078         self.driver.find_element(by=AppiumBy.NAME, value="Previous Month").click()
0079 
0080         wait = WebDriverWait(self.driver, 50)
0081         wait.until(lambda x: self.compareMonthLabel(lastMonthDate))
0082         self.assertEqual(self.compareMonthLabel(lastMonthDate), True)
0083 
0084     def test_1_months_view(self):
0085         dateAugust = date.today()
0086         dateAugust = dateAugust.replace(month=8)
0087 
0088         self.driver.find_element(by=AppiumBy.NAME, value="Months").click()
0089 
0090         self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="calendarCell-{}-{}".format(dateAugust.year, dateAugust.month)).click()
0091         wait = WebDriverWait(self.driver, 50)
0092         wait.until(lambda x: self.compareMonthLabel(dateAugust))
0093         self.assertEqual(self.compareMonthLabel(dateAugust), True)
0094 
0095     def test_1_years_view(self):
0096         dateFuture = date.today() + relativedelta(years=2)
0097 
0098         self.driver.find_element(by=AppiumBy.NAME, value="Years").click()
0099 
0100         self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="calendarCell-{}".format(dateFuture.year)).click()
0101         wait = WebDriverWait(self.driver, 50)
0102         wait.until(lambda x: self.compareMonthLabel(dateFuture))
0103         self.assertEqual(self.compareMonthLabel(dateFuture), True)
0104 
0105     def test_2_config_dialog_1_appearance(self) -> None:
0106         """
0107         Opens the config dialog
0108         """
0109         subprocess.check_call(["plasmawindowed", "--config"])
0110         wait = WebDriverWait(self.driver, 10)
0111         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Date format:")))
0112 
0113     def test_2_config_dialog_2_calendar_plugin_list(self) -> None:
0114         """
0115         Checks the calendar plugin list
0116         """
0117         self.driver.find_element(AppiumBy.NAME, "Calendar").click()
0118         wait = WebDriverWait(self.driver, 10)
0119         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Available Plugins:")))
0120         # Enable the plugin (BUG 480668)
0121         plugin_checkbox: WebElement = wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Holidays")))
0122         plugin_checkbox.click()
0123         # Switch back to Appearance so there will only be one "Holidays" match
0124         self.driver.find_element(AppiumBy.NAME, "Appearance").click()
0125         wait.until_not(lambda _: plugin_checkbox.is_displayed())
0126         # Switch to the calendar plugin
0127         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Holidays"))).click()
0128         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Search")))
0129 
0130     def test_2_config_dialog_3_timezones(self) -> None:
0131         """
0132         Checks the timezone list
0133         """
0134         self.driver.find_element(AppiumBy.NAME, "Time Zones").click()
0135         wait = WebDriverWait(self.driver, 10)
0136         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Add Time Zones…"))).click()
0137         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Add More Timezones")))
0138         self.driver.find_element(AppiumBy.NAME, "Search").send_keys("utc+03:00")
0139         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "UTC+03:00")))
0140 
0141 
0142 if __name__ == '__main__':
0143     unittest.main()