Skip to content

Commit afea9f5

Browse files
committed
Changes to simplify the execution of finding elements.
The driver now delegates more of the error handling of finding elements to the JavaScript automation atoms. This has the effect of simplifying the code within the driver, and synchronizes the error-case behavior with other implementations.
1 parent b21a4f5 commit afea9f5

File tree

11 files changed

+1855
-1864
lines changed

11 files changed

+1855
-1864
lines changed

cpp/iedriver/CommandHandlers/FindChildElementCommandHandler.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,15 @@ class FindChildElementCommandHandler : public IECommandHandler {
7676
response->SetSuccessResponse(found_element);
7777
return;
7878
}
79-
if(status_code == EINVALIDSELECTOR){
80-
response->SetErrorResponse(status_code,
81-
"The xpath expression '" + value + "' cannot be evaluated or does not" +
82-
"result in a WebElement");
83-
return;
84-
}
85-
if (status_code == EUNHANDLEDERROR) {
86-
response->SetErrorResponse(status_code,
87-
"Unknown finder mechanism: " + mechanism);
88-
return;
89-
}
9079
if (status_code == ENOSUCHWINDOW) {
9180
response->SetErrorResponse(status_code, "Unable to find element on closed window");
9281
return;
9382
}
83+
if (status_code != ENOSUCHELEMENT) {
84+
response->SetErrorResponse(status_code, found_element.asString());
85+
return;
86+
}
87+
9488
// Release the thread so that the browser doesn't starve.
9589
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
9690
} while (clock() < end);

cpp/iedriver/CommandHandlers/FindChildElementsCommandHandler.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,19 @@ class FindChildElementsCommandHandler : public IECommandHandler {
7272
mechanism,
7373
value,
7474
&found_elements);
75-
if (status_code == WD_SUCCESS && found_elements.size() > 0) {
76-
response->SetSuccessResponse(found_elements);
77-
return;
78-
}
79-
if (status_code == EINVALIDSELECTOR) {
80-
response->SetErrorResponse(status_code,
81-
"The xpath expression '" + value + "' cannot be evaluated or does not" +
82-
"result in a WebElement");
83-
return;
84-
}
85-
if (status_code == EUNHANDLEDERROR) {
86-
response->SetErrorResponse(status_code,
87-
"Unknown finder mechanism: " + mechanism);
88-
return;
89-
}
90-
if (status_code == ENOSUCHWINDOW) {
75+
if (status_code == WD_SUCCESS) {
76+
if (found_elements.isArray() && found_elements.size() > 0) {
77+
response->SetSuccessResponse(found_elements);
78+
return;
79+
}
80+
} else if (status_code == ENOSUCHWINDOW) {
9181
response->SetErrorResponse(status_code, "Unable to find elements on closed window");
9282
return;
83+
} else {
84+
response->SetErrorResponse(status_code, found_elements.asString());
85+
return;
9386
}
87+
9488
// Release the thread so that the browser doesn't starve.
9589
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
9690
} while (clock() < end);

cpp/iedriver/CommandHandlers/FindElementCommandHandler.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,15 @@ class FindElementCommandHandler : public IECommandHandler {
6565
response->SetSuccessResponse(found_element);
6666
return;
6767
}
68-
if(status_code == EINVALIDSELECTOR) {
69-
response->SetErrorResponse(status_code,
70-
"The xpath expression '" + value + "' cannot be evaluated or does not" +
71-
"result in a WebElement");
72-
return;
73-
}
74-
if (status_code == EUNHANDLEDERROR) {
75-
response->SetErrorResponse(status_code,
76-
"Unknown finder mechanism: " + mechanism);
77-
return;
78-
}
7968
if (status_code == ENOSUCHWINDOW) {
8069
response->SetErrorResponse(status_code, "Unable to find element on closed window");
8170
return;
8271
}
72+
if (status_code != ENOSUCHELEMENT) {
73+
response->SetErrorResponse(status_code, found_element.asString());
74+
return;
75+
}
76+
8377
// Release the thread so that the browser doesn't starve.
8478
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
8579
} while (clock() < end);

cpp/iedriver/CommandHandlers/FindElementsCommandHandler.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,25 @@ class FindElementsCommandHandler : public IECommandHandler {
5555
}
5656

5757
int status_code = WD_SUCCESS;
58-
Json::Value found_elements(Json::arrayValue);
58+
Json::Value found_elements;
5959
do {
6060
status_code = executor.LocateElements(ElementHandle(),
6161
mechanism,
6262
value,
6363
&found_elements);
64-
if (status_code == WD_SUCCESS && found_elements.size() > 0) {
65-
response->SetSuccessResponse(found_elements);
66-
return;
67-
}
68-
if(status_code == EINVALIDSELECTOR) {
69-
response->SetErrorResponse(status_code,
70-
"The xpath expression '" + value + "' cannot be evaluated or does not" +
71-
"result in a WebElement");
72-
return;
73-
}
74-
if (status_code == EUNHANDLEDERROR) {
75-
response->SetErrorResponse(status_code,
76-
"Unknown finder mechanism: " + mechanism);
77-
return;
78-
}
79-
if (status_code == ENOSUCHWINDOW) {
64+
if (status_code == WD_SUCCESS) {
65+
if (found_elements.isArray() && found_elements.size() > 0) {
66+
response->SetSuccessResponse(found_elements);
67+
return;
68+
}
69+
} else if (status_code == ENOSUCHWINDOW) {
8070
response->SetErrorResponse(status_code, "Unable to find elements on closed window");
8171
return;
72+
} else {
73+
response->SetErrorResponse(status_code, found_elements.asString());
74+
return;
8275
}
76+
8377
// Release the thread so that the browser doesn't starve.
8478
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
8579
} while (clock() < end);

cpp/iedriver/ElementFinder.cpp

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,14 @@ int ElementFinder::FindElement(const IECommandExecutor& executor,
6969

7070
status_code = script_wrapper.Execute();
7171
if (status_code == WD_SUCCESS) {
72-
if (script_wrapper.ResultIsElement()) {
73-
script_wrapper.ConvertResultToJsonValue(executor, found_element);
74-
} else {
75-
LOG(WARN) << "Unable to find element by mechanism "
76-
<< LOGWSTRING(mechanism) << " and criteria "
77-
<< LOGWSTRING(criteria);
78-
status_code = ENOSUCHELEMENT;
79-
}
72+
Json::Value atom_result;
73+
script_wrapper.ConvertResultToJsonValue(executor, &atom_result);
74+
int atom_status_code = atom_result["status"].asInt();
75+
Json::Value atom_value = atom_result["value"];
76+
status_code = atom_status_code;
77+
*found_element = atom_result["value"];
8078
} else {
81-
// An error in the execution of the FindElement atom for XPath is assumed
82-
// to be a syntactically invalid XPath.
83-
if (mechanism == L"xpath") {
84-
LOG(WARN) << "Attempted to find element using invalid xpath: "
85-
<< LOGWSTRING(criteria);
86-
status_code = EINVALIDSELECTOR;
87-
} else {
88-
LOG(WARN) << "Unexpected error attempting to find element by mechanism "
89-
<< LOGWSTRING(mechanism) << " with criteria "
90-
<< LOGWSTRING(
91-
criteria);
92-
status_code = ENOSUCHELEMENT;
93-
}
79+
*found_element = "A JavaScript error was encountered executing the findElement atom.";
9480
}
9581
} else {
9682
LOG(WARN) << "Unable to get browser";
@@ -139,26 +125,14 @@ int ElementFinder::FindElements(const IECommandExecutor& executor,
139125

140126
status_code = script_wrapper.Execute();
141127
if (status_code == WD_SUCCESS) {
142-
if (script_wrapper.ResultIsArray() ||
143-
script_wrapper.ResultIsElementCollection()) {
144-
script_wrapper.ConvertResultToJsonValue(executor, found_elements);
145-
} else {
146-
LOG(WARN) << "Returned value is not an array or element collection";
147-
status_code = ENOSUCHELEMENT;
148-
}
128+
Json::Value atom_result;
129+
script_wrapper.ConvertResultToJsonValue(executor, &atom_result);
130+
int atom_status_code = atom_result["status"].asInt();
131+
Json::Value atom_value = atom_result["value"];
132+
status_code = atom_status_code;
133+
*found_elements = atom_result["value"];
149134
} else {
150-
// An error in the execution of the FindElement atom for XPath is assumed
151-
// to be a syntactically invalid XPath.
152-
if (mechanism == L"xpath") {
153-
LOG(WARN) << "Attempted to find elements using invalid xpath: "
154-
<< LOGWSTRING(criteria);
155-
status_code = EINVALIDSELECTOR;
156-
} else {
157-
LOG(WARN) << "Unexpected error attempting to find element by mechanism "
158-
<< LOGWSTRING(mechanism) << " and criteria "
159-
<< LOGWSTRING(criteria);
160-
status_code = ENOSUCHELEMENT;
161-
}
135+
*found_elements = "A JavaScript error was encountered executing the findElements atom.";
162136
}
163137
} else {
164138
LOG(WARN) << "Unable to get browser";

0 commit comments

Comments
 (0)