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

Commit 4011a90

Browse files
committed
Extended AlertHandler and reworked corresponding tests
1 parent b2ae974 commit 4011a90

File tree

2 files changed

+179
-100
lines changed

2 files changed

+179
-100
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package info.novatec.testit.webtester.browser.operations;
22

3+
import java.util.Optional;
4+
35
import org.openqa.selenium.Alert;
46
import org.openqa.selenium.Beta;
57
import org.openqa.selenium.NoAlertPresentException;
@@ -149,11 +151,22 @@ public void decline() throws NoAlertPresentException {
149151
* @since 2.0
150152
*/
151153
public boolean isPresent() {
154+
return get().isPresent();
155+
}
156+
157+
/**
158+
* Returns the Selenium {@link Alert} instance as an optional.
159+
*
160+
* @return optional of the alert - if no alert is present the optional will be empty.
161+
* @see Alert
162+
* @see WebDriver.TargetLocator#alert()
163+
* @since 2.0
164+
*/
165+
public Optional<Alert> get() {
152166
try {
153-
webDriver().switchTo().alert();
154-
return true;
167+
return Optional.of(webDriver().switchTo().alert());
155168
} catch (NoAlertPresentException e) {
156-
return false;
169+
return Optional.empty();
157170
}
158171
}
159172

Lines changed: 163 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package info.novatec.testit.webtester.browser.operations;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.mockito.Mockito.doReturn;
5-
import static org.mockito.Mockito.doThrow;
64
import static org.mockito.Mockito.mock;
75
import static org.mockito.Mockito.verify;
6+
import static org.mockito.Mockito.when;
87

98
import org.junit.Before;
109
import org.junit.Test;
10+
import org.junit.experimental.runners.Enclosed;
1111
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;
1216
import org.mockito.runners.MockitoJUnitRunner;
1317
import org.openqa.selenium.Alert;
1418
import org.openqa.selenium.NoAlertPresentException;
1519
import org.openqa.selenium.WebDriver;
20+
import org.openqa.selenium.security.Credentials;
21+
import org.openqa.selenium.security.UserAndPassword;
1622

17-
import utils.MockFactory;
1823
import utils.events.EventCaptor;
1924

2025
import info.novatec.testit.webtester.browser.Browser;
@@ -24,129 +29,190 @@
2429
import info.novatec.testit.webtester.events.browser.DeclinedAlertEvent;
2530

2631

27-
@RunWith(MockitoJUnitRunner.class)
32+
@RunWith(Enclosed.class)
2833
public class AlertHandlerTest {
2934

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 {
4237

43-
/* accept */
38+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
39+
WebDriver webDriver;
4440

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;
5144

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+
}
5751

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+
}
6658

67-
/* accept if present */
59+
void alertIsNotPresent() {
60+
NoAlertPresentException exception = new NoAlertPresentException();
61+
when(webDriver.switchTo().alert()).thenThrow(exception);
62+
}
6863

69-
@Test
70-
public void alertsCanBeAcceptedIfTheyArePresent() {
71-
Alert alert = alertIsPresent("hello alert!");
72-
cut.acceptIfPresent();
73-
verify(alert).accept();
7464
}
7565

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+
8190
}
8291

83-
/* decline */
92+
public static class AcceptIfPresent extends AbstractAlertHandlerTest {
8493

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+
}
91100

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+
}
97107

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!"));
105108
}
106109

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+
}
108133

109-
@Test
110-
public void alertsCanBeDeclinedIfTheyArePresent() {
111-
Alert alert = alertIsPresent("hello alert!");
112-
cut.declineIfPresent();
113-
verify(alert).dismiss();
114134
}
115135

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+
}
122144

123-
/* presence */
145+
@Test
146+
public void decliningAnAlertIfItsPresentDoesNotThrowExceptionIfNoAlertExists() {
147+
alertIsNotPresent();
148+
cut.declineIfPresent();
149+
// no exception
150+
}
124151

125-
@Test
126-
public void presenceOfNonExistentAlertReturnsFalse() {
127-
alertIsNotPresent();
128-
assertThat(cut.isPresent()).isFalse();
129152
}
130153

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+
135168
}
136169

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+
}
138183

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;
145184
}
146185

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+
150216
}
151217

152218
}

0 commit comments

Comments
 (0)