int Element::IsDisplayed(bool* result) { LOG(TRACE) << "Entering Element::IsDisplayed"; int status_code = WD_SUCCESS; // The atom is just the definition of an anonymous // function: "function() {...}"; Wrap it in another function so we can // invoke it with our arguments without polluting the current namespace. std::wstring script_source(L"(function() { return ("); script_source += atoms::asString(atoms::IS_DISPLAYED); script_source += L")})();"; CComPtr<IHTMLDocument2> doc; this->GetContainingDocument(false, &doc); // N.B., The second argument to the IsDisplayed atom is "ignoreOpacity". Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(this->element_); script_wrapper.AddArgument(true); status_code = script_wrapper.Execute(); if (status_code == WD_SUCCESS) { *result = script_wrapper.result().boolVal == VARIANT_TRUE; } else { LOG(WARN) << "Failed to determine is element displayed"; } return status_code; }
int Element::GetAttributeValue(const std::string& attribute_name, std::string* attribute_value, bool* value_is_null) { LOG(TRACE) << "Entering Element::GetAttributeValue"; std::wstring wide_attribute_name = StringUtilities::ToWString(attribute_name); int status_code = WD_SUCCESS; // The atom is just the definition of an anonymous // function: "function() {...}"; Wrap it in another function so we can // invoke it with our arguments without polluting the current namespace. std::wstring script_source(L"(function() { return ("); script_source += atoms::asString(atoms::GET_ATTRIBUTE); script_source += L")})();"; CComPtr<IHTMLDocument2> doc; this->GetContainingDocument(false, &doc); Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(this->element_); script_wrapper.AddArgument(wide_attribute_name); status_code = script_wrapper.Execute(); if (status_code == WD_SUCCESS) { *value_is_null = !script_wrapper.ConvertResultToString(attribute_value); } else { LOG(WARN) << "Failed to determine element attribute"; } return WD_SUCCESS; }
bool Element::IsSelected() { LOG(TRACE) << "Entering Element::IsSelected"; bool selected(false); // The atom is just the definition of an anonymous // function: "function() {...}"; Wrap it in another function so we can // invoke it with our arguments without polluting the current namespace. std::wstring script_source(L"(function() { return ("); script_source += atoms::asString(atoms::IS_SELECTED); script_source += L")})();"; CComPtr<IHTMLDocument2> doc; this->GetContainingDocument(false, &doc); Script script_wrapper(doc, script_source, 1); script_wrapper.AddArgument(this->element_); int status_code = script_wrapper.Execute(); if (status_code == WD_SUCCESS && script_wrapper.ResultIsBoolean()) { selected = script_wrapper.result().boolVal == VARIANT_TRUE; } else { LOG(WARN) << "Unable to determine is element selected"; } return selected; }
int ElementFinder::FindElementsByCssSelector(const IESessionWindow& session, const ElementHandle parent_wrapper, const std::wstring& criteria, Json::Value* found_elements) { int result = ENOSUCHELEMENT; BrowserHandle browser; result = session.GetCurrentBrowser(&browser); if (result != SUCCESS) { return result; } std::wstring script_source(L"(function() { return function(){"); script_source += atoms::SIZZLE; script_source += L"\n"; script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;"; script_source += L"if (root['querySelectorAll']) { return root.querySelectorAll(arguments[0]); } "; script_source += L"var results = []; Sizzle(arguments[0], root, results);"; script_source += L"return results;"; script_source += L"};})();"; CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(criteria); if (parent_wrapper) { // Use a copy for the parent element? CComPtr<IHTMLElement> parent(parent_wrapper->element()); IHTMLElement* parent_element_copy; HRESULT hr = parent.CopyTo(&parent_element_copy); script_wrapper.AddArgument(parent_element_copy); } result = script_wrapper.Execute(); CComVariant snapshot = script_wrapper.result(); std::wstring get_element_count_script = L"(function(){return function() {return arguments[0].length;}})();"; Script get_element_count_script_wrapper(doc, get_element_count_script, 1); get_element_count_script_wrapper.AddArgument(snapshot); result = get_element_count_script_wrapper.Execute(); if (result == SUCCESS) { if (!get_element_count_script_wrapper.ResultIsInteger()) { result = EUNEXPECTEDJSERROR; } else { long length = get_element_count_script_wrapper.result().lVal; std::wstring get_next_element_script = L"(function(){return function() {return arguments[0][arguments[1]];}})();"; for (long i = 0; i < length; ++i) { Script get_element_script_wrapper(doc, get_next_element_script, 2); get_element_script_wrapper.AddArgument(snapshot); get_element_script_wrapper.AddArgument(i); result = get_element_script_wrapper.Execute(); Json::Value json_element; get_element_script_wrapper.ConvertResultToJsonValue(session, &json_element); found_elements->append(json_element); } } } return result; }
int ElementFinder::FindElementUsingSizzle(const IECommandExecutor& executor, const ElementHandle parent_wrapper, const std::wstring& criteria, Json::Value* found_element) { LOG(TRACE) << "Entering ElementFinder::FindElementUsingSizzle"; int result; BrowserHandle browser; result = executor.GetCurrentBrowser(&browser); if (result != WD_SUCCESS) { LOG(WARN) << "Unable to get browser"; return result; } std::wstring script_source(L"(function() { return function(){ if (!window.Sizzle) {"); script_source += atoms::asString(atoms::SIZZLE); script_source += L"}\n"; script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;"; script_source += L"if (root['querySelector']) { return root.querySelector(arguments[0]); } "; script_source += L"var results = []; Sizzle(arguments[0], root, results);"; script_source += L"return results.length > 0 ? results[0] : null;"; script_source += L"};})();"; CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(criteria); if (parent_wrapper) { CComPtr<IHTMLElement> parent(parent_wrapper->element()); script_wrapper.AddArgument(parent); } result = script_wrapper.Execute(); if (result == WD_SUCCESS) { if (!script_wrapper.ResultIsElement()) { LOG(WARN) << "Found result is not element"; result = ENOSUCHELEMENT; } else { result = script_wrapper.ConvertResultToJsonValue(executor, found_element); } } else { LOG(WARN) << "Unable to find elements"; result = ENOSUCHELEMENT; } return result; }
int ElementFinder::FindElement(const IESessionWindow& session, const ElementHandle parent_wrapper, const std::wstring& mechanism, const std::wstring& criteria, Json::Value* found_element) { BrowserHandle browser; int status_code = session.GetCurrentBrowser(&browser); if (status_code == SUCCESS) { if (mechanism == L"css") { return this->FindElementByCssSelector(session, parent_wrapper, criteria, found_element); } else if (mechanism == L"xpath") { return this->FindElementByXPath(session, parent_wrapper, criteria, found_element); } else { std::wstring criteria_object_script = L"(function() { return function(){ return { \"" + mechanism + L"\" : \"" + criteria + L"\" }; };})();"; CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); Script criteria_wrapper(doc, criteria_object_script, 0); status_code = criteria_wrapper.Execute(); if (status_code == SUCCESS) { CComVariant criteria_object; HRESULT hr = ::VariantCopy(&criteria_object, &criteria_wrapper.result()); // The atom is just the definition of an anonymous // function: "function() {...}"; Wrap it in another function so we can // invoke it with our arguments without polluting the current namespace. std::wstring script_source(L"(function() { return ("); script_source += atoms::FIND_ELEMENT; script_source += L")})();"; Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(criteria_object); if (parent_wrapper) { script_wrapper.AddArgument(parent_wrapper->element()); } status_code = script_wrapper.Execute(); if (status_code == SUCCESS && script_wrapper.ResultIsElement()) { script_wrapper.ConvertResultToJsonValue(session, found_element); } else { status_code = ENOSUCHELEMENT; } } else { status_code = ENOSUCHELEMENT; } } } return status_code; }
bool ElementFinder::HasNativeCssSelectorEngine(const IECommandExecutor& executor) { LOG(TRACE) << "Entering ElementFinder::HasNativeCssSelectorEngine"; BrowserHandle browser; executor.GetCurrentBrowser(&browser); std::wstring script_source(L"(function() { return function(){"); script_source += L"var root = document.documentElement;"; script_source += L"if (root['querySelectorAll']) { return true; } "; script_source += L"return false;"; script_source += L"};})();"; CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); Script script_wrapper(doc, script_source, 0); script_wrapper.Execute(); return script_wrapper.result().boolVal == VARIANT_TRUE; }
int ElementFinder::FindElementByCssSelector(const IESessionWindow& session, const ElementHandle parent_wrapper, const std::wstring& criteria, Json::Value* found_element) { int result = ENOSUCHELEMENT; BrowserHandle browser; result = session.GetCurrentBrowser(&browser); if (result != SUCCESS) { return result; } std::wstring script_source(L"(function() { return function(){"); script_source += atoms::SIZZLE; script_source += L"\n"; script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;"; script_source += L"if (root['querySelector']) { return root.querySelector(arguments[0]); } "; script_source += L"var results = []; Sizzle(arguments[0], root, results);"; script_source += L"return results.length > 0 ? results[0] : null;"; script_source += L"};})();"; CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(criteria); if (parent_wrapper) { CComPtr<IHTMLElement> parent(parent_wrapper->element()); IHTMLElement* parent_element_copy; HRESULT hr = parent.CopyTo(&parent_element_copy); script_wrapper.AddArgument(parent_element_copy); } result = script_wrapper.Execute(); if (result == SUCCESS) { if (!script_wrapper.ResultIsElement()) { result = ENOSUCHELEMENT; } else { result = script_wrapper.ConvertResultToJsonValue(session, found_element); } } return result; }
bool Element::IsHiddenByOverflow() { LOG(TRACE) << "Entering Element::IsHiddenByOverflow"; bool isOverflow = false; std::wstring script_source(L"(function() { return ("); script_source += atoms::asString(atoms::IS_IN_PARENT_OVERFLOW); script_source += L")})();"; CComPtr<IHTMLDocument2> doc; this->GetContainingDocument(false, &doc); Script script_wrapper(doc, script_source, 1); script_wrapper.AddArgument(this->element_); int status_code = script_wrapper.Execute(); if (status_code == WD_SUCCESS) { isOverflow = script_wrapper.result().boolVal == VARIANT_TRUE; } else { LOG(WARN) << "Unable to determine is element hidden by overflow"; } return isOverflow; }
int ElementFinder::FindElements(const IECommandExecutor& executor, const ElementHandle parent_wrapper, const std::wstring& mechanism, const std::wstring& criteria, Json::Value* found_elements) { LOG(TRACE) << "Entering ElementFinder::FindElements"; BrowserHandle browser; int status_code = executor.GetCurrentBrowser(&browser); if (status_code == WD_SUCCESS) { if (mechanism == L"css") { if (!this->HasNativeCssSelectorEngine(executor)) { LOG(DEBUG) << "Element location strategy is CSS selectors, but " << "document does not support CSS selectors. Falling back " << "to using the Sizzle JavaScript CSS selector engine."; return this->FindElementsUsingSizzle(executor, parent_wrapper, criteria, found_elements); } } LOG(DEBUG) << "Using FindElements atom to locate element having " << LOGWSTRING(mechanism) << " = " << LOGWSTRING(criteria); CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); std::wstring script_source(L"(function() { return ("); script_source += atoms::asString(atoms::FIND_ELEMENTS); script_source += L")})();"; Script script_wrapper(doc, script_source, 3); script_wrapper.AddArgument(mechanism); script_wrapper.AddArgument(criteria); if (parent_wrapper) { script_wrapper.AddArgument(parent_wrapper->element()); } status_code = script_wrapper.Execute(); if (status_code == WD_SUCCESS) { 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 { // Hitting a JavaScript error with the atom is an unrecoverable // error. The most common case of this for IE is when there is a // page refresh, navigation, or similar, and the driver is polling // for element presence. The calling code can't do anything about // it, so we might as well just log and return In the common case, // this means that the error will be transitory, and will sort // itself out once the DOM returns to normal after the page transition // is completed. LOG(WARN) << "A JavaScript error was encountered executing the findElements atom."; } } else { LOG(WARN) << "Unable to get browser"; } return status_code; }
int ElementFinder::FindElementsUsingSizzle(const IECommandExecutor& executor, const ElementHandle parent_wrapper, const std::wstring& criteria, Json::Value* found_elements) { LOG(TRACE) << "Entering ElementFinder::FindElementsUsingSizzle"; int result; if (criteria == L"") { // Apparently, Sizzle will happily return an empty array for an empty // string as the selector. We do not want this. return ENOSUCHELEMENT; } BrowserHandle browser; result = executor.GetCurrentBrowser(&browser); if (result != WD_SUCCESS) { LOG(WARN) << "Unable to get browser"; return result; } std::wstring script_source(L"(function() { return function(){ if (!window.Sizzle) {"); script_source += atoms::asString(atoms::SIZZLE); script_source += L"}\n"; script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;"; script_source += L"if (root['querySelectorAll']) { return root.querySelectorAll(arguments[0]); } "; script_source += L"var results = []; try { Sizzle(arguments[0], root, results); } catch(ex) { results = null; }"; script_source += L"return results;"; script_source += L"};})();"; CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(criteria); if (parent_wrapper) { // Use a copy for the parent element? CComPtr<IHTMLElement> parent(parent_wrapper->element()); script_wrapper.AddArgument(parent); } result = script_wrapper.Execute(); if (result == WD_SUCCESS) { CComVariant snapshot = script_wrapper.result(); if (snapshot.vt == VT_NULL || snapshot.vt == VT_EMPTY) { // We explicitly caught an error from Sizzle. Return ENOSUCHELEMENT. return ENOSUCHELEMENT; } std::wstring get_element_count_script = L"(function(){return function() {return arguments[0].length;}})();"; Script get_element_count_script_wrapper(doc, get_element_count_script, 1); get_element_count_script_wrapper.AddArgument(snapshot); result = get_element_count_script_wrapper.Execute(); if (result == WD_SUCCESS) { *found_elements = Json::Value(Json::arrayValue); if (!get_element_count_script_wrapper.ResultIsInteger()) { LOG(WARN) << "Found elements count is not integer"; result = EUNEXPECTEDJSERROR; } else { long length = get_element_count_script_wrapper.result().lVal; std::wstring get_next_element_script = L"(function(){return function() {return arguments[0][arguments[1]];}})();"; for (long i = 0; i < length; ++i) { Script get_element_script_wrapper(doc, get_next_element_script, 2); get_element_script_wrapper.AddArgument(snapshot); get_element_script_wrapper.AddArgument(i); result = get_element_script_wrapper.Execute(); if (result == WD_SUCCESS) { Json::Value json_element; get_element_script_wrapper.ConvertResultToJsonValue(executor, &json_element); found_elements->append(json_element); } else { LOG(WARN) << "Unable to get " << i << " found element"; } } } } else { LOG(WARN) << "Unable to get count of found elements"; result = EUNEXPECTEDJSERROR; } } else { LOG(WARN) << "Execution returned error"; } return result; }
int ElementFinder::FindElement(const IECommandExecutor& executor, const ElementHandle parent_wrapper, const std::wstring& mechanism, const std::wstring& criteria, Json::Value* found_element) { LOG(TRACE) << "Entering ElementFinder::FindElement"; BrowserHandle browser; int status_code = executor.GetCurrentBrowser(&browser); if (status_code == WD_SUCCESS) { if (mechanism == L"css") { if (!this->HasNativeCssSelectorEngine(executor)) { LOG(DEBUG) << "Element location strategy is CSS selectors, but " << "document does not support CSS selectors. Falling back " << "to using the Sizzle JavaScript CSS selector engine."; return this->FindElementUsingSizzle(executor, parent_wrapper, criteria, found_element); } } LOG(DEBUG) << L"Using FindElement atom to locate element having " << LOGWSTRING(mechanism) << " = " << LOGWSTRING(criteria); std::wstring sanitized_criteria = criteria; this->SanitizeCriteria(mechanism, &sanitized_criteria); std::wstring criteria_object_script = L"(function() { return function(){ return { \"" + mechanism + L"\" : \"" + sanitized_criteria + L"\" }; };})();"; CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); Script criteria_wrapper(doc, criteria_object_script, 0); status_code = criteria_wrapper.Execute(); if (status_code == WD_SUCCESS) { CComVariant criteria_object; criteria_object.Copy(&criteria_wrapper.result()); // The atom is just the definition of an anonymous // function: "function() {...}"; Wrap it in another function so we can // invoke it with our arguments without polluting the current namespace. std::wstring script_source(L"(function() { return ("); script_source += atoms::asString(atoms::FIND_ELEMENT); script_source += L")})();"; Script script_wrapper(doc, script_source, 2); script_wrapper.AddArgument(criteria_object); if (parent_wrapper) { script_wrapper.AddArgument(parent_wrapper->element()); } 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(sanitized_criteria); status_code = ENOSUCHELEMENT; } } 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(sanitized_criteria); status_code = EINVALIDSELECTOR; } else { LOG(WARN) << "Unexpected error attempting to find element by mechanism " << LOGWSTRING(mechanism) << " with criteria " << LOGWSTRING(sanitized_criteria); status_code = ENOSUCHELEMENT; } } } else { LOG(WARN) << "Unable to create criteria object for mechanism " << LOGWSTRING(mechanism) << " and criteria " << LOGWSTRING(sanitized_criteria); status_code = ENOSUCHELEMENT; } } else { LOG(WARN) << "Unable to get browser"; } return status_code; }
result_t util_base::compile(exlib::string srcname, exlib::string script, int32_t mode, obj_ptr<Buffer_base>& retVal) { Isolate* isolate = Isolate::current(); exlib::string oname = srcname; v8::Local<v8::String> soname = isolate->NewFromUtf8(oname); v8::Local<v8::Script> code; { TryCatch try_catch; { v8::ScriptCompiler::Source script_source( isolate->NewFromUtf8(script)); if (v8::ScriptCompiler::CompileUnbound( isolate->m_isolate, &script_source) .IsEmpty()) return throwSyntaxError(try_catch); } const char* args; switch (mode) { case 1: args = main_args; break; case 2: args = script_args; break; case 3: args = worker_args; break; default: args = module_args; break; } script = args + script + "\n});"; v8::ScriptCompiler::Source script_source( isolate->NewFromUtf8(script), v8::ScriptOrigin(soname)); if (v8::ScriptCompiler::CompileUnbound( isolate->m_isolate, &script_source, v8::ScriptCompiler::kProduceCodeCache) .IsEmpty()) return throwSyntaxError(try_catch); const v8::ScriptCompiler::CachedData* cache = script_source.GetCachedData(); exlib::string buf((const char*)cache->data, cache->length); int32_t len = (int32_t)script.length(); buf.append((const char*)&len, sizeof(len)); obj_ptr<Buffer_base> unz = new Buffer(buf); return zlib_base::cc_gzip(unz, retVal); } return 0; }
int ElementFinder::FindElements(const IECommandExecutor& executor, const ElementHandle parent_wrapper, const std::wstring& mechanism, const std::wstring& criteria, Json::Value* found_elements) { LOG(TRACE) << "Entering ElementFinder::FindElements"; BrowserHandle browser; int status_code = executor.GetCurrentBrowser(&browser); if (status_code == WD_SUCCESS) { if (mechanism == L"css") { if (!this->HasNativeCssSelectorEngine(executor)) { LOG(DEBUG) << "Element location strategy is CSS selectors, but " << "document does not support CSS selectors. Falling back " << "to using the Sizzle JavaScript CSS selector engine."; return this->FindElementsUsingSizzle(executor, parent_wrapper, criteria, found_elements); } } LOG(DEBUG) << "Using FindElements atom to locate element having " << LOGWSTRING(mechanism) << " = " << LOGWSTRING(criteria); CComPtr<IHTMLDocument2> doc; browser->GetDocument(&doc); std::wstring script_source(L"(function() { return ("); script_source += atoms::asString(atoms::FIND_ELEMENTS); script_source += L")})();"; Script script_wrapper(doc, script_source, 3); script_wrapper.AddArgument(mechanism); script_wrapper.AddArgument(criteria); if (parent_wrapper) { script_wrapper.AddArgument(parent_wrapper->element()); } 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; } } 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; } } } else { LOG(WARN) << "Unable to get browser"; } return status_code; }