PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/abahdanovich/selenium
Java | 70 lines | 41 code | 14 blank | 15 comment | 2 complexity | 87a0815190d7d95325f38ee5dd9fb753 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-2010 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 ExecuteAsyncScript extends ResponseAwareWebDriverHandler
  26. implements JsonParametersAware {
  27. private volatile String script;
  28. private final List<Object> args = new ArrayList<Object>();
  29. public ExecuteAsyncScript(Session session) {
  30. super(session);
  31. }
  32. public void setJsonParameters(Map<String, Object> allParameters) throws Exception {
  33. script = (String) allParameters.get("script");
  34. List<?> params = (List<?>) allParameters.get("args");
  35. args.addAll(Lists.newArrayList(
  36. Iterables.transform(params, new ArgumentConverter(getKnownElements()))));
  37. }
  38. public ResultType call() throws Exception {
  39. Object value;
  40. if (args.size() > 0) {
  41. value = ((JavascriptExecutor) getDriver()).executeAsyncScript(script, args.toArray());
  42. } else {
  43. value = ((JavascriptExecutor) getDriver()).executeAsyncScript(script);
  44. }
  45. Object result = new ResultConverter(getKnownElements()).apply(value);
  46. response.setValue(result);
  47. return ResultType.SUCCESS;
  48. }
  49. @Override
  50. public String toString() {
  51. return String.format("[execute async script: %s, %s]", script, args);
  52. }
  53. }