-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_calc.py
216 lines (174 loc) · 6.5 KB
/
lib_calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""lib_calc.py: Robot Framework Library for Calculator"""
import time
from logging import debug, error, info, warn
import atomacos
from var_calc import *
class OPERATOR:
"""
Define basic operators.
"""
ADD = '+'
SUB = '-'
MUL = '*'
DIV = '/'
class lib_calc:
"""
A helper library for calc robot.
"""
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
def __init__(self):
info('Initialzed the lib_calc.')
def get_result(self):
"""
Get the result in the display of the calculator.
:return: str, the number of result
"""
debug('Getting display result.')
result = self.main_display.AXValue
info('Succeeded to get display result: {}.'.format(result))
return result
def start_calc(self):
"""
Start the calculator app by bundle id.
:return: None
"""
debug('Starting calculator.app.')
atomacos.launchAppByBundleId(CALC_BUNDLE_ID)
time.sleep(1)
info('Succeeded to start calculator.app.')
self.get_ui_elements()
def get_ui_elements(self):
"""
Get the some interesting UI element references, and save them to the instance variables.
:return: None
"""
debug('Getting the UI element references.')
calculator = atomacos.getAppRefByBundleId('com.apple.calculator')
calc_window = calculator.AXMainWindow
ns_box = calc_window.AXChildren[0]
self.calc_box = calc_window.AXChildren[1]
self.main_display = ns_box.AXChildren[0]
self.button_clear = self.get_button(LABEL_CLEAR)
self.button_add = self.get_button(LABEL_ADD)
self.button_substract = self.get_button(LABEL_SUBSTRACT)
self.button_multiply = self.get_button(LABEL_MULTIPLY)
self.button_divide = self.get_button(LABEL_DIVIDE)
self.button_equals = self.get_button(LABEL_EQUALS)
self.button_negate = self.get_button(LABEL_NEGATE)
info('Succeeded to get the UI element references.')
def stop_calc(self):
"""
Stop the calculator app by bundle id.
:return: None
"""
debug('Stopping calculator.app.')
atomacos.terminateAppByBundleId(CALC_BUNDLE_ID)
info('Succeeded to stop calculator.app.')
def get_button(self,button_name):
"""
Get the button reference by button label name.
:param button_name: str, label of the button
:return: obj, button reference
"""
debug('Getting button reference for button {}.'.format(button_name))
button = self.calc_box.findFirst(AXRole='AXButton', AXDescription=button_name)
info('Succeeded to get button reference for button {}.'.format(button_name))
return button
def press_button(self, button):
"""
Perform button press.
:param button: obj, button reference
:return: None
"""
button_name = button.AXDescription
debug('Performing the {} button press.'.format(button_name))
button.Press()
info('Succeeded to performing the {} button press.'.format(button_name))
def clear_all(self):
"""
Perform clear button press to clear the result.
:return: None
"""
debug('Clear all.')
self.press_button(self.button_clear)
info('Succeeded to clear all.')
def get_number_button(self, number):
"""
Get button reference of a number.
:param number: int, number 0-9
:return: obj, button reference
"""
return self.get_button(NUMBERS[number])
def press_number_button(self, number):
"""
Perform a number button press.
:param number: int, number 0-9
:return: None
"""
self.press_button(self.get_number_button(number))
def check_result(self, expected_number):
"""
Check the current display result with the expected number.
:return: None
"""
debug('Checking the result with expected number {}.'.format(expected_number))
result = self.get_result()
assert result == str(expected_number), \
'Failed, expected result {}, result {}.'.format(expected_number, result)
info('Succeeded to check the result with expected number {}.'.format(expected_number))
def check_number(self, number):
"""
Check number button press functionality by comparing with the display result.
:param number: int, number 0-9
:return: None
"""
debug('Checking number button {} press.'.format(number))
button = self.get_number_button(number)
self.press_button(button)
self.check_result(number)
info('Succeeded to check number button {} press.'.format(number))
def input_digits(self, number):
"""
Input a integer number by performing number button presses for the digits.
:param number: int, number 0-9
:return: None
"""
for digit in str(abs(number)):
self.press_number_button(int(digit))
def check_binary_calc(self, a, operator, b, expected_result):
"""
Check binary calculation by comparing the expected result with the display result.
:param a: str, number 0-9
:param operator: str, basic operators containing +, -, *, /
:param b: str, number 0-9
:param expected_result: str, number 0-9
:return: None
"""
debug('Checking calculation: {} {} {} = {}.'.format(a, operator, b, expected_result))
a = int(a)
b = int(b)
self.input_digits(a)
if a < 0:
self.press_button(self.button_negate)
if operator == OPERATOR.ADD:
self.press_button(self.button_add)
elif operator == OPERATOR.SUB:
self.press_button(self.button_substract)
elif operator == OPERATOR.MUL:
self.press_button(self.button_multiply)
elif operator == OPERATOR.DIV:
self.press_button(self.button_divide)
else:
raise Exception('Not supported operator.')
self.input_digits(b)
if b < 0:
self.press_button(self.button_negate)
self.press_button(self.button_equals)
self.check_result(expected_result)
info('Succeeded to check calculation: {} {} {} = {}.'.format(a, operator, b, expected_result))
if __name__ == "__main__":
# Basic test
calc = lib_calc()
calc.start_calc()
calc.check_binary_calc(1, '+', 1, 2)
calc.stop_calc()