@@ -21,10 +21,7 @@ Alert::Alert(BrowserHandle browser, HWND handle) {
21
21
this ->browser_ = browser;
22
22
this ->alert_handle_ = handle;
23
23
24
- HWND direct_ui_child = NULL ;
25
- ::EnumChildWindows (this ->alert_handle_,
26
- &Alert::FindDirectUIChild,
27
- reinterpret_cast <LPARAM>(&direct_ui_child));
24
+ HWND direct_ui_child = this ->GetDirectUIChild ();
28
25
this ->is_standard_alert_ = direct_ui_child == NULL ;
29
26
}
30
27
@@ -99,6 +96,21 @@ int Alert::SendKeys(std::string keys) {
99
96
100
97
std::string Alert::GetText () {
101
98
LOG (TRACE) << " Entering Alert::GetText" ;
99
+ std::string alert_text_value = " " ;
100
+ if (this ->is_standard_alert_ ) {
101
+ alert_text_value = this ->GetStandardDialogText ();
102
+ } else {
103
+ std::string alert_text = this ->GetDirectUIDialogText ();
104
+ size_t first_crlf = alert_text.find (" \r\n\r\n " );
105
+ if (first_crlf != std::string::npos && first_crlf + 4 < alert_text.size ()) {
106
+ alert_text_value = alert_text.substr (first_crlf + 4 );
107
+ }
108
+ }
109
+ return alert_text_value;
110
+ }
111
+
112
+ std::string Alert::GetStandardDialogText () {
113
+ LOG (TRACE) << " Entering Alert::GetStandardDialogText" ;
102
114
TextLabelFindInfo info;
103
115
info.label_handle = NULL ;
104
116
info.control_id_found = 0 ;
@@ -147,6 +159,114 @@ std::string Alert::GetText() {
147
159
return alert_text_value;
148
160
}
149
161
162
+ std::string Alert::GetDirectUIDialogText () {
163
+ LOG (TRACE) << " Entering Alert::GetDirectUIDialogText" ;
164
+ std::string alert_text_value = " " ;
165
+ HWND direct_ui_child_handle = this ->GetDirectUIChild ();
166
+
167
+ CComPtr<IAccessible> window_object;
168
+ HRESULT hr = ::AccessibleObjectFromWindow (
169
+ direct_ui_child_handle,
170
+ OBJID_WINDOW,
171
+ IID_IAccessible,
172
+ reinterpret_cast <void **>(&window_object));
173
+ if (FAILED (hr)) {
174
+ LOGHR (WARN, hr) << " Failed to get Active Accessibility window object from dialog" ;
175
+ return alert_text_value;
176
+ }
177
+
178
+ // ASSUMPTION: There is an object with the role of "pane" as a child of
179
+ // the window object.
180
+ CComPtr<IAccessible> pane_object = this ->GetChildWithRole (window_object,
181
+ ROLE_SYSTEM_PANE,
182
+ 0 );
183
+ if (!pane_object) {
184
+ LOG (WARN) << " Failed to get Active Accessibility pane child object from window" ;
185
+ return alert_text_value;
186
+ }
187
+
188
+ // ASSUMPTION: The second "static text" accessibility object is the one
189
+ // that contains the message.
190
+ CComPtr<IAccessible> message_text_object = this ->GetChildWithRole (
191
+ pane_object,
192
+ ROLE_SYSTEM_STATICTEXT,
193
+ 1 );
194
+ if (!message_text_object) {
195
+ LOG (WARN) << " Failed to get Active Accessibility text child object from pane" ;
196
+ return alert_text_value;
197
+ }
198
+
199
+ CComVariant child_id;
200
+ child_id.vt = VT_I4;
201
+ child_id.lVal = CHILDID_SELF;
202
+
203
+ CComBSTR text_bstr;
204
+ hr = message_text_object->get_accName (child_id, &text_bstr);
205
+ if (FAILED (hr)) {
206
+ LOGHR (WARN, hr) << " Failed to get accName property from text object" ;
207
+ return alert_text_value;
208
+ }
209
+
210
+ std::wstring text = text_bstr;
211
+ alert_text_value = StringUtilities::ToString (text);
212
+ return alert_text_value;
213
+ }
214
+
215
+ IAccessible* Alert::GetChildWithRole (IAccessible* parent, long expected_role, int index) {
216
+ LOG (TRACE) << " Entering Alert::GetChildWithRole" ;
217
+ IAccessible* child = NULL ;
218
+ long child_count;
219
+ HRESULT hr = parent->get_accChildCount (&child_count);
220
+ if (FAILED (hr)) {
221
+ LOGHR (WARN, hr) << " Failed to get accChildCount property from Active Accessibility object" ;
222
+ return child;
223
+ }
224
+
225
+ long returned_children = 0 ;
226
+ std::vector<CComVariant> child_array (child_count);
227
+ hr = ::AccessibleChildren (parent, 0 , child_count, &child_array[0 ], &returned_children);
228
+
229
+ int found_index = 0 ;
230
+ for (long i = 0 ; i < child_count; ++i) {
231
+ if (child_array[i].vt == VT_DISPATCH) {
232
+ CComPtr<IAccessible> child_object;
233
+ hr = child_array[i].pdispVal ->QueryInterface <IAccessible>(&child_object);
234
+ if (FAILED (hr)) {
235
+ LOGHR (WARN, hr) << " QueryInterface for IAccessible failed for child object with index " << i;
236
+ }
237
+
238
+ CComVariant child_id;
239
+ child_id.vt = VT_I4;
240
+ child_id.lVal = CHILDID_SELF;
241
+
242
+ CComVariant actual_role;
243
+ hr = child_object->get_accRole (child_id, &actual_role);
244
+ if (FAILED (hr)) {
245
+ LOGHR (WARN, hr) << " Failed to get accRole property from Active Accessibility object" ;
246
+ }
247
+
248
+ if (expected_role == actual_role.lVal ) {
249
+ if (found_index == index ) {
250
+ child = child_object.Detach ();
251
+ } else {
252
+ ++found_index;
253
+ }
254
+ }
255
+ LOG (DEBUG) << " accRole for child with index " << i << " : " << actual_role.lVal ;
256
+ }
257
+ }
258
+ return child;
259
+ }
260
+
261
+ HWND Alert::GetDirectUIChild () {
262
+ LOG (TRACE) << " Entering Alert::GetDirectUIChild" ;
263
+ HWND direct_ui_child = NULL ;
264
+ ::EnumChildWindows (this ->alert_handle_,
265
+ &Alert::FindDirectUIChild,
266
+ reinterpret_cast <LPARAM>(&direct_ui_child));
267
+ return direct_ui_child;
268
+ }
269
+
150
270
int Alert::ClickAlertButton (DialogButtonInfo button_info) {
151
271
LOG (TRACE) << " Entering Alert::ClickAlertButton" ;
152
272
// Click on the appropriate button of the Alert
0 commit comments