| 191 | } |
| 192 | |
| 193 | int ElementFinder::FindElementUsingSizzle(const IECommandExecutor& executor, |
| 194 | const ElementHandle parent_wrapper, |
| 195 | const std::wstring& criteria, |
| 196 | Json::Value* found_element) { |
| 197 | LOG(TRACE) << "Entering ElementFinder::FindElementUsingSizzle"; |
| 198 | |
| 199 | int result; |
| 200 | |
| 201 | BrowserHandle browser; |
| 202 | result = executor.GetCurrentBrowser(&browser); |
| 203 | if (result != WD_SUCCESS) { |
| 204 | LOG(WARN) << "Unable to get browser"; |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | std::wstring script_source(L"(function() { return function(){ if (!window.Sizzle) {"); |
| 209 | script_source += atoms::asString(atoms::SIZZLE); |
| 210 | script_source += L"}\n"; |
| 211 | script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;"; |
| 212 | script_source += L"if (root['querySelector']) { return root.querySelector(arguments[0]); } "; |
| 213 | script_source += L"var results = []; Sizzle(arguments[0], root, results);"; |
| 214 | script_source += L"return results.length > 0 ? results[0] : null;"; |
| 215 | script_source += L"};})();"; |
| 216 | |
| 217 | CComPtr<IHTMLDocument2> doc; |
| 218 | browser->GetDocument(&doc); |
| 219 | Script script_wrapper(doc, script_source, 2); |
| 220 | script_wrapper.AddArgument(criteria); |
| 221 | if (parent_wrapper) { |
| 222 | CComPtr<IHTMLElement> parent(parent_wrapper->element()); |
| 223 | script_wrapper.AddArgument(parent); |
| 224 | } |
| 225 | result = script_wrapper.Execute(); |
| 226 | |
| 227 | if (result == WD_SUCCESS) { |
| 228 | if (!script_wrapper.ResultIsElement()) { |
| 229 | LOG(WARN) << "Found result is not element"; |
| 230 | result = ENOSUCHELEMENT; |
| 231 | } else { |
| 232 | result = script_wrapper.ConvertResultToJsonValue(executor, |
| 233 | found_element); |
| 234 | } |
| 235 | } else { |
| 236 | LOG(WARN) << "Unable to find elements"; |
| 237 | result = ENOSUCHELEMENT; |
| 238 | } |
| 239 | |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | int ElementFinder::FindElementsUsingSizzle(const IECommandExecutor& executor, |
| 244 | const ElementHandle parent_wrapper, |
no test coverage detected