|
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.Mockito.doReturn; |
5 |
| -import static org.mockito.Mockito.doThrow; |
6 | 4 | import static org.mockito.Mockito.mock;
|
7 | 5 | import static org.mockito.Mockito.verify;
|
| 6 | +import static org.mockito.Mockito.when; |
8 | 7 |
|
9 | 8 | import org.junit.Before;
|
10 | 9 | import org.junit.Test;
|
| 10 | +import org.junit.experimental.runners.Enclosed; |
11 | 11 | import org.junit.runner.RunWith;
|
| 12 | +import org.mockito.Answers; |
| 13 | +import org.mockito.ArgumentCaptor; |
| 14 | +import org.mockito.Captor; |
| 15 | +import org.mockito.Mock; |
12 | 16 | import org.mockito.runners.MockitoJUnitRunner;
|
13 | 17 | import org.openqa.selenium.Alert;
|
14 | 18 | import org.openqa.selenium.NoAlertPresentException;
|
15 | 19 | import org.openqa.selenium.WebDriver;
|
| 20 | +import org.openqa.selenium.security.Credentials; |
| 21 | +import org.openqa.selenium.security.UserAndPassword; |
16 | 22 |
|
17 |
| -import utils.MockFactory; |
18 | 23 | import utils.events.EventCaptor;
|
19 | 24 |
|
20 | 25 | import info.novatec.testit.webtester.browser.Browser;
|
|
24 | 29 | import info.novatec.testit.webtester.events.browser.DeclinedAlertEvent;
|
25 | 30 |
|
26 | 31 |
|
27 |
| -@RunWith(MockitoJUnitRunner.class) |
| 32 | +@RunWith(Enclosed.class) |
28 | 33 | public class AlertHandlerTest {
|
29 | 34 |
|
30 |
| - WebDriver webDriver; |
31 |
| - Browser browser; |
32 |
| - EventSystem eventSystem; |
33 |
| - AlertHandler cut; |
34 |
| - |
35 |
| - @Before |
36 |
| - public void init() { |
37 |
| - webDriver = MockFactory.webDriver(); |
38 |
| - browser = WebDriverBrowser.forWebDriver(webDriver).build(); |
39 |
| - eventSystem = browser.events(); |
40 |
| - cut = new AlertHandler(browser); |
41 |
| - } |
| 35 | + @RunWith(MockitoJUnitRunner.class) |
| 36 | + static abstract class AbstractAlertHandlerTest { |
42 | 37 |
|
43 |
| - /* accept */ |
| 38 | + @Mock(answer = Answers.RETURNS_DEEP_STUBS) |
| 39 | + WebDriver webDriver; |
44 | 40 |
|
45 |
| - @Test |
46 |
| - public void alertsCanBeAccepted() { |
47 |
| - Alert alert = alertIsPresent("hello alert!"); |
48 |
| - cut.accept(); |
49 |
| - verify(alert).accept(); |
50 |
| - } |
| 41 | + Browser browser; |
| 42 | + EventSystem eventSystem; |
| 43 | + AlertHandler cut; |
51 | 44 |
|
52 |
| - @Test(expected = NoAlertPresentException.class) |
53 |
| - public void acceptingNonExistentAlertThrowsException() { |
54 |
| - alertIsNotPresent(); |
55 |
| - cut.accept(); |
56 |
| - } |
| 45 | + @Before |
| 46 | + public void initializeClassUnderTest() { |
| 47 | + browser = WebDriverBrowser.forWebDriver(webDriver).build(); |
| 48 | + eventSystem = browser.events(); |
| 49 | + cut = new AlertHandler(browser); |
| 50 | + } |
57 | 51 |
|
58 |
| - @Test |
59 |
| - public void acceptingAnAlertFiresEvent() { |
60 |
| - alertIsPresent("hello alert!"); |
61 |
| - EventCaptor.capture(eventSystem, AcceptedAlertEvent.class) |
62 |
| - .execute(() -> cut.accept()) |
63 |
| - .assertEventWasFired() |
64 |
| - .assertEvent(event -> assertThat(event.getAlertMessage()).isEqualTo("hello alert!")); |
65 |
| - } |
| 52 | + Alert alertIsPresent(String message) { |
| 53 | + Alert alert = mock(Alert.class); |
| 54 | + when(alert.getText()).thenReturn(message); |
| 55 | + when(webDriver.switchTo().alert()).thenReturn(alert); |
| 56 | + return alert; |
| 57 | + } |
66 | 58 |
|
67 |
| - /* accept if present */ |
| 59 | + void alertIsNotPresent() { |
| 60 | + NoAlertPresentException exception = new NoAlertPresentException(); |
| 61 | + when(webDriver.switchTo().alert()).thenThrow(exception); |
| 62 | + } |
68 | 63 |
|
69 |
| - @Test |
70 |
| - public void alertsCanBeAcceptedIfTheyArePresent() { |
71 |
| - Alert alert = alertIsPresent("hello alert!"); |
72 |
| - cut.acceptIfPresent(); |
73 |
| - verify(alert).accept(); |
74 | 64 | }
|
75 | 65 |
|
76 |
| - @Test |
77 |
| - public void acceptingAnAlertIfItsPresentDoesNotThrowExceptionIfNoAlertExists() { |
78 |
| - alertIsNotPresent(); |
79 |
| - cut.acceptIfPresent(); |
80 |
| - // no exception |
| 66 | + public static class Accept extends AbstractAlertHandlerTest { |
| 67 | + |
| 68 | + @Test |
| 69 | + public void alertsCanBeAccepted() { |
| 70 | + Alert alert = alertIsPresent("hello alert!"); |
| 71 | + cut.accept(); |
| 72 | + verify(alert).accept(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test(expected = NoAlertPresentException.class) |
| 76 | + public void acceptingNonExistentAlertThrowsException() { |
| 77 | + alertIsNotPresent(); |
| 78 | + cut.accept(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void acceptingAnAlertFiresEvent() { |
| 83 | + alertIsPresent("hello alert!"); |
| 84 | + EventCaptor.capture(eventSystem, AcceptedAlertEvent.class) |
| 85 | + .execute(() -> cut.accept()) |
| 86 | + .assertEventWasFired() |
| 87 | + .assertEvent(event -> assertThat(event.getAlertMessage()).isEqualTo("hello alert!")); |
| 88 | + } |
| 89 | + |
81 | 90 | }
|
82 | 91 |
|
83 |
| - /* decline */ |
| 92 | + public static class AcceptIfPresent extends AbstractAlertHandlerTest { |
84 | 93 |
|
85 |
| - @Test |
86 |
| - public void alertsCanBeDeclined() { |
87 |
| - Alert alert = alertIsPresent("hello alert!"); |
88 |
| - cut.decline(); |
89 |
| - verify(alert).dismiss(); |
90 |
| - } |
| 94 | + @Test |
| 95 | + public void alertsCanBeAcceptedIfTheyArePresent() { |
| 96 | + Alert alert = alertIsPresent("hello alert!"); |
| 97 | + cut.acceptIfPresent(); |
| 98 | + verify(alert).accept(); |
| 99 | + } |
91 | 100 |
|
92 |
| - @Test(expected = NoAlertPresentException.class) |
93 |
| - public void decliningNonExistentAlertThrowsException() { |
94 |
| - alertIsNotPresent(); |
95 |
| - cut.decline(); |
96 |
| - } |
| 101 | + @Test |
| 102 | + public void acceptingAnAlertIfItsPresentDoesNotThrowExceptionIfNoAlertExists() { |
| 103 | + alertIsNotPresent(); |
| 104 | + cut.acceptIfPresent(); |
| 105 | + // no exception |
| 106 | + } |
97 | 107 |
|
98 |
| - @Test |
99 |
| - public void decliningAnAlertFiresEvent() { |
100 |
| - alertIsPresent("hello alert!"); |
101 |
| - EventCaptor.capture(eventSystem, DeclinedAlertEvent.class) |
102 |
| - .execute(() -> cut.decline()) |
103 |
| - .assertEventWasFired() |
104 |
| - .assertEvent(event -> assertThat(event.getAlertMessage()).isEqualTo("hello alert!")); |
105 | 108 | }
|
106 | 109 |
|
107 |
| - /* decline if present */ |
| 110 | + public static class Decline extends AbstractAlertHandlerTest { |
| 111 | + |
| 112 | + @Test |
| 113 | + public void alertsCanBeDeclined() { |
| 114 | + Alert alert = alertIsPresent("hello alert!"); |
| 115 | + cut.decline(); |
| 116 | + verify(alert).dismiss(); |
| 117 | + } |
| 118 | + |
| 119 | + @Test(expected = NoAlertPresentException.class) |
| 120 | + public void decliningNonExistentAlertThrowsException() { |
| 121 | + alertIsNotPresent(); |
| 122 | + cut.decline(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void decliningAnAlertFiresEvent() { |
| 127 | + alertIsPresent("hello alert!"); |
| 128 | + EventCaptor.capture(eventSystem, DeclinedAlertEvent.class) |
| 129 | + .execute(() -> cut.decline()) |
| 130 | + .assertEventWasFired() |
| 131 | + .assertEvent(event -> assertThat(event.getAlertMessage()).isEqualTo("hello alert!")); |
| 132 | + } |
108 | 133 |
|
109 |
| - @Test |
110 |
| - public void alertsCanBeDeclinedIfTheyArePresent() { |
111 |
| - Alert alert = alertIsPresent("hello alert!"); |
112 |
| - cut.declineIfPresent(); |
113 |
| - verify(alert).dismiss(); |
114 | 134 | }
|
115 | 135 |
|
116 |
| - @Test |
117 |
| - public void decliningAnAlertIfItsPresentDoesNotThrowExceptionIfNoAlertExists() { |
118 |
| - alertIsNotPresent(); |
119 |
| - cut.declineIfPresent(); |
120 |
| - // no exception |
121 |
| - } |
| 136 | + public static class DeclineIfPresent extends AbstractAlertHandlerTest { |
| 137 | + |
| 138 | + @Test |
| 139 | + public void alertsCanBeDeclinedIfTheyArePresent() { |
| 140 | + Alert alert = alertIsPresent("hello alert!"); |
| 141 | + cut.declineIfPresent(); |
| 142 | + verify(alert).dismiss(); |
| 143 | + } |
122 | 144 |
|
123 |
| - /* presence */ |
| 145 | + @Test |
| 146 | + public void decliningAnAlertIfItsPresentDoesNotThrowExceptionIfNoAlertExists() { |
| 147 | + alertIsNotPresent(); |
| 148 | + cut.declineIfPresent(); |
| 149 | + // no exception |
| 150 | + } |
124 | 151 |
|
125 |
| - @Test |
126 |
| - public void presenceOfNonExistentAlertReturnsFalse() { |
127 |
| - alertIsNotPresent(); |
128 |
| - assertThat(cut.isPresent()).isFalse(); |
129 | 152 | }
|
130 | 153 |
|
131 |
| - @Test |
132 |
| - public void presenceOfExistentAlertReturnsTrue() { |
133 |
| - alertIsPresent("hello alert!"); |
134 |
| - assertThat(cut.isPresent()).isTrue(); |
| 154 | + public static class IsPresent extends AbstractAlertHandlerTest { |
| 155 | + |
| 156 | + @Test |
| 157 | + public void presenceOfNonExistentAlertReturnsFalse() { |
| 158 | + alertIsNotPresent(); |
| 159 | + assertThat(cut.isPresent()).isFalse(); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void presenceOfExistentAlertReturnsTrue() { |
| 164 | + alertIsPresent("hello alert!"); |
| 165 | + assertThat(cut.isPresent()).isTrue(); |
| 166 | + } |
| 167 | + |
135 | 168 | }
|
136 | 169 |
|
137 |
| - /* utilities */ |
| 170 | + public static class Get extends AbstractAlertHandlerTest { |
| 171 | + |
| 172 | + @Test |
| 173 | + public void gettingNonExistentAlertReturnsEmptyOptional() { |
| 174 | + alertIsNotPresent(); |
| 175 | + assertThat(cut.get()).isEmpty(); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void gettingExistentAlertReturnsItAsAnOptional() { |
| 180 | + alertIsPresent("hello alert!"); |
| 181 | + assertThat(cut.get()).isPresent(); |
| 182 | + } |
138 | 183 |
|
139 |
| - Alert alertIsPresent(String message) { |
140 |
| - WebDriver.TargetLocator targetLocator = webDriver.switchTo(); |
141 |
| - Alert alert = mock(Alert.class); |
142 |
| - doReturn(message).when(alert).getText(); |
143 |
| - doReturn(alert).when(targetLocator).alert(); |
144 |
| - return alert; |
145 | 184 | }
|
146 | 185 |
|
147 |
| - void alertIsNotPresent() { |
148 |
| - WebDriver.TargetLocator targetLocator = webDriver.switchTo(); |
149 |
| - doThrow(new NoAlertPresentException()).when(targetLocator).alert(); |
| 186 | + public static class AuthenticateWith extends AbstractAlertHandlerTest { |
| 187 | + |
| 188 | + @Captor |
| 189 | + ArgumentCaptor<Credentials> credentialsCaptor; |
| 190 | + |
| 191 | + @Test |
| 192 | + public void canAuthenticateWithCredentials() { |
| 193 | + |
| 194 | + Alert alert = alertIsPresent("please sign in"); |
| 195 | + Credentials credentials = new UserAndPassword("foo", "bar"); |
| 196 | + browser.alert().authenticateWith(credentials); |
| 197 | + |
| 198 | + verify(alert).authenticateUsing(credentials); |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + public void canAuthenticateWithUsernameAndPassword() { |
| 204 | + |
| 205 | + Alert alert = alertIsPresent("please sign in"); |
| 206 | + browser.alert().authenticateWith("foo", "bar"); |
| 207 | + verify(alert).authenticateUsing(credentialsCaptor.capture()); |
| 208 | + |
| 209 | + Credentials credentials = credentialsCaptor.getValue(); |
| 210 | + assertThat(credentials).isInstanceOf(UserAndPassword.class); |
| 211 | + assertThat((( UserAndPassword ) credentials).getUsername()).isEqualTo("foo"); |
| 212 | + assertThat((( UserAndPassword ) credentials).getPassword()).isEqualTo("bar"); |
| 213 | + |
| 214 | + } |
| 215 | + |
150 | 216 | }
|
151 | 217 |
|
152 | 218 | }
|
0 commit comments