From affb635427253bc516911b3b63e6f42f42d20772 Mon Sep 17 00:00:00 2001 From: "carlosgc@webkit.org" Date: Mon, 4 Dec 2017 11:29:29 +0000 Subject: [PATCH] WebDriver: implement element property command https://bugs.webkit.org/show_bug.cgi?id=180244 Reviewed by Brian Burg. 13.3 Get Element Property https://w3c.github.io/webdriver/webdriver-spec.html#get-element-property Fixes: imported/w3c/webdriver/tests/state/get_element_property.py::test_no_browsing_context imported/w3c/webdriver/tests/state/get_element_property.py::test_handle_prompt_dismiss imported/w3c/webdriver/tests/state/get_element_property.py::test_handle_prompt_accept imported/w3c/webdriver/tests/state/get_element_property.py::test_handle_prompt_missing_value imported/w3c/webdriver/tests/state/get_element_property.py::test_element_stale * Session.cpp: (WebDriver::Session::getElementAttribute): (WebDriver::Session::getElementProperty): * Session.h: * WebDriverService.cpp: (WebDriver::WebDriverService::getElementProperty): * WebDriverService.h: git-svn-id: https://svn.webkit.org/repository/webkit/trunk@225474 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- Source/WebDriver/ChangeLog | 24 ++++++++++++++++++++ Source/WebDriver/Session.cpp | 41 +++++++++++++++++++++++++++++++++++ Source/WebDriver/Session.h | 3 ++- Source/WebDriver/WebDriverService.cpp | 21 ++++++++++++++++++ Source/WebDriver/WebDriverService.h | 3 ++- 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/Source/WebDriver/ChangeLog b/Source/WebDriver/ChangeLog index e7cd759..661c08a 100644 --- a/Source/WebDriver/ChangeLog +++ b/Source/WebDriver/ChangeLog @@ -1,3 +1,27 @@ +2017-12-04 Carlos Garcia Campos + + WebDriver: implement element property command + https://bugs.webkit.org/show_bug.cgi?id=180244 + + Reviewed by Brian Burg. + + 13.3 Get Element Property + https://w3c.github.io/webdriver/webdriver-spec.html#get-element-property + + Fixes: imported/w3c/webdriver/tests/state/get_element_property.py::test_no_browsing_context + imported/w3c/webdriver/tests/state/get_element_property.py::test_handle_prompt_dismiss + imported/w3c/webdriver/tests/state/get_element_property.py::test_handle_prompt_accept + imported/w3c/webdriver/tests/state/get_element_property.py::test_handle_prompt_missing_value + imported/w3c/webdriver/tests/state/get_element_property.py::test_element_stale + + * Session.cpp: + (WebDriver::Session::getElementAttribute): + (WebDriver::Session::getElementProperty): + * Session.h: + * WebDriverService.cpp: + (WebDriver::WebDriverService::getElementProperty): + * WebDriverService.h: + 2017-12-02 Carlos Garcia Campos WebDriver: handle user prompts shown while executing scripts diff --git a/Source/WebDriver/Session.cpp b/Source/WebDriver/Session.cpp index 2f6d5ee..98733f7 100644 --- a/Source/WebDriver/Session.cpp +++ b/Source/WebDriver/Session.cpp @@ -1289,6 +1289,47 @@ void Session::getElementAttribute(const String& elementID, const String& attribu }); } +void Session::getElementProperty(const String& elementID, const String& property, Function&& completionHandler) +{ + if (!m_toplevelBrowsingContext) { + completionHandler(CommandResult::fail(CommandResult::ErrorCode::NoSuchWindow)); + return; + } + + handleUserPrompts([this, elementID, property, completionHandler = WTFMove(completionHandler)](CommandResult&& result) mutable { + if (result.isError()) { + completionHandler(WTFMove(result)); + return; + } + RefPtr arguments = JSON::Array::create(); + arguments->pushString(createElement(elementID)->toJSONString()); + + RefPtr parameters = JSON::Object::create(); + parameters->setString(ASCIILiteral("browsingContextHandle"), m_toplevelBrowsingContext.value()); + if (m_currentBrowsingContext) + parameters->setString(ASCIILiteral("frameHandle"), m_currentBrowsingContext.value()); + parameters->setString(ASCIILiteral("function"), makeString("function(element) { return element.", property, "; }")); + parameters->setArray(ASCIILiteral("arguments"), WTFMove(arguments)); + m_host->sendCommandToBackend(ASCIILiteral("evaluateJavaScriptFunction"), WTFMove(parameters), [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)](SessionHost::CommandResponse&& response) { + if (response.isError || !response.responseObject) { + completionHandler(CommandResult::fail(WTFMove(response.responseObject))); + return; + } + String valueString; + if (!response.responseObject->getString(ASCIILiteral("result"), valueString)) { + completionHandler(CommandResult::fail(CommandResult::ErrorCode::UnknownError)); + return; + } + RefPtr resultValue; + if (!JSON::Value::parseJSON(valueString, resultValue)) { + completionHandler(CommandResult::fail(CommandResult::ErrorCode::UnknownError)); + return; + } + completionHandler(CommandResult::success(WTFMove(resultValue))); + }); + }); +} + void Session::waitForNavigationToComplete(Function&& completionHandler) { if (!m_toplevelBrowsingContext) { diff --git a/Source/WebDriver/Session.h b/Source/WebDriver/Session.h index 373113d..f7b1ce9 100644 --- a/Source/WebDriver/Session.h +++ b/Source/WebDriver/Session.h @@ -86,12 +86,13 @@ public: void findElements(const String& strategy, const String& selector, FindElementsMode, const String& rootElementID, Function&&); void getActiveElement(Function&&); void isElementSelected(const String& elementID, Function&&); + void getElementAttribute(const String& elementID, const String& attribute, Function&&); + void getElementProperty(const String& elementID, const String& attribute, Function&&); void getElementText(const String& elementID, Function&&); void getElementTagName(const String& elementID, Function&&); void getElementRect(const String& elementID, Function&&); void isElementEnabled(const String& elementID, Function&&); void isElementDisplayed(const String& elementID, Function&&); - void getElementAttribute(const String& elementID, const String& attribute, Function&&); void elementClick(const String& elementID, Function&&); void elementClear(const String& elementID, Function&&); void elementSendKeys(const String& elementID, Vector&& keys, Function&&); diff --git a/Source/WebDriver/WebDriverService.cpp b/Source/WebDriver/WebDriverService.cpp index b635ded..1221584 100644 --- a/Source/WebDriver/WebDriverService.cpp +++ b/Source/WebDriver/WebDriverService.cpp @@ -127,6 +127,7 @@ const WebDriverService::Command WebDriverService::s_commands[] = { { HTTPMethod::Get, "/session/$sessionId/element/$elementId/selected", &WebDriverService::isElementSelected }, { HTTPMethod::Get, "/session/$sessionId/element/$elementId/attribute/$name", &WebDriverService::getElementAttribute }, + { HTTPMethod::Get, "/session/$sessionId/element/$elementId/property/$name", &WebDriverService::getElementProperty }, { HTTPMethod::Get, "/session/$sessionId/element/$elementId/text", &WebDriverService::getElementText }, { HTTPMethod::Get, "/session/$sessionId/element/$elementId/name", &WebDriverService::getElementTagName }, { HTTPMethod::Get, "/session/$sessionId/element/$elementId/rect", &WebDriverService::getElementRect }, @@ -1133,6 +1134,26 @@ void WebDriverService::getElementAttribute(RefPtr&& parameters, Fu m_session->getElementAttribute(elementID.value(), attribute, WTFMove(completionHandler)); } +void WebDriverService::getElementProperty(RefPtr&& parameters, Function&& completionHandler) +{ + // §13.3 Get Element Property + // https://w3c.github.io/webdriver/webdriver-spec.html#get-element-property + if (!findSessionOrCompleteWithError(*parameters, completionHandler)) + return; + + auto elementID = findElementOrCompleteWithError(*parameters, completionHandler); + if (!elementID) + return; + + String attribute; + if (!parameters->getString(ASCIILiteral("name"), attribute)) { + completionHandler(CommandResult::fail(CommandResult::ErrorCode::InvalidArgument)); + return; + } + + m_session->getElementProperty(elementID.value(), attribute, WTFMove(completionHandler)); +} + void WebDriverService::getElementText(RefPtr&& parameters, Function&& completionHandler) { // §13.5 Get Element Text. diff --git a/Source/WebDriver/WebDriverService.h b/Source/WebDriver/WebDriverService.h index c3d0812..e842172 100644 --- a/Source/WebDriver/WebDriverService.h +++ b/Source/WebDriver/WebDriverService.h @@ -84,12 +84,13 @@ private: void findElementsFromElement(RefPtr&&, Function&&); void getActiveElement(RefPtr&&, Function&&); void isElementSelected(RefPtr&&, Function&&); + void getElementAttribute(RefPtr&&, Function&&); + void getElementProperty(RefPtr&&, Function&&); void getElementText(RefPtr&&, Function&&); void getElementTagName(RefPtr&&, Function&&); void getElementRect(RefPtr&&, Function&&); void isElementEnabled(RefPtr&&, Function&&); void isElementDisplayed(RefPtr&&, Function&&); - void getElementAttribute(RefPtr&&, Function&&); void elementClick(RefPtr&&, Function&&); void elementClear(RefPtr&&, Function&&); void elementSendKeys(RefPtr&&, Function&&); -- 1.8.3.1