Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 12fc8cb

Browse files
committed
Added unit tests for PageSourceSaver
1 parent 93bb73b commit 12fc8cb

File tree

2 files changed

+230
-1
lines changed

2 files changed

+230
-1
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/browser/operations/PageSourceSaver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public Optional<File> save(File targetFolder, String fileNameWithoutSuffix) {
183183
} catch (IOException | RuntimeException e) {
184184
log.warn("Exception while saving page source, returning null.", e);
185185
}
186-
return Optional.ofNullable(file).filter(File::isFile);
186+
return Optional.ofNullable(file);
187187
}
188188

189189
private File doSavePageSource(File targetFolder, String fileNameWithoutSuffix) throws IOException {
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
package info.novatec.testit.webtester.browser.operations;
2+
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;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.util.Optional;
13+
import java.util.function.Supplier;
14+
15+
import org.junit.Before;
16+
import org.junit.ClassRule;
17+
import org.junit.Test;
18+
import org.junit.experimental.runners.Enclosed;
19+
import org.junit.rules.TemporaryFolder;
20+
import org.junit.runner.RunWith;
21+
import org.mockito.ArgumentCaptor;
22+
import org.mockito.Captor;
23+
import org.mockito.InjectMocks;
24+
import org.mockito.Mock;
25+
import org.mockito.runners.MockitoJUnitRunner;
26+
import org.openqa.selenium.WebDriver;
27+
28+
import info.novatec.testit.webtester.browser.Browser;
29+
import info.novatec.testit.webtester.config.Configuration;
30+
import info.novatec.testit.webtester.events.Event;
31+
import info.novatec.testit.webtester.events.EventSystem;
32+
import info.novatec.testit.webtester.events.browser.SavedSourceCodeEvent;
33+
34+
35+
@RunWith(Enclosed.class)
36+
public class PageSourceSaverTest {
37+
38+
@RunWith(MockitoJUnitRunner.class)
39+
public static abstract class AbstractPageSourceSaverTest {
40+
41+
static final String FILE_NAME_PATTERN = "\\d{4}-\\d{2}-\\d{2}T\\d{2}_\\d{2}_\\d{2}(\\.\\d{3})?.html";
42+
static final String PAGE_SOURCE = "<html>...</html>";
43+
static final String FILE_NAME = "my-file";
44+
45+
@ClassRule
46+
public static TemporaryFolder folder = new TemporaryFolder();
47+
48+
@Mock
49+
WebDriver webDriver;
50+
@Mock
51+
Configuration configuration;
52+
@Mock
53+
EventSystem events;
54+
@Mock
55+
Browser browser;
56+
@InjectMocks
57+
PageSourceSaver cut;
58+
59+
File currentFolder;
60+
61+
@Before
62+
public void init() throws IOException {
63+
64+
doReturn(webDriver).when(browser).webDriver();
65+
doReturn(configuration).when(browser).configuration();
66+
doReturn(events).when(browser).events();
67+
68+
currentFolder = folder.newFolder();
69+
doReturn(currentFolder).when(configuration).getPageSourceFolder();
70+
71+
doReturn(PAGE_SOURCE).when(webDriver).getPageSource();
72+
73+
}
74+
75+
static Supplier<AssertionError> notSavedException() {
76+
return () -> new AssertionError("not save - see log");
77+
}
78+
79+
void assertFileParent(File file) {
80+
assertThat(file).hasParent(currentFolder);
81+
}
82+
83+
void assertFileContent(File file) {
84+
assertThat(file).hasContent(PAGE_SOURCE);
85+
}
86+
87+
void assertFileNamePattern(File file) {
88+
assertThat(file.getName()).matches(FILE_NAME_PATTERN);
89+
}
90+
91+
void assertCustomFileName(File file) {
92+
assertThat(file.getName()).matches(FILE_NAME + ".html");
93+
}
94+
95+
}
96+
97+
public static class Get extends AbstractPageSourceSaverTest {
98+
99+
@Test
100+
public void pageSourceCanBeGot() {
101+
doReturn(PAGE_SOURCE).when(webDriver).getPageSource();
102+
String source = cut.get();
103+
assertThat(source).isEqualTo(PAGE_SOURCE);
104+
}
105+
106+
@Test
107+
public void pageSourceIsDefaultedToEmptyStringIfNull() {
108+
doReturn(null).when(webDriver).getPageSource();
109+
String source = cut.get();
110+
assertThat(source).isEqualTo("");
111+
}
112+
113+
}
114+
115+
public static class DefaultSave extends AbstractPageSourceSaverTest {
116+
117+
@Test
118+
public void pageSourceCanBeSavedWithDefaults() {
119+
File file = cut.save().orElseThrow(notSavedException());
120+
assertFileParent(file);
121+
assertFileContent(file);
122+
assertFileNamePattern(file);
123+
}
124+
125+
}
126+
127+
public static class SaveWithCustomTarget extends AbstractPageSourceSaverTest {
128+
129+
@Test
130+
public void canHandleStringReference() {
131+
File file = cut.save(currentFolder.getAbsolutePath()).orElseThrow(notSavedException());
132+
assertFileParent(file);
133+
assertFileContent(file);
134+
assertFileNamePattern(file);
135+
}
136+
137+
@Test
138+
public void canHandlePathReference() {
139+
File file = cut.save(currentFolder.toPath()).orElseThrow(notSavedException());
140+
assertFileParent(file);
141+
assertFileContent(file);
142+
assertFileNamePattern(file);
143+
}
144+
145+
@Test
146+
public void canHandleFileReference() {
147+
File file = cut.save(currentFolder).orElseThrow(notSavedException());
148+
assertFileParent(file);
149+
assertFileContent(file);
150+
assertFileNamePattern(file);
151+
}
152+
153+
}
154+
155+
public static class SaveWithCustomTargetAndFileName extends AbstractPageSourceSaverTest {
156+
157+
@Test
158+
public void canHandleStringReference() {
159+
File file = cut.save(currentFolder.getAbsolutePath(), FILE_NAME).orElseThrow(notSavedException());
160+
assertFileParent(file);
161+
assertFileContent(file);
162+
assertCustomFileName(file);
163+
}
164+
165+
@Test
166+
public void canHandlePathReference() {
167+
File file = cut.save(currentFolder.toPath(), FILE_NAME).orElseThrow(notSavedException());
168+
assertFileParent(file);
169+
assertFileContent(file);
170+
assertCustomFileName(file);
171+
}
172+
173+
@Test
174+
public void canHandleFileReference() {
175+
File file = cut.save(currentFolder, FILE_NAME).orElseThrow(notSavedException());
176+
assertFileParent(file);
177+
assertFileContent(file);
178+
assertCustomFileName(file);
179+
}
180+
181+
}
182+
183+
public static class Events extends AbstractPageSourceSaverTest {
184+
185+
@Captor
186+
ArgumentCaptor<SavedSourceCodeEvent> eventCaptor;
187+
188+
@Test
189+
public void eventIsFiredWhenSaving() {
190+
doReturn(true).when(events).isEnabled();
191+
File file = cut.save().orElseThrow(notSavedException());
192+
verify(events).fireEvent(eventCaptor.capture());
193+
assertThat(eventCaptor.getValue().getPageSource()).isEqualTo(file);
194+
}
195+
196+
@Test
197+
public void noEventsAreFiredIfEventSystemIsDisabled() {
198+
doReturn(false).when(events).isEnabled();
199+
cut.save();
200+
verify(events, never()).fireEvent(any(Event.class));
201+
}
202+
203+
}
204+
205+
public static class Exceptions extends AbstractPageSourceSaverTest {
206+
207+
@Test
208+
public void inCaseOfRuntimeExceptionAnEmptyOptionalIsReturned() {
209+
doThrow(RuntimeException.class).when(webDriver).getPageSource();
210+
Optional<File> file = cut.save();
211+
assertThat(file).isEmpty();
212+
}
213+
214+
@Test
215+
public void inCaseOfIOExceptionAnEmptyOptionalIsReturned() {
216+
doThrow(RuntimeException.class).when(webDriver).getPageSource();
217+
Optional<File> file = cut.save();
218+
assertThat(file).isEmpty();
219+
}
220+
221+
@Test(expected = Exception.class)
222+
public void otherExceptionsAreNotHandled() {
223+
doThrow(Exception.class).when(webDriver).getPageSource();
224+
cut.save();
225+
}
226+
227+
}
228+
229+
}

0 commit comments

Comments
 (0)