|
1 | 1 | package info.novatec.testit.webtester.browser.operations;
|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat;
|
| 4 | +import static org.mockito.Matchers.any; |
| 5 | +import static org.mockito.Mockito.doReturn; |
| 6 | +import static org.mockito.Mockito.doThrow; |
| 7 | +import static org.mockito.Mockito.never; |
| 8 | +import static org.mockito.Mockito.verify; |
4 | 9 |
|
5 | 10 | import java.io.File;
|
| 11 | +import java.io.IOException; |
| 12 | +import java.util.List; |
6 | 13 | import java.util.Optional;
|
| 14 | +import java.util.function.Supplier; |
7 | 15 |
|
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.ClassRule; |
8 | 18 | import org.junit.Test;
|
| 19 | +import org.junit.experimental.runners.Enclosed; |
| 20 | +import org.junit.rules.TemporaryFolder; |
9 | 21 | import org.junit.runner.RunWith;
|
| 22 | +import org.mockito.ArgumentCaptor; |
| 23 | +import org.mockito.Captor; |
| 24 | +import org.mockito.InjectMocks; |
10 | 25 | import org.mockito.Mock;
|
11 | 26 | import org.mockito.runners.MockitoJUnitRunner;
|
| 27 | +import org.openqa.selenium.OutputType; |
| 28 | +import org.openqa.selenium.TakesScreenshot; |
12 | 29 | import org.openqa.selenium.WebDriver;
|
13 | 30 |
|
| 31 | +import com.google.common.primitives.Bytes; |
| 32 | + |
14 | 33 | import integration.browser.operations.ScreenshotTakerIntegrationTest;
|
15 | 34 |
|
16 | 35 | import info.novatec.testit.webtester.browser.Browser;
|
17 |
| -import info.novatec.testit.webtester.browser.WebDriverBrowser; |
| 36 | +import info.novatec.testit.webtester.config.Configuration; |
| 37 | +import info.novatec.testit.webtester.events.Event; |
| 38 | +import info.novatec.testit.webtester.events.EventSystem; |
| 39 | +import info.novatec.testit.webtester.events.browser.TookScreenshotEvent; |
18 | 40 |
|
19 | 41 |
|
20 | 42 | /**
|
21 | 43 | * Since taking screenshots is a very complex (in the sense of involved classes and systems) operation this class is mainly
|
22 | 44 | * integration tests by {@link ScreenshotTakerIntegrationTest}.
|
23 | 45 | */
|
24 |
| -@RunWith(MockitoJUnitRunner.class) |
| 46 | +@RunWith(Enclosed.class) |
25 | 47 | public class ScreenshotTakerTest {
|
26 | 48 |
|
27 |
| - @Mock |
28 |
| - WebDriver webDriver; |
| 49 | + @RunWith(MockitoJUnitRunner.class) |
| 50 | + public static abstract class AbstractScreenshotTakerTest { |
| 51 | + |
| 52 | + static final String FILE_NAME_PATTERN = "\\d{4}-\\d{2}-\\d{2}T\\d{2}_\\d{2}_\\d{2}(\\.\\d{3})?.png"; |
| 53 | + static final String FILE_NAME = "my-file"; |
| 54 | + static final byte[] BYTES = new byte[] { 1, 2, 3 }; |
| 55 | + |
| 56 | + @ClassRule |
| 57 | + public static TemporaryFolder folder = new TemporaryFolder(); |
| 58 | + |
| 59 | + @Mock(extraInterfaces = WebDriver.class) |
| 60 | + TakesScreenshot webDriver; |
| 61 | + @Mock |
| 62 | + Configuration configuration; |
| 63 | + @Mock |
| 64 | + EventSystem events; |
| 65 | + @Mock |
| 66 | + Browser browser; |
| 67 | + @InjectMocks |
| 68 | + ScreenshotTaker cut; |
| 69 | + |
| 70 | + File currentFolder; |
| 71 | + File takenScreenshotFile; |
| 72 | + |
| 73 | + @Before |
| 74 | + public void init() throws IOException { |
| 75 | + |
| 76 | + doReturn(webDriver).when(browser).webDriver(); |
| 77 | + doReturn(configuration).when(browser).configuration(); |
| 78 | + doReturn(events).when(browser).events(); |
| 79 | + |
| 80 | + currentFolder = folder.newFolder(); |
| 81 | + doReturn(currentFolder).when(configuration).getScreenshotFolder(); |
| 82 | + takenScreenshotFile = folder.newFile(); |
| 83 | + doReturn(takenScreenshotFile).when(webDriver).getScreenshotAs(OutputType.FILE); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + static Supplier<AssertionError> assertionException() { |
| 88 | + return () -> new AssertionError("not save - see log"); |
| 89 | + } |
| 90 | + |
| 91 | + void assertFileParent(File file) { |
| 92 | + assertThat(file).hasParent(currentFolder); |
| 93 | + } |
| 94 | + |
| 95 | + void assertFileNamePattern(File file) { |
| 96 | + assertThat(file.getName()).matches(FILE_NAME_PATTERN); |
| 97 | + } |
| 98 | + |
| 99 | + void assertCustomFileName(File file) { |
| 100 | + assertThat(file.getName()).matches(FILE_NAME + ".png"); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + public static class Take extends AbstractScreenshotTakerTest { |
| 106 | + |
| 107 | + @Test |
| 108 | + public void screenshotCanBeTaken() { |
| 109 | + doReturn(BYTES).when(webDriver).getScreenshotAs(OutputType.BYTES); |
| 110 | + List<Byte> actualBytes = cut.take().orElseThrow(assertionException()); |
| 111 | + assertThat(actualBytes).isEqualTo(Bytes.asList(BYTES)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void inCaseOfRuntimeExceptionAnEmptyOptionalIsReturned() { |
| 116 | + doThrow(RuntimeException.class).when(webDriver).getScreenshotAs(OutputType.BYTES); |
| 117 | + Optional<List<Byte>> bytes = cut.take(); |
| 118 | + assertThat(bytes).isEmpty(); |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + public static class DefaultTakeAndStore extends AbstractScreenshotTakerTest { |
| 124 | + |
| 125 | + @Test |
| 126 | + public void screenshotCanBeTakenWithDefaults() { |
| 127 | + File file = cut.takeAndStore().orElseThrow(assertionException()); |
| 128 | + assertFileParent(file); |
| 129 | + assertFileNamePattern(file); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + public static class TakeAndStoreWithCustomTarget extends AbstractScreenshotTakerTest { |
| 135 | + |
| 136 | + @Test |
| 137 | + public void canHandleStringReference() { |
| 138 | + File file = cut.takeAndStore(currentFolder.getAbsolutePath()).orElseThrow(assertionException()); |
| 139 | + assertFileParent(file); |
| 140 | + assertFileNamePattern(file); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void canHandlePathReference() { |
| 145 | + File file = cut.takeAndStore(currentFolder.toPath()).orElseThrow(assertionException()); |
| 146 | + assertFileParent(file); |
| 147 | + assertFileNamePattern(file); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void canHandleFileReference() { |
| 152 | + File file = cut.takeAndStore(currentFolder).orElseThrow(assertionException()); |
| 153 | + assertFileParent(file); |
| 154 | + assertFileNamePattern(file); |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + public static class TakeAndStoreWithCustomTargetAndFileName extends AbstractScreenshotTakerTest { |
| 160 | + |
| 161 | + @Test |
| 162 | + public void canHandleStringReference() { |
| 163 | + File file = cut.takeAndStore(currentFolder.getAbsolutePath(), FILE_NAME).orElseThrow(assertionException()); |
| 164 | + assertFileParent(file); |
| 165 | + assertCustomFileName(file); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void canHandlePathReference() { |
| 170 | + File file = cut.takeAndStore(currentFolder.toPath(), FILE_NAME).orElseThrow(assertionException()); |
| 171 | + assertFileParent(file); |
| 172 | + assertCustomFileName(file); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void canHandleFileReference() { |
| 177 | + File file = cut.takeAndStore(currentFolder, FILE_NAME).orElseThrow(assertionException()); |
| 178 | + assertFileParent(file); |
| 179 | + assertCustomFileName(file); |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + public static class Events extends AbstractScreenshotTakerTest { |
| 185 | + |
| 186 | + @Captor |
| 187 | + ArgumentCaptor<TookScreenshotEvent> eventCaptor; |
| 188 | + |
| 189 | + @Test |
| 190 | + public void eventIsFiredWhenSaving() { |
| 191 | + doReturn(true).when(events).isEnabled(); |
| 192 | + File file = cut.takeAndStore().orElseThrow(assertionException()); |
| 193 | + verify(events).fireEvent(eventCaptor.capture()); |
| 194 | + assertThat(eventCaptor.getValue().getScreenshot()).isEqualTo(file); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void noEventsAreFiredIfEventSystemIsDisabled() { |
| 199 | + doReturn(false).when(events).isEnabled(); |
| 200 | + cut.takeAndStore(); |
| 201 | + verify(events, never()).fireEvent(any(Event.class)); |
| 202 | + } |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | + public static class Exceptions extends AbstractScreenshotTakerTest { |
| 207 | + |
| 208 | + @Test |
| 209 | + public void inCaseOfRuntimeExceptionAnEmptyOptionalIsReturned() { |
| 210 | + doThrow(RuntimeException.class).when(webDriver).getScreenshotAs(OutputType.FILE); |
| 211 | + Optional<File> file = cut.takeAndStore(); |
| 212 | + assertThat(file).isEmpty(); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void inCaseOfIOExceptionAnEmptyOptionalIsReturned() { |
| 217 | + doThrow(RuntimeException.class).when(webDriver).getScreenshotAs(OutputType.FILE); |
| 218 | + Optional<File> file = cut.takeAndStore(); |
| 219 | + assertThat(file).isEmpty(); |
| 220 | + } |
| 221 | + |
| 222 | + @Test(expected = Exception.class) |
| 223 | + public void otherExceptionsAreNotHandled() { |
| 224 | + doThrow(Exception.class).when(webDriver).getScreenshotAs(OutputType.FILE); |
| 225 | + cut.takeAndStore(); |
| 226 | + } |
| 227 | + |
| 228 | + } |
| 229 | + |
| 230 | + @RunWith(MockitoJUnitRunner.class) |
| 231 | + public static class NonScreenshotTakingWebDriver { |
29 | 232 |
|
30 |
| - @Test |
31 |
| - public void takingScreenshotWithUnsupportedWebDriverReturnsEmptyOptional() { |
| 233 | + @ClassRule |
| 234 | + public static TemporaryFolder folder = new TemporaryFolder(); |
32 | 235 |
|
33 |
| - Browser browser = WebDriverBrowser.buildForWebDriver(webDriver); |
34 |
| - ScreenshotTaker cut = new ScreenshotTaker(browser); |
| 236 | + @Mock |
| 237 | + WebDriver webDriver; |
| 238 | + @Mock |
| 239 | + Configuration configuration; |
| 240 | + @Mock |
| 241 | + Browser browser; |
| 242 | + @InjectMocks |
| 243 | + ScreenshotTaker cut; |
35 | 244 |
|
36 |
| - Optional<File> file = cut.takeAndStore(); |
37 |
| - assertThat(file).isEmpty(); |
| 245 | + @Test |
| 246 | + public void takingScreenshotWithUnsupportedWebDriverReturnsEmptyOptional() throws IOException { |
| 247 | + doReturn(webDriver).when(browser).webDriver(); |
| 248 | + doReturn(configuration).when(browser).configuration(); |
| 249 | + doReturn(folder.newFolder()).when(configuration).getScreenshotFolder(); |
| 250 | + Optional<File> file = cut.takeAndStore(); |
| 251 | + assertThat(file).isEmpty(); |
| 252 | + } |
38 | 253 |
|
39 | 254 | }
|
40 | 255 |
|
|
0 commit comments