File indexing completed on 2024-03-24 17:21:54

0001 #!/usr/bin/env python3
0002 
0003 # SPDX-License-Identifier: MIT
0004 # SPDX-FileCopyrightText: 2016 Microsoft Corporation. All rights reserved.
0005 # SPDX-FileCopyrightText: 2021-2022 Harald Sitter <sitter@kde.org>
0006 
0007 import unittest
0008 from appium import webdriver
0009 from appium.webdriver.common.appiumby import AppiumBy
0010 from appium.options.common.base import AppiumOptions
0011 import selenium.common.exceptions
0012 from selenium.webdriver.support.ui import WebDriverWait
0013 
0014 
0015 class SimpleCalculatorTests(unittest.TestCase):
0016 
0017     @classmethod
0018     def setUpClass(self):
0019         options = AppiumOptions()
0020         # The app capability may be a command line or a desktop file id.
0021         options.set_capability("app", "org.kde.kcalc.desktop")
0022         # Boilerplate, always the same
0023         self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', options=options)
0024         # Set a timeout for waiting to find elements. If elements cannot be found
0025         # in time we'll get a test failure. This should be somewhat long so as to
0026         # not fall over when the system is under load, but also not too long that
0027         # the test takes forever.
0028         self.driver.implicitly_wait = 10
0029 
0030     @classmethod
0031     def tearDownClass(self):
0032         # Make sure to terminate the driver again, lest it dangles.
0033         self.driver.quit()
0034 
0035     def setUp(self):
0036         self.driver.find_element(by=AppiumBy.NAME, value="AC").click()
0037         wait = WebDriverWait(self.driver, 20)
0038         wait.until(lambda x: self.getresults() == '0')
0039 
0040     def getresults(self):
0041         displaytext = self.driver.find_element(by='description', value='Result Display').text
0042         return displaytext
0043 
0044     def assertResult(self, actual, expected):
0045         wait = WebDriverWait(self.driver, 20)
0046         try:
0047             wait.until(lambda x: self.getresults() == expected)
0048         except selenium.common.exceptions.TimeoutException:
0049             pass
0050         self.assertEqual(self.getresults(), expected)
0051 
0052     def test_initialize(self):
0053         self.driver.find_element(by=AppiumBy.NAME, value="AC").click()
0054         self.driver.find_element(by=AppiumBy.NAME, value="7").click()
0055         self.assertResult(self.getresults(), "7")
0056 
0057     def test_addition(self):
0058         self.driver.find_element(by=AppiumBy.NAME, value="1").click()
0059         self.driver.find_element(by=AppiumBy.NAME, value="+").click()
0060         self.driver.find_element(by=AppiumBy.NAME, value="7").click()
0061         self.driver.find_element(by=AppiumBy.NAME, value="=").click()
0062         self.assertResult(self.getresults(), "8")
0063 
0064     def test_combination(self):
0065         self.driver.find_element(by=AppiumBy.NAME, value="7").click()
0066         self.driver.find_element(by=AppiumBy.NAME, value="×").click()
0067         self.driver.find_element(by=AppiumBy.NAME, value="9").click()
0068         self.driver.find_element(by=AppiumBy.NAME, value="+").click()
0069         self.driver.find_element(by=AppiumBy.NAME, value="1").click()
0070         self.driver.find_element(by=AppiumBy.NAME, value="=").click()
0071         self.driver.find_element(by=AppiumBy.NAME, value="÷").click()
0072         self.driver.find_element(by=AppiumBy.NAME, value="8").click()
0073         self.driver.find_element(by=AppiumBy.NAME, value="=").click()
0074         self.assertResult(self.getresults(), "8")
0075 
0076     def test_division(self):
0077         # Using find element by name twice risks the driver finding the
0078         # result display text rather than finding the button. To avoid
0079         # that, execute the call once and store that as a local value.
0080         button8 = self.driver.find_element(by=AppiumBy.NAME, value="8")
0081         button8.click()
0082         button8.click()
0083         self.driver.find_element(by=AppiumBy.NAME, value="÷").click()
0084         button1 = self.driver.find_element(by=AppiumBy.NAME, value="1")
0085         button1.click()
0086         button1.click()
0087         self.driver.find_element(by=AppiumBy.NAME, value="=").click()
0088         self.assertResult(self.getresults(), "8")
0089 
0090     def test_multiplication(self):
0091         self.driver.find_element(by=AppiumBy.NAME, value="9").click()
0092         self.driver.find_element(by=AppiumBy.NAME, value="×").click()
0093         self.driver.find_element(by=AppiumBy.NAME, value="9").click()
0094         self.driver.find_element(by=AppiumBy.NAME, value="=").click()
0095         self.assertResult(self.getresults(), "81")
0096 
0097     def test_subtraction(self):
0098         self.driver.find_element(by=AppiumBy.NAME, value="9").click()
0099         self.driver.find_element(by=AppiumBy.NAME, value="−").click()
0100         self.driver.find_element(by=AppiumBy.NAME, value="1").click()
0101         self.driver.find_element(by=AppiumBy.NAME, value="=").click()
0102         self.assertResult(self.getresults(), "8")
0103 
0104 
0105 if __name__ == '__main__':
0106     unittest.main()