Skip to content

Commit

Permalink
Changes to simplify the execution of finding elements.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimevans committed Jan 22, 2016
1 parent b21a4f5 commit afea9f5
Show file tree
Hide file tree
Showing 11 changed files with 1,855 additions and 1,864 deletions.
16 changes: 5 additions & 11 deletions cpp/iedriver/CommandHandlers/FindChildElementCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,15 @@ class FindChildElementCommandHandler : public IECommandHandler {
response->SetSuccessResponse(found_element);
return;
}
if(status_code == EINVALIDSELECTOR){
response->SetErrorResponse(status_code,
"The xpath expression '" + value + "' cannot be evaluated or does not" +
"result in a WebElement");
return;
}
if (status_code == EUNHANDLEDERROR) {
response->SetErrorResponse(status_code,
"Unknown finder mechanism: " + mechanism);
return;
}
if (status_code == ENOSUCHWINDOW) {
response->SetErrorResponse(status_code, "Unable to find element on closed window");
return;
}
if (status_code != ENOSUCHELEMENT) {
response->SetErrorResponse(status_code, found_element.asString());
return;
}

// Release the thread so that the browser doesn't starve.
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
} while (clock() < end);
Expand Down
26 changes: 10 additions & 16 deletions cpp/iedriver/CommandHandlers/FindChildElementsCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,19 @@ class FindChildElementsCommandHandler : public IECommandHandler {
mechanism,
value,
&found_elements);
if (status_code == WD_SUCCESS && found_elements.size() > 0) {
response->SetSuccessResponse(found_elements);
return;
}
if (status_code == EINVALIDSELECTOR) {
response->SetErrorResponse(status_code,
"The xpath expression '" + value + "' cannot be evaluated or does not" +
"result in a WebElement");
return;
}
if (status_code == EUNHANDLEDERROR) {
response->SetErrorResponse(status_code,
"Unknown finder mechanism: " + mechanism);
return;
}
if (status_code == ENOSUCHWINDOW) {
if (status_code == WD_SUCCESS) {
if (found_elements.isArray() && found_elements.size() > 0) {
response->SetSuccessResponse(found_elements);
return;
}
} else if (status_code == ENOSUCHWINDOW) {
response->SetErrorResponse(status_code, "Unable to find elements on closed window");
return;
} else {
response->SetErrorResponse(status_code, found_elements.asString());
return;
}

// Release the thread so that the browser doesn't starve.
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
} while (clock() < end);
Expand Down
16 changes: 5 additions & 11 deletions cpp/iedriver/CommandHandlers/FindElementCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,15 @@ class FindElementCommandHandler : public IECommandHandler {
response->SetSuccessResponse(found_element);
return;
}
if(status_code == EINVALIDSELECTOR) {
response->SetErrorResponse(status_code,
"The xpath expression '" + value + "' cannot be evaluated or does not" +
"result in a WebElement");
return;
}
if (status_code == EUNHANDLEDERROR) {
response->SetErrorResponse(status_code,
"Unknown finder mechanism: " + mechanism);
return;
}
if (status_code == ENOSUCHWINDOW) {
response->SetErrorResponse(status_code, "Unable to find element on closed window");
return;
}
if (status_code != ENOSUCHELEMENT) {
response->SetErrorResponse(status_code, found_element.asString());
return;
}

// Release the thread so that the browser doesn't starve.
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
} while (clock() < end);
Expand Down
28 changes: 11 additions & 17 deletions cpp/iedriver/CommandHandlers/FindElementsCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,25 @@ class FindElementsCommandHandler : public IECommandHandler {
}

int status_code = WD_SUCCESS;
Json::Value found_elements(Json::arrayValue);
Json::Value found_elements;
do {
status_code = executor.LocateElements(ElementHandle(),
mechanism,
value,
&found_elements);
if (status_code == WD_SUCCESS && found_elements.size() > 0) {
response->SetSuccessResponse(found_elements);
return;
}
if(status_code == EINVALIDSELECTOR) {
response->SetErrorResponse(status_code,
"The xpath expression '" + value + "' cannot be evaluated or does not" +
"result in a WebElement");
return;
}
if (status_code == EUNHANDLEDERROR) {
response->SetErrorResponse(status_code,
"Unknown finder mechanism: " + mechanism);
return;
}
if (status_code == ENOSUCHWINDOW) {
if (status_code == WD_SUCCESS) {
if (found_elements.isArray() && found_elements.size() > 0) {
response->SetSuccessResponse(found_elements);
return;
}
} else if (status_code == ENOSUCHWINDOW) {
response->SetErrorResponse(status_code, "Unable to find elements on closed window");
return;
} else {
response->SetErrorResponse(status_code, found_elements.asString());
return;
}

// Release the thread so that the browser doesn't starve.
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
} while (clock() < end);
Expand Down
54 changes: 14 additions & 40 deletions cpp/iedriver/ElementFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,14 @@ int ElementFinder::FindElement(const IECommandExecutor& executor,

status_code = script_wrapper.Execute();
if (status_code == WD_SUCCESS) {
if (script_wrapper.ResultIsElement()) {
script_wrapper.ConvertResultToJsonValue(executor, found_element);
} else {
LOG(WARN) << "Unable to find element by mechanism "
<< LOGWSTRING(mechanism) << " and criteria "
<< LOGWSTRING(criteria);
status_code = ENOSUCHELEMENT;
}
Json::Value atom_result;
script_wrapper.ConvertResultToJsonValue(executor, &atom_result);
int atom_status_code = atom_result["status"].asInt();
Json::Value atom_value = atom_result["value"];
status_code = atom_status_code;
*found_element = atom_result["value"];
} else {
// An error in the execution of the FindElement atom for XPath is assumed
// to be a syntactically invalid XPath.
if (mechanism == L"xpath") {
LOG(WARN) << "Attempted to find element using invalid xpath: "
<< LOGWSTRING(criteria);
status_code = EINVALIDSELECTOR;
} else {
LOG(WARN) << "Unexpected error attempting to find element by mechanism "
<< LOGWSTRING(mechanism) << " with criteria "
<< LOGWSTRING(
criteria);
status_code = ENOSUCHELEMENT;
}
*found_element = "A JavaScript error was encountered executing the findElement atom.";
}
} else {
LOG(WARN) << "Unable to get browser";
Expand Down Expand Up @@ -139,26 +125,14 @@ int ElementFinder::FindElements(const IECommandExecutor& executor,

status_code = script_wrapper.Execute();
if (status_code == WD_SUCCESS) {
if (script_wrapper.ResultIsArray() ||
script_wrapper.ResultIsElementCollection()) {
script_wrapper.ConvertResultToJsonValue(executor, found_elements);
} else {
LOG(WARN) << "Returned value is not an array or element collection";
status_code = ENOSUCHELEMENT;
}
Json::Value atom_result;
script_wrapper.ConvertResultToJsonValue(executor, &atom_result);
int atom_status_code = atom_result["status"].asInt();
Json::Value atom_value = atom_result["value"];
status_code = atom_status_code;
*found_elements = atom_result["value"];
} else {
// An error in the execution of the FindElement atom for XPath is assumed
// to be a syntactically invalid XPath.
if (mechanism == L"xpath") {
LOG(WARN) << "Attempted to find elements using invalid xpath: "
<< LOGWSTRING(criteria);
status_code = EINVALIDSELECTOR;
} else {
LOG(WARN) << "Unexpected error attempting to find element by mechanism "
<< LOGWSTRING(mechanism) << " and criteria "
<< LOGWSTRING(criteria);
status_code = ENOSUCHELEMENT;
}
*found_elements = "A JavaScript error was encountered executing the findElements atom.";
}
} else {
LOG(WARN) << "Unable to get browser";
Expand Down
Loading

0 comments on commit afea9f5

Please sign in to comment.