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