@@ -38,64 +38,90 @@ void FindChildElementsCommandHandler::ExecuteInternal(
38
38
if (id_parameter_iterator == command_parameters.end ()) {
39
39
response->SetErrorResponse (ERROR_INVALID_ARGUMENT, " Missing parameter in URL: id" );
40
40
return ;
41
- } else if (using_parameter_iterator == command_parameters.end ()) {
41
+ }
42
+ if (using_parameter_iterator == command_parameters.end ()) {
42
43
response->SetErrorResponse (ERROR_INVALID_ARGUMENT, " Missing parameter: using" );
43
44
return ;
44
- } else if (value_parameter_iterator == command_parameters.end ()) {
45
+ }
46
+ if (!using_parameter_iterator->second .isString ()) {
47
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT, " using parameter must be a string" );
48
+ return ;
49
+ }
50
+ if (value_parameter_iterator == command_parameters.end ()) {
45
51
response->SetErrorResponse (ERROR_INVALID_ARGUMENT, " Missing parameter: value" );
46
52
return ;
47
- } else {
48
- std::string mechanism = using_parameter_iterator->second .asString ();
49
- std::string value = value_parameter_iterator->second .asString ();
50
- std::string element_id = id_parameter_iterator->second .asString ();
53
+ }
54
+ if (!value_parameter_iterator->second .isString ()) {
55
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT, " value parameter must be a string" );
56
+ return ;
57
+ }
51
58
52
- ElementHandle parent_element_wrapper;
53
- int status_code = this ->GetElement (executor,
54
- element_id,
55
- &parent_element_wrapper);
59
+ std::string mechanism = using_parameter_iterator->second .asString ();
60
+ std::string value = value_parameter_iterator->second .asString ();
61
+ std::string element_id = id_parameter_iterator->second .asString ();
56
62
57
- if (status_code == WD_SUCCESS) {
58
- Json::Value found_elements (Json::arrayValue);
63
+ if (mechanism != " css selector" &&
64
+ mechanism != " tag name" &&
65
+ mechanism != " link text" &&
66
+ mechanism != " partial link text" &&
67
+ mechanism != " xpath" ) {
68
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT, " using parameter value '" + mechanism + " ' is not a valid value" );
69
+ return ;
70
+ }
59
71
60
- int timeout = executor.implicit_wait_timeout ();
61
- clock_t end = clock () + (timeout / 1000 * CLOCKS_PER_SEC);
62
- if (timeout > 0 && timeout < 1000 ) {
63
- end += 1 * CLOCKS_PER_SEC;
64
- }
72
+ BrowserHandle browser_wrapper;
73
+ int status_code = executor.GetCurrentBrowser (&browser_wrapper);
74
+ if (status_code != WD_SUCCESS) {
75
+ response->SetErrorResponse (status_code, " Currently focused window has been closed." );
76
+ return ;
77
+ }
65
78
66
- do {
67
- status_code = executor.LocateElements (parent_element_wrapper,
68
- mechanism,
69
- value,
70
- &found_elements);
71
- if (status_code == WD_SUCCESS) {
72
- if (found_elements.isArray () && found_elements.size () > 0 ) {
73
- response->SetSuccessResponse (found_elements);
74
- return ;
75
- }
76
- } else if (status_code == ENOSUCHWINDOW) {
77
- response->SetErrorResponse (ERROR_NO_SUCH_WINDOW, " Unable to find elements on closed window" );
78
- return ;
79
- } else {
80
- response->SetErrorResponse (status_code, found_elements.asString ());
81
- return ;
82
- }
79
+ ElementHandle parent_element_wrapper;
80
+ status_code = this ->GetElement (executor,
81
+ element_id,
82
+ &parent_element_wrapper);
83
+
84
+ if (status_code == WD_SUCCESS) {
85
+ Json::Value found_elements (Json::arrayValue);
83
86
84
- // Release the thread so that the browser doesn't starve.
85
- ::Sleep (FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
86
- } while (clock () < end);
87
+ int timeout = executor.implicit_wait_timeout ();
88
+ clock_t end = clock () + (timeout / 1000 * CLOCKS_PER_SEC);
89
+ if (timeout > 0 && timeout < 1000 ) {
90
+ end += 1 * CLOCKS_PER_SEC;
91
+ }
87
92
88
- // This code is executed when no elements where found and no errors occurred.
93
+ do {
94
+ status_code = executor.LocateElements (parent_element_wrapper,
95
+ mechanism,
96
+ value,
97
+ &found_elements);
89
98
if (status_code == WD_SUCCESS) {
90
- response->SetSuccessResponse (found_elements);
99
+ if (found_elements.isArray () && found_elements.size () > 0 ) {
100
+ response->SetSuccessResponse (found_elements);
101
+ return ;
102
+ }
103
+ } else if (status_code == ENOSUCHWINDOW) {
104
+ response->SetErrorResponse (ERROR_NO_SUCH_WINDOW, " Unable to find elements on closed window" );
105
+ return ;
91
106
} else {
92
- response->SetErrorResponse (status_code,
93
- " Finding elements with " + mechanism + " == " + value +
94
- " returned an unexpected error" );
107
+ response->SetErrorResponse (status_code, found_elements.asString ());
108
+ return ;
95
109
}
110
+
111
+ // Release the thread so that the browser doesn't starve.
112
+ ::Sleep (FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
113
+ } while (clock () < end);
114
+
115
+ // This code is executed when no elements where found and no errors occurred.
116
+ if (status_code == WD_SUCCESS) {
117
+ response->SetSuccessResponse (found_elements);
96
118
} else {
97
- response->SetErrorResponse (status_code, " Element is no longer valid" );
119
+ response->SetErrorResponse (status_code,
120
+ " Finding elements with " + mechanism + " == " + value +
121
+ " returned an unexpected error" );
98
122
}
123
+ } else {
124
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT, " Element is no longer valid" );
99
125
}
100
126
}
101
127
0 commit comments