testcentricity_mobile 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,152 @@
1
+ module TestCentricity
2
+ module AppElements
3
+ class AppAlert < AppUIElement
4
+ def initialize(name, parent, locator, context)
5
+ super
6
+ @type = :alert
7
+ end
8
+
9
+ # Wait until the alert modal is visible, or until the specified wait time has expired. If the wait time is nil, then the
10
+ # wait time will be Environ.default_max_wait_time. Unlike wait_until_visible or wait_until_exists, this method does not
11
+ # raise an exception if the alert modal does not appear within the specified wait time.
12
+ # Returns true if the alert modal is visible.
13
+ #
14
+ # @return [Integer]
15
+ # @param seconds [Integer or Float] wait time in seconds
16
+ # @example
17
+ # permissions_modal.await(2)
18
+ #
19
+ def await(seconds)
20
+ timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
21
+ wait = Selenium::WebDriver::Wait.new(timeout: timeout)
22
+ wait.until do
23
+ reset_mru_cache
24
+ visible?
25
+ end
26
+ true
27
+ rescue
28
+ false
29
+ end
30
+
31
+ # Performs the action to required accept the currently visible alert modal. If the alert modal is still visible after
32
+ # 5 seconds, an exception is raised.
33
+ #
34
+ # @example
35
+ # alert_modal.accept
36
+ #
37
+ def accept
38
+ reset_mru_cache
39
+ alert_accept
40
+ wait_until_gone(seconds = 5, post_exception = false)
41
+ if visible?
42
+ alert_accept
43
+ wait_until_gone(5)
44
+ end
45
+ end
46
+
47
+ # Performs the action required dismiss the currently visible alert modal. If the alert modal is still visible after
48
+ # 5 seconds, an exception is raised.
49
+ #
50
+ # @example
51
+ # alert_modal.dismiss
52
+ #
53
+ def dismiss
54
+ reset_mru_cache
55
+ alert_dismiss
56
+ wait_until_gone(seconds = 5, post_exception = false)
57
+ if visible?
58
+ alert_dismiss
59
+ wait_until_gone(5)
60
+ end
61
+ end
62
+
63
+ #
64
+ # @return [Integer]
65
+ # @param action [Symbol or String] action to perform if alert modal is visible. Acceptable values are :allow, :accept, :dont_allow, or :dismiss
66
+ # @param timeout [Integer or Float] wait time in seconds. Defaults to 2 if not specified
67
+ # @param button_name [String] optional caption of button to tap associated with the specified action
68
+ # @example
69
+ # alert_modal.await_and_respond(:dismiss)
70
+ # OR
71
+ # permissions_modal.await_and_respond(:accept, timeout = 1, button_name = 'Allow Once')
72
+ #
73
+ def await_and_respond(action, timeout = 2, button_name = nil)
74
+ reset_mru_cache
75
+ action = action.gsub(/\s+/, '_').downcase.to_sym if action.is_a?(String)
76
+ if await(timeout)
77
+ case action
78
+ when :allow, :accept
79
+ if button_name.nil?
80
+ accept
81
+ elsif Environ.is_ios?
82
+ Environ.appium_driver.execute_script('mobile: alert', { action: 'accept', buttonLabel: button_name })
83
+ else
84
+ Environ.appium_driver.execute_script('mobile: acceptAlert', { buttonLabel: button_name })
85
+ end
86
+ when :dont_allow, :dismiss
87
+ if button_name.nil?
88
+ dismiss
89
+ elsif Environ.is_ios?
90
+ Environ.appium_driver.execute_script('mobile: alert', { action: 'dismiss', buttonLabel: button_name })
91
+ else
92
+ Environ.appium_driver.execute_script('mobile: dismissAlert', { buttonLabel: button_name })
93
+ end
94
+ else
95
+ raise "#{action} is not a valid selector"
96
+ end
97
+ if Environ.is_ios? && await(1)
98
+ case action
99
+ when :allow, :accept
100
+ accept
101
+ when :dont_allow, :dismiss
102
+ dismiss
103
+ end
104
+ if await(1)
105
+ buttons = Environ.appium_driver.execute_script('mobile: alert', { action: 'getButtons' })
106
+ raise "Could not perform #{action} action on active modal. Available modal buttons are #{buttons}"
107
+ end
108
+ end
109
+ true
110
+ else
111
+ false
112
+ end
113
+ end
114
+
115
+ def buttons
116
+ if Environ.is_ios?
117
+ Environ.appium_driver.execute_script('mobile: alert', { action: 'getButtons' })
118
+ else
119
+ obj = element
120
+ object_not_found_exception(obj)
121
+ captions = []
122
+ labels = obj.find_elements(:class, 'android.widget.Button')
123
+ labels.each do |label|
124
+ captions.push(label.text)
125
+ end
126
+ captions
127
+ end
128
+ end
129
+
130
+ def get_caption
131
+ obj = element
132
+ object_not_found_exception(obj)
133
+ if Environ.is_ios?
134
+ captions = []
135
+ labels = obj.find_elements(:class, 'XCUIElementTypeStaticText')
136
+ labels.each do |label|
137
+ captions.push(label.text)
138
+ end
139
+ captions
140
+ else
141
+ title_obj = obj.find_element(:id, 'android:id/alertTitle')
142
+ msg_obj = obj.find_element(:id, 'android:id/message')
143
+ if msg_obj.nil?
144
+ [title_obj.text]
145
+ else
146
+ [title_obj.text, msg_obj.text]
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end