PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/java/client/src/org/openqa/selenium/internal/seleniumemulation/JavascriptLibrary.java

https://bitbucket.org/abahdanovich/selenium
Java | 116 lines | 73 code | 22 blank | 21 comment | 5 complexity | e587f85fdcfa071c3b8e72c8ba38708b 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.internal.seleniumemulation;
  14. import com.google.common.base.Charsets;
  15. import com.google.common.base.Throwables;
  16. import com.google.common.io.Resources;
  17. import org.openqa.selenium.JavascriptExecutor;
  18. import org.openqa.selenium.WebDriver;
  19. import org.openqa.selenium.WebElement;
  20. import java.io.IOException;
  21. import java.net.URL;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. public class JavascriptLibrary {
  27. static final String PREFIX = "/" + JavascriptLibrary.class.getPackage()
  28. .getName().replace(".", "/") + "/selenium_atoms/";
  29. private final ConcurrentHashMap<String, String> scripts = new ConcurrentHashMap<String, String>();
  30. private static final String injectableSelenium =
  31. "/org/openqa/selenium/internal/seleniumemulation/scripts/injectableSelenium.js";
  32. private static final String htmlUtils =
  33. "/org/openqa/selenium/internal/seleniumemulation/scripts/htmlutils.js";
  34. /**
  35. * Loads the named Selenium script and returns it wrapped in an anonymous function.
  36. *
  37. * @param name The script to load.
  38. * @return The loaded script wrapped in an anonymous function.
  39. */
  40. public String getSeleniumScript(String name) {
  41. String rawFunction = readScript(PREFIX + name);
  42. return String.format("function() { return (%s).apply(null, arguments);}",
  43. rawFunction);
  44. }
  45. public void callEmbeddedSelenium(WebDriver driver, String functionName,
  46. WebElement element, Object... values) {
  47. StringBuilder builder = new StringBuilder(readScript(injectableSelenium));
  48. builder.append("return browserbot.").append(functionName)
  49. .append(".apply(browserbot, arguments);");
  50. List<Object> args = new ArrayList<Object>();
  51. args.add(element);
  52. args.addAll(Arrays.asList(values));
  53. ((JavascriptExecutor) driver).executeScript(builder.toString(), args.toArray());
  54. }
  55. public Object callEmbeddedHtmlUtils(WebDriver driver, String functionName, WebElement element,
  56. Object... values) {
  57. StringBuilder builder = new StringBuilder(readScript(htmlUtils));
  58. builder.append("return htmlutils.").append(functionName)
  59. .append(".apply(htmlutils, arguments);");
  60. List<Object> args = new ArrayList<Object>();
  61. args.add(element);
  62. args.addAll(Arrays.asList(values));
  63. return ((JavascriptExecutor) driver).executeScript(builder.toString(), args.toArray());
  64. }
  65. public Object executeScript(WebDriver driver, String script, Object... args) {
  66. if (driver instanceof JavascriptExecutor) {
  67. return ((JavascriptExecutor) driver).executeScript(script, args);
  68. }
  69. throw new UnsupportedOperationException(
  70. "The underlying WebDriver instance does not support executing javascript");
  71. }
  72. private String readScript(String script) {
  73. String result = scripts.get(script);
  74. if (result == null) {
  75. result = readScriptImpl(script);
  76. scripts.put(script, result);
  77. }
  78. return result;
  79. }
  80. String readScriptImpl(String script) {
  81. URL url = getClass().getResource(script);
  82. if (url == null) {
  83. throw new RuntimeException("Cannot locate " + script);
  84. }
  85. try {
  86. return Resources.toString(url, Charsets.UTF_8);
  87. } catch (IOException e) {
  88. throw Throwables.propagate(e);
  89. }
  90. }
  91. }