@@ -26,6 +26,12 @@ Alert::Alert(BrowserHandle browser, HWND handle) {
26
26
27
27
HWND direct_ui_child = this ->GetDirectUIChild ();
28
28
this ->is_standard_alert_ = direct_ui_child == NULL ;
29
+
30
+ std::vector<HWND> text_boxes;
31
+ ::EnumChildWindows (this ->alert_handle_,
32
+ &Alert::FindTextBoxes,
33
+ reinterpret_cast <LPARAM>(&text_boxes));
34
+ this ->is_security_alert_ = text_boxes.size () > 1 ;
29
35
}
30
36
31
37
@@ -68,28 +74,53 @@ int Alert::Dismiss() {
68
74
return WD_SUCCESS;
69
75
}
70
76
71
- int Alert::SendKeys (std::string keys) {
77
+ int Alert::SendKeys (const std::string& keys) {
72
78
LOG (TRACE) << " Entering Alert::SendKeys" ;
73
- HWND text_box_handle = NULL ;
74
- // Alert present, find the OK button.
79
+ return this ->SendKeysInternal (keys, 0 );
80
+ }
81
+
82
+ int Alert::SetUserName (const std::string& username) {
83
+ LOG (TRACE) << " Entering Alert::SetUserName" ;
84
+ // If this isn't a security alert, return an error.
85
+ if (!this ->is_security_alert_ ) {
86
+ return EUNEXPECTEDALERTOPEN;
87
+ }
88
+ return this ->SendKeysInternal (username, 0 );
89
+ }
90
+
91
+ int Alert::SetPassword (const std::string& password) {
92
+ LOG (TRACE) << " Entering Alert::SetPassword" ;
93
+ // If this isn't a security alert, return an error.
94
+ if (!this ->is_security_alert_ ) {
95
+ return EUNEXPECTEDALERTOPEN;
96
+ }
97
+ return this ->SendKeysInternal (password, ES_PASSWORD);
98
+ }
99
+
100
+ int Alert::SendKeysInternal (const std::string& keys, const long text_box_style) {
101
+ LOG (TRACE) << " Entering Alert::SendKeysInternal" ;
102
+ TextBoxFindInfo text_box_find_info;
103
+ text_box_find_info.textbox_handle = NULL ;
104
+ text_box_find_info.style_match = text_box_style;
105
+ // Alert present, find the text box.
75
106
// Retry up to 10 times to find the dialog.
76
107
int max_wait = 10 ;
77
- while ((text_box_handle == NULL ) && --max_wait) {
108
+ while ((text_box_find_info. textbox_handle == NULL ) && --max_wait) {
78
109
::EnumChildWindows (this ->alert_handle_,
79
110
&Alert::FindTextBox,
80
- reinterpret_cast <LPARAM>(&text_box_handle ));
81
- if (text_box_handle == NULL ) {
111
+ reinterpret_cast <LPARAM>(&text_box_find_info ));
112
+ if (text_box_find_info. textbox_handle == NULL ) {
82
113
::Sleep (50 );
83
114
}
84
115
}
85
116
86
- if (text_box_handle == NULL ) {
117
+ if (text_box_find_info. textbox_handle == NULL ) {
87
118
LOG (WARN) << " Text box not found on alert" ;
88
119
return EELEMENTNOTDISPLAYED;
89
120
} else {
90
121
LOG (DEBUG) << " Sending keystrokes to alert using SendMessage" ;
91
122
std::wstring text = StringUtilities::ToWString (keys);
92
- ::SendMessage (text_box_handle ,
123
+ ::SendMessage (text_box_find_info.textbox_handle ,
93
124
WM_SETTEXT,
94
125
NULL ,
95
126
reinterpret_cast <LPARAM>(text.c_str()));
@@ -104,9 +135,11 @@ std::string Alert::GetText() {
104
135
alert_text_value = this ->GetStandardDialogText ();
105
136
} else {
106
137
std::string alert_text = this ->GetDirectUIDialogText ();
107
- size_t first_crlf = alert_text.find (" \r\n\r\n " );
108
- if (first_crlf != std::string::npos && first_crlf + 4 < alert_text.size ()) {
109
- alert_text_value = alert_text.substr (first_crlf + 4 );
138
+ if (!this ->is_security_alert_ ) {
139
+ size_t first_crlf = alert_text.find (" \r\n\r\n " );
140
+ if (first_crlf != std::string::npos && first_crlf + 4 < alert_text.size ()) {
141
+ alert_text_value = alert_text.substr (first_crlf + 4 );
142
+ }
110
143
}
111
144
}
112
145
return alert_text_value;
@@ -133,11 +166,13 @@ std::string Alert::GetStandardDialogText() {
133
166
134
167
// BIG ASSUMPTION HERE! If we found the text label, assume that
135
168
// all other controls on the alert are fully drawn too.
136
- HWND text_box_handle = NULL ;
169
+ TextBoxFindInfo textbox_find_info;
170
+ textbox_find_info.textbox_handle = NULL ;
171
+ textbox_find_info.style_match = 0 ;
137
172
::EnumChildWindows (this ->alert_handle_,
138
173
&Alert::FindTextBox,
139
- reinterpret_cast <LPARAM>(&text_box_handle ));
140
- if (text_box_handle ) {
174
+ reinterpret_cast <LPARAM>(&textbox_find_info ));
175
+ if (textbox_find_info. textbox_handle ) {
141
176
// There's a text box on the alert. That means the first
142
177
// label found is the system-provided label. Ignore that
143
178
// one and return the next one.
@@ -367,7 +402,8 @@ bool Alert::IsOKButton(HWND button_handle) {
367
402
::GetClassName (button_handle, &button_window_class[0 ], static_cast <int >(button_window_class.size()));
368
403
if (wcscmp (&button_window_class[0 ], L" Button" ) == 0 ) {
369
404
long window_long = ::GetWindowLong (button_handle, GWL_STYLE);
370
- return (window_long & BS_DEFCOMMANDLINK) == BS_DEFCOMMANDLINK;
405
+ long button_style = window_long & BS_TYPEMASK;
406
+ return button_style == BS_DEFCOMMANDLINK || button_style == BS_DEFPUSHBUTTON;
371
407
}
372
408
return false ;
373
409
}
@@ -381,9 +417,10 @@ bool Alert::IsCancelButton(HWND button_handle) {
381
417
::GetClassName (button_handle, &button_window_class[0 ], static_cast <int >(button_window_class.size()));
382
418
if (wcscmp (&button_window_class[0 ], L" Button" ) == 0 ) {
383
419
long window_long = ::GetWindowLong (button_handle, GWL_STYLE);
420
+ long button_style = window_long & BS_TYPEMASK;
384
421
// The BS_DEFCOMMANDLINK mask includes BS_COMMANDLINK, but we
385
422
// want only to match those without the default bits set.
386
- return (window_long & BS_DEFCOMMANDLINK) == BS_COMMANDLINK ;
423
+ return button_style == BS_COMMANDLINK || button_style == BS_PUSHBUTTON ;
387
424
}
388
425
return false ;
389
426
}
@@ -400,13 +437,22 @@ BOOL CALLBACK Alert::FindDialogButton(HWND hwnd, LPARAM arg) {
400
437
}
401
438
402
439
BOOL CALLBACK Alert::FindTextBox (HWND hwnd, LPARAM arg) {
403
- HWND *dialog_handle = reinterpret_cast <HWND *>(arg);
440
+ TextBoxFindInfo* find_info = reinterpret_cast <TextBoxFindInfo *>(arg);
404
441
std::vector<wchar_t > child_window_class (100 );
405
442
::GetClassName (hwnd, &child_window_class[0 ], 100 );
406
443
407
444
if (wcscmp (&child_window_class[0 ], L" Edit" ) == 0 ) {
408
- *dialog_handle = hwnd;
409
- return FALSE ;
445
+ if (find_info->style_match == 0 ) {
446
+ find_info->textbox_handle = hwnd;;;
447
+ return FALSE ;
448
+ } else {
449
+ long window_long = ::GetWindowLong (hwnd, GWL_STYLE);
450
+ long edit_style = window_long & find_info->style_match ;
451
+ if (edit_style == find_info->style_match ) {
452
+ find_info->textbox_handle = hwnd;
453
+ return FALSE ;
454
+ }
455
+ }
410
456
}
411
457
return TRUE ;
412
458
}
@@ -445,4 +491,15 @@ BOOL CALLBACK Alert::FindDirectUIChild(HWND hwnd, LPARAM arg){
445
491
return FALSE ;
446
492
}
447
493
494
+ BOOL CALLBACK Alert::FindTextBoxes (HWND hwnd, LPARAM arg) {
495
+ std::vector<HWND>* dialog_handles = reinterpret_cast <std::vector<HWND>*>(arg);
496
+ std::vector<wchar_t > child_window_class (100 );
497
+ ::GetClassName (hwnd, &child_window_class[0 ], 100 );
498
+
499
+ if (wcscmp (&child_window_class[0 ], L" Edit" ) == 0 ) {
500
+ dialog_handles->push_back (hwnd);
501
+ }
502
+ return TRUE ;
503
+ }
504
+
448
505
} // namespace webdriver
0 commit comments