PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/java/server/src/org/openqa/selenium/remote/server/handler/ExecuteScript.java

https://bitbucket.org/abahdanovich/selenium
Java | 68 lines | 40 code | 13 blank | 15 comment | 2 complexity | d9cfd1e6a688e4492719d504cd9ce7a2 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-1.0, MIT, Apache-2.0, BSD-3-Clause, GPL-2.0
  1. /*
  2. Copyright 2007-2009 Selenium committers
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package org.openqa.selenium.remote.server.handler;
  14. import com.google.common.collect.Iterables;
  15. import com.google.common.collect.Lists;
  16. import org.openqa.selenium.JavascriptExecutor;
  17. import org.openqa.selenium.remote.server.JsonParametersAware;
  18. import org.openqa.selenium.remote.server.Session;
  19. import org.openqa.selenium.remote.server.handler.internal.ArgumentConverter;
  20. import org.openqa.selenium.remote.server.handler.internal.ResultConverter;
  21. import org.openqa.selenium.remote.server.rest.ResultType;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Map;
  25. public class ExecuteScript extends ResponseAwareWebDriverHandler implements JsonParametersAware {
  26. private volatile String script;
  27. private volatile List<Object> args = new ArrayList<Object>();
  28. public ExecuteScript(Session session) {
  29. super(session);
  30. }
  31. public void setJsonParameters(Map<String, Object> allParameters) throws Exception {
  32. script = (String) allParameters.get("script");
  33. List<?> params = (List<?>) allParameters.get("args");
  34. args = Lists.newArrayList(Iterables.transform(params,
  35. new ArgumentConverter(getKnownElements())));
  36. }
  37. public ResultType call() throws Exception {
  38. Object value;
  39. if (args.size() > 0) {
  40. value = ((JavascriptExecutor) getDriver()).executeScript(script, args.toArray());
  41. } else {
  42. value = ((JavascriptExecutor) getDriver()).executeScript(script);
  43. }
  44. Object result = new ResultConverter(getKnownElements()).apply(value);
  45. response.setValue(result);
  46. return ResultType.SUCCESS;
  47. }
  48. @Override
  49. public String toString() {
  50. return String.format("[execute script: %s, %s]", script, args);
  51. }
  52. }