PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-webdriver/atlassian-webdriver-core/src/main/java/com/atlassian/webdriver/debug/WebDriverDebug.java

https://bitbucket.org/atlassian/atlassian-selenium
Java | 159 lines | 102 code | 14 blank | 43 comment | 1 complexity | 91ea96f1d48ea7ef905bfd99072aa817 MD5 | raw file
  1. package com.atlassian.webdriver.debug;
  2. import org.apache.commons.io.FileUtils;
  3. import org.apache.commons.io.IOUtils;
  4. import org.openqa.selenium.OutputType;
  5. import org.openqa.selenium.TakesScreenshot;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebDriverException;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import javax.annotation.Nonnull;
  11. import javax.inject.Inject;
  12. import java.io.File;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.io.Writer;
  16. import static com.atlassian.webdriver.utils.WebDriverUtil.as;
  17. import static com.atlassian.webdriver.utils.WebDriverUtil.isInstance;
  18. import static java.util.Objects.requireNonNull;
  19. /**
  20. * An injectable component for performing common debug operations using {@link WebDriver}.
  21. *
  22. * @since 2.2
  23. */
  24. public final class WebDriverDebug
  25. {
  26. private static final Logger log = LoggerFactory.getLogger(WebDriverDebug.class);
  27. private final WebDriver webDriver;
  28. @Inject
  29. public WebDriverDebug(@Nonnull WebDriver webDriver)
  30. {
  31. this.webDriver = requireNonNull(webDriver, "webDriver");
  32. }
  33. /**
  34. * Get the URL that the underlying web driver is currently at.
  35. *
  36. * @return current URL
  37. */
  38. @Nonnull
  39. public String getCurrentUrl()
  40. {
  41. return webDriver.getCurrentUrl();
  42. }
  43. /**
  44. * <p>
  45. * Writes the source of the last loaded page to the specified file. See {@link #dumpPageSourceTo(java.io.Writer)}
  46. * for details on how it works.
  47. * </p>
  48. *
  49. * <p>
  50. * As opposed to {@link #dumpPageSourceTo(java.io.Writer)}, this method makes sure to close the handle used
  51. * to write to {@code dumpFile}.
  52. * </p>
  53. *
  54. * @param dumpFile File to write the source to.
  55. *
  56. */
  57. public boolean dumpSourceTo(@Nonnull File dumpFile)
  58. {
  59. requireNonNull(dumpFile, "dumpFile");
  60. FileWriter fileWriter = null;
  61. try
  62. {
  63. fileWriter = new FileWriter(dumpFile);
  64. IOUtils.write(webDriver.getPageSource(), fileWriter);
  65. return true;
  66. }
  67. catch (IOException e)
  68. {
  69. log.warn("Error dumping page source to " + dumpFile.getAbsolutePath() + ": " + e.getMessage());
  70. log.debug("Error dumping page source - details", e);
  71. return false;
  72. }
  73. finally
  74. {
  75. IOUtils.closeQuietly(fileWriter);
  76. }
  77. }
  78. /**
  79. * <p>
  80. * Writes the source of the last loaded page to the specified writer. The writer is othewrwise not interacted with.
  81. * In particular, clients have to take care to close the {@code writer}.
  82. * </p>
  83. *
  84. * <p>
  85. * This method will attempt to not propagate any exceptions that prevent it from completing. Instead, the write will
  86. * be aborted, the exception logged and {@literal false} returned. The only exception raised by this method is
  87. * {@link NullPointerException} in case {@code writer} is {@literal null}.
  88. * </p>
  89. *
  90. * @param writer writer to write the page source to
  91. */
  92. public boolean dumpPageSourceTo(@Nonnull Writer writer)
  93. {
  94. requireNonNull(writer, "writer");
  95. try
  96. {
  97. IOUtils.write(webDriver.getPageSource(), writer);
  98. return true;
  99. }
  100. catch (IOException e)
  101. {
  102. log.warn("Error dumping page source to " + writer + ": " + e.getMessage());
  103. log.debug("Error dumping page source - details", e);
  104. return false;
  105. }
  106. }
  107. /**
  108. * Saves screen shot of the browser to the specified file.
  109. *
  110. * @param destFile File to save screen shot.
  111. */
  112. public boolean takeScreenshotTo(@Nonnull File destFile)
  113. {
  114. requireNonNull(destFile, "destFile");
  115. if (isInstance(webDriver, TakesScreenshot.class))
  116. {
  117. TakesScreenshot shotter = as(webDriver, TakesScreenshot.class);
  118. log.info("Saving screenshot to: " + destFile.getAbsolutePath());
  119. try
  120. {
  121. File screenshot = shotter.getScreenshotAs(OutputType.FILE);
  122. FileUtils.copyFile(screenshot, destFile);
  123. return true;
  124. }
  125. catch (IOException e)
  126. {
  127. logScreenshotError(destFile, e);
  128. }
  129. catch (WebDriverException e)
  130. {
  131. logScreenshotError(destFile, e);
  132. }
  133. }
  134. else
  135. {
  136. log.warn("Driver {} is not capable of taking screenshots.", webDriver);
  137. }
  138. return false;
  139. }
  140. private void logScreenshotError(File destFile, Exception e)
  141. {
  142. log.warn("Could not capture screenshot to {}: {}", destFile, e.getMessage());
  143. log.debug("Capture screenshot - error details", e);
  144. }
  145. }