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

/atlassian-webdriver/atlassian-webdriver-core/src/test/java/com/atlassian/webdriver/debug/TestWebDriverDebug.java

https://bitbucket.org/atlassian/atlassian-selenium
Java | 175 lines | 147 code | 22 blank | 6 comment | 0 complexity | 1e1ba9557192c705f0e7bf932a0e9de4 MD5 | raw file
  1. package com.atlassian.webdriver.debug;
  2. import org.junit.Before;
  3. import org.junit.Rule;
  4. import org.junit.Test;
  5. import org.junit.rules.TemporaryFolder;
  6. import org.junit.runner.RunWith;
  7. import org.mockito.junit.MockitoJUnitRunner;
  8. import org.openqa.selenium.OutputType;
  9. import org.openqa.selenium.TakesScreenshot;
  10. import org.openqa.selenium.WebDriver;
  11. import org.openqa.selenium.WrapsDriver;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.io.StringWriter;
  15. import static org.apache.commons.io.FileUtils.readFileToString;
  16. import static org.apache.commons.io.FileUtils.writeStringToFile;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.mockito.Mockito.mock;
  21. import static org.mockito.Mockito.when;
  22. import static org.mockito.Mockito.withSettings;
  23. /**
  24. * Test case for {@link WebDriverDebug}.
  25. *
  26. * @since 2.2
  27. */
  28. @RunWith(MockitoJUnitRunner.Silent.class)
  29. public class TestWebDriverDebug
  30. {
  31. private WebDriver mockDriver;
  32. private WebDriver mockWrappingDriver;
  33. private WebDriver mockDriverTakingScreenshot;
  34. private WebDriver mockDriverNotTakingScreenshot;
  35. @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
  36. @Before
  37. public void initDrivers()
  38. {
  39. mockDriver = mock(WebDriver.class);
  40. mockDriverTakingScreenshot = mock(WebDriver.class, withSettings().extraInterfaces(TakesScreenshot.class));
  41. mockDriverNotTakingScreenshot = mock(WebDriver.class);
  42. mockWrappingDriver = mock(WebDriver.class, withSettings().extraInterfaces(WrapsDriver.class));
  43. }
  44. @SuppressWarnings("ConstantConditions")
  45. @Test(expected = NullPointerException.class)
  46. public void shouldNotAcceptNullDriver()
  47. {
  48. new WebDriverDebug(null);
  49. }
  50. @Test
  51. public void shouldReturnCurrentUrl()
  52. {
  53. when(mockDriver.getCurrentUrl()).thenReturn("http://some-url.com");
  54. assertEquals("http://some-url.com", new WebDriverDebug(mockDriver).getCurrentUrl());
  55. }
  56. @Test
  57. public void dumpPageSourceToNonExistingFile() throws IOException
  58. {
  59. final File dumpDir = temporaryFolder.newFolder("TestWebDriverDebug");
  60. final File dumpFile = new File(dumpDir, "test.html");
  61. when(mockDriver.getPageSource()).thenReturn("<html>Awesome</html>");
  62. final WebDriverDebug debug = new WebDriverDebug(mockDriver);
  63. assertTrue("Should dump HTML source successfully", debug.dumpSourceTo(dumpFile));
  64. assertEquals("<html>Awesome</html>", readFileToString(dumpFile));
  65. }
  66. @Test
  67. public void dumpPageSourceToExistingFile() throws IOException
  68. {
  69. final File dumpFile = temporaryFolder.newFile("test.html");
  70. writeStringToFile(dumpFile, "blah blah");
  71. when(mockDriver.getPageSource()).thenReturn("<html>Awesome no blah</html>");
  72. final WebDriverDebug debug = new WebDriverDebug(mockDriver);
  73. assertTrue("Should dump HTML source successfully", debug.dumpSourceTo(dumpFile));
  74. // should overwrite the previous contents
  75. assertEquals("<html>Awesome no blah</html>", readFileToString(dumpFile));
  76. }
  77. @Test
  78. public void dumpPageShouldNotThrowExceptionOnIoError() throws IOException
  79. {
  80. final File dumpFile = temporaryFolder.newFile("test.html");
  81. assertTrue(dumpFile.setWritable(false));
  82. when(mockDriver.getPageSource()).thenReturn("<html>Awesome</html>");
  83. final WebDriverDebug debug = new WebDriverDebug(mockDriver);
  84. assertFalse("Should fail to dump HTML source into non-writable file", debug.dumpSourceTo(dumpFile));
  85. }
  86. @Test
  87. public void dumpPageSourceToWriterShouldWorkGivenWorkingWriter()
  88. {
  89. when(mockDriver.getPageSource()).thenReturn("<html>Awesome</html>");
  90. final WebDriverDebug debug = new WebDriverDebug(mockDriver);
  91. final StringWriter output = new StringWriter();
  92. assertTrue("Should dump HTML source successfully", debug.dumpPageSourceTo(output));
  93. assertEquals("<html>Awesome</html>", output.toString());
  94. }
  95. @Test
  96. public void shouldTakeScreenshotGivenDriverTakesScreenshot() throws IOException
  97. {
  98. final File dumpDir = temporaryFolder.newFolder("TestWebDriverDebug");
  99. final File fakeScreenshot = temporaryFolder.newFile("fake.png");
  100. final File testOutput = new File(dumpDir, "test.png");
  101. writeStringToFile(fakeScreenshot, "FAKE SCREENSHOT!");
  102. when(mockDriverTakingScreenshot().getScreenshotAs(OutputType.FILE)).thenReturn(fakeScreenshot);
  103. final WebDriverDebug debug = new WebDriverDebug(mockDriverTakingScreenshot);
  104. assertTrue(debug.takeScreenshotTo(testOutput));
  105. assertEquals("FAKE SCREENSHOT!", readFileToString(testOutput));
  106. }
  107. @Test
  108. public void shouldTakeScreenshotGivenUnderlyingDriverTakesScreenshot() throws IOException
  109. {
  110. final File dumpDir = temporaryFolder.newFolder("TestWebDriverDebug");
  111. final File fakeScreenshot = temporaryFolder.newFile("fake.png");
  112. final File testOutput = new File(dumpDir, "test.png");
  113. writeStringToFile(fakeScreenshot, "FAKE SCREENSHOT!");
  114. when(mockDriverTakingScreenshot().getScreenshotAs(OutputType.FILE)).thenReturn(fakeScreenshot);
  115. stubWrappedDriver(mockDriverTakingScreenshot);
  116. final WebDriverDebug debug = new WebDriverDebug(mockWrappingDriver);
  117. assertTrue(debug.takeScreenshotTo(testOutput));
  118. assertEquals("FAKE SCREENSHOT!", readFileToString(testOutput));
  119. }
  120. @Test
  121. public void shouldNotTakeScreenshotGivenNoUnderlyingDriverTakesScreenshot() throws IOException
  122. {
  123. final File dumpDir = temporaryFolder.newFolder("TestWebDriverDebug");
  124. final File fakeScreenshot = temporaryFolder.newFile("fake.png");
  125. final File testOutput = new File(dumpDir, "test.png");
  126. writeStringToFile(fakeScreenshot, "FAKE SCREENSHOT!");
  127. when(mockDriverTakingScreenshot().getScreenshotAs(OutputType.FILE)).thenReturn(fakeScreenshot);
  128. stubWrappedDriver(mockDriverNotTakingScreenshot);
  129. final WebDriverDebug debug = new WebDriverDebug(mockWrappingDriver);
  130. assertFalse("Screenshot should not be taken", debug.takeScreenshotTo(testOutput));
  131. }
  132. @Test
  133. public void shouldNotTakeScreenshotGivenIoError() throws IOException
  134. {
  135. final File fakeScreenshot = temporaryFolder.newFile("fake.png");
  136. final File testOutput = temporaryFolder.newFile("output.png");
  137. assertTrue(testOutput.setWritable(false));
  138. writeStringToFile(fakeScreenshot, "FAKE SCREENSHOT!");
  139. when(mockDriverTakingScreenshot().getScreenshotAs(OutputType.FILE)).thenReturn(fakeScreenshot);
  140. stubWrappedDriver(mockDriverTakingScreenshot);
  141. final WebDriverDebug debug = new WebDriverDebug(mockWrappingDriver);
  142. assertFalse("Screenshot should not be taken given output file not writable", debug.takeScreenshotTo(testOutput));
  143. }
  144. private void stubWrappedDriver(WebDriver wrappedDriver)
  145. {
  146. final WrapsDriver wrapsDriver = (WrapsDriver) mockWrappingDriver;
  147. when(wrapsDriver.getWrappedDriver()).thenReturn(wrappedDriver);
  148. }
  149. private TakesScreenshot mockDriverTakingScreenshot()
  150. {
  151. return (TakesScreenshot) mockDriverTakingScreenshot;
  152. }
  153. }