1 package com.atlassian.webdriver.rule;
2
3 import com.atlassian.webdriver.testing.rule.WebDriverScreenshotRule;
4 import com.google.common.base.Suppliers;
5 import org.apache.commons.io.FileUtils;
6 import org.junit.Before;
7 import org.junit.Rule;
8 import org.junit.Test;
9 import org.junit.rules.TemporaryFolder;
10 import org.junit.runner.Description;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.model.Statement;
13 import org.mockito.Mock;
14 import org.mockito.runners.MockitoJUnitRunner;
15 import org.openqa.selenium.OutputType;
16 import org.openqa.selenium.TakesScreenshot;
17 import org.openqa.selenium.WebDriver;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import static com.atlassian.webdriver.matchers.FileMatchers.*;
23 import static org.apache.commons.io.FileUtils.readFileToString;
24 import static org.hamcrest.Matchers.not;
25 import static org.junit.Assert.*;
26 import static org.mockito.Mockito.*;
27
28
29
30
31
32
33 @RunWith(MockitoJUnitRunner.class)
34 public class TestWebDriverScreenshotRule
35 {
36
37 @Mock private Statement mockTest;
38
39 private WebDriver webDriver;
40
41 @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
42
43 @Before
44 public void initWebDriver()
45 {
46 webDriver = mock(WebDriver.class, withSettings().extraInterfaces(TakesScreenshot.class));
47 }
48
49 @Test
50 public void shouldTakeScreenshotAndPageDump() throws Throwable
51 {
52 final String fakeScreenshotContent = "FAKE SCREENSHOT!";
53 when(asTakingScreenshot().getScreenshotAs(OutputType.FILE)).thenReturn(prepareFakeScreenshot("fake.png", fakeScreenshotContent));
54 when(webDriver.getCurrentUrl()).thenReturn("something");
55 final String pageSource = "<html>some source</html>";
56 when(webDriver.getPageSource()).thenReturn(pageSource);
57 doThrow(new RuntimeException("failed")).when(mockTest).evaluate();
58 final WebDriverScreenshotRule rule = new WebDriverScreenshotRule(Suppliers.ofInstance(webDriver),
59 temporaryFolder.getRoot());
60 final Description description = Description.createTestDescription(TestWebDriverScreenshotRule.class, "someTest");
61 new SafeStatementInvoker(rule.apply(mockTest, description)).invokeSafely();
62 assertThat(expectedTargetDir(), isDirectory());
63 assertFilesGeneratedProperly(fakeScreenshotContent, pageSource, "someTest.html", "someTest.png");
64 }
65
66 private File prepareFakeScreenshot(final String fileName, final String data) throws IOException {
67 final File fakeScreenshot = temporaryFolder.newFile(fileName);
68 FileUtils.writeStringToFile(fakeScreenshot, data);
69 return fakeScreenshot;
70 }
71
72 @Test
73 public void shouldNotOverrideScreenshotAndPageDump() throws Throwable {
74 final String[] screenshotContents = {"Fake Screenshot 1", "Fake Screenshot 2", "Fake Screenshot 3"};
75 when(asTakingScreenshot().getScreenshotAs(OutputType.FILE))
76 .thenReturn(prepareFakeScreenshot("fake-1.png", screenshotContents[0]))
77 .thenReturn(prepareFakeScreenshot("fake-2.png", screenshotContents[1]))
78 .thenReturn(prepareFakeScreenshot("fake-3.png", screenshotContents[2]));
79
80 when(webDriver.getCurrentUrl()).thenReturn("something");
81 final String[] sourceConents = {"<html>source 1</html>", "<html>source 2</html>", "<html>source 3</html>"};
82 when(webDriver.getPageSource()).thenReturn(sourceConents[0]).thenReturn(sourceConents[1]).thenReturn(sourceConents[2]);
83
84 doThrow(new RuntimeException("failed")).when(mockTest).evaluate();
85 final WebDriverScreenshotRule rule = new WebDriverScreenshotRule(Suppliers.ofInstance(webDriver),
86 temporaryFolder.getRoot());
87 final Description description = Description.createTestDescription(TestWebDriverScreenshotRule.class, "someTest");
88
89
90 final String[] expectedFileNames = {"someTest", "someTest-1", "someTest-2"};
91 for (int i = 0; i < screenshotContents.length; i++) {
92 new SafeStatementInvoker(rule.apply(mockTest, description)).invokeSafely();
93 assertThat(expectedTargetDir(), isDirectory());
94 final String fn = expectedFileNames[i];
95 assertFilesGeneratedProperly(screenshotContents[i], sourceConents[i], fn + ".html", fn + ".png");
96 }
97 }
98
99 private void assertFilesGeneratedProperly(String firstFakeScreenshotContent, String sourceForFirstTest, String htmlFileName,
100 String screenshotFileName) throws IOException {
101 final File firstExpectedHtmlFile = expectedTargetFile(htmlFileName);
102 final File firstExpectedScreenshotfile = expectedTargetFile(screenshotFileName);
103
104 assertThat(firstExpectedHtmlFile, isFile());
105 assertEquals(sourceForFirstTest, readFileToString(firstExpectedHtmlFile));
106 assertThat(firstExpectedScreenshotfile, isFile());
107 assertEquals(firstFakeScreenshotContent, readFileToString(firstExpectedScreenshotfile));
108 }
109
110 @Test
111 public void shouldNotTakeScreenshotIfNotFailed() throws Throwable
112 {
113 when(webDriver.getCurrentUrl()).thenReturn("something");
114 when(webDriver.getPageSource()).thenReturn("<html>some source</html>");
115 final WebDriverScreenshotRule rule = new WebDriverScreenshotRule(Suppliers.ofInstance(webDriver),
116 temporaryFolder.getRoot());
117 final Description description = Description.createTestDescription(TestWebDriverScreenshotRule.class, "someTest");
118 new SafeStatementInvoker(rule.apply(mockTest, description)).invokeSafely();
119 verifyZeroInteractions(webDriver);
120 assertThat(expectedTargetDir(), isDirectory());
121 assertThat(expectedTargetFile("someTest.html"), not(exists()));
122 assertThat(expectedTargetFile("someTest.png"), not(exists()));
123 }
124
125 private TakesScreenshot asTakingScreenshot()
126 {
127 return (TakesScreenshot) webDriver;
128 }
129
130 private File expectedTargetDir()
131 {
132 return new File(temporaryFolder.getRoot(), TestWebDriverScreenshotRule.class.getName());
133 }
134
135 private File expectedTargetFile(String name)
136 {
137 return new File(expectedTargetDir(), name);
138 }
139
140
141 }