|
12 | 12 |
|
13 | 13 | import org.junit.Before;
|
14 | 14 | import org.junit.Test;
|
| 15 | +import org.junit.experimental.runners.Enclosed; |
15 | 16 | import org.junit.runner.RunWith;
|
16 | 17 | import org.mockito.Mock;
|
17 | 18 | import org.mockito.runners.MockitoJUnitRunner;
|
|
34 | 35 | import info.novatec.testit.webtester.pagefragments.PageFragment;
|
35 | 36 |
|
36 | 37 |
|
37 |
| -@RunWith(MockitoJUnitRunner.class) |
| 38 | +@RunWith(Enclosed.class) |
38 | 39 | public class FocusSetterTest {
|
39 | 40 |
|
40 |
| - private static final int INDEX = 42; |
41 |
| - private static final String NAME_OR_ID = "fooBar"; |
42 |
| - private static final String NAME_OR_HANDLE = NAME_OR_ID; |
43 |
| - |
44 |
| - @Mock |
45 |
| - Configuration configuration; |
46 |
| - @Mock |
47 |
| - WebDriver webDriver; |
48 |
| - @Mock |
49 |
| - TargetLocator targetLocator; |
50 |
| - |
51 |
| - Browser browser; |
52 |
| - EventSystem eventSystem; |
53 |
| - FocusSetter cut; |
54 |
| - |
55 |
| - @Before |
56 |
| - public void init() throws IOException { |
57 |
| - doReturn(targetLocator).when(webDriver).switchTo(); |
58 |
| - doReturn(true).when(configuration).isEventSystemEnabled(); |
59 |
| - browser = WebDriverBrowser.forWebDriver(webDriver).withConfiguration(configuration).build(); |
60 |
| - eventSystem = browser.events(); |
61 |
| - cut = new FocusSetter(browser); |
62 |
| - } |
63 |
| - |
64 |
| - /* focusing on frame by index */ |
65 |
| - |
66 |
| - @Test |
67 |
| - public void switchingToFrameByIndexInvokesCorrectLocatorMethod() { |
68 |
| - executeOnFrameByIndex(); |
69 |
| - verify(targetLocator).frame(INDEX); |
70 |
| - } |
71 |
| - |
72 |
| - @Test |
73 |
| - public void switchingToFrameByIndexFiresEvent() { |
74 |
| - EventCaptor.capture(eventSystem, SwitchedToFrameEvent.class) |
75 |
| - .execute(this::executeOnFrameByIndex) |
76 |
| - .assertEventWasFired() |
77 |
| - .assertEvent(event -> assertThat(event.getTarget()).isEqualTo(String.valueOf(INDEX))); |
78 |
| - } |
79 |
| - |
80 |
| - @Test |
81 |
| - public void switchingToFrameByUnknownIndexThrowsException() { |
82 |
| - doThrow(mock(NoSuchFrameException.class)).when(targetLocator).frame(anyInt()); |
83 |
| - ExceptionEventCaptor.capture(eventSystem, NoSuchFrameException.class) |
84 |
| - .execute(this::executeOnFrameByIndex) |
85 |
| - .assertExceptionWasThrown() |
86 |
| - .assertExceptionEventWasFired(); |
87 |
| - } |
| 41 | + @RunWith(MockitoJUnitRunner.class) |
| 42 | + public static abstract class AbstractFocusSetterTest { |
| 43 | + |
| 44 | + static final int INDEX = 42; |
| 45 | + static final String NAME_OR_ID = "fooBar"; |
| 46 | + static final String NAME_OR_HANDLE = NAME_OR_ID; |
88 | 47 |
|
89 |
| - /* focusing on frame by name or id */ |
90 |
| - |
91 |
| - @Test |
92 |
| - public void switchingToFrameByNameOrIdInvokesCorrectLocatorMethod() { |
93 |
| - executeOnFrameByNameOrId(); |
94 |
| - verify(targetLocator).frame(NAME_OR_ID); |
95 |
| - } |
96 |
| - |
97 |
| - @Test |
98 |
| - public void switchingToFrameByNameOrIdFiresEvent() { |
99 |
| - EventCaptor.capture(eventSystem, SwitchedToFrameEvent.class) |
100 |
| - .execute(this::executeOnFrameByNameOrId) |
101 |
| - .assertEventWasFired() |
102 |
| - .assertEvent(event -> assertThat(event.getTarget()).isEqualTo(NAME_OR_ID)); |
103 |
| - } |
104 |
| - |
105 |
| - @Test |
106 |
| - public void switchingToFrameByUnknownNameOrIdThrowsException() { |
107 |
| - doThrow(mock(NoSuchFrameException.class)).when(targetLocator).frame(anyString()); |
108 |
| - ExceptionEventCaptor.capture(eventSystem, NoSuchFrameException.class) |
109 |
| - .execute(this::executeOnFrameByNameOrId) |
110 |
| - .assertExceptionWasThrown() |
111 |
| - .assertExceptionEventWasFired(); |
112 |
| - } |
113 |
| - |
114 |
| - /* focusing on frame by page fragment */ |
115 |
| - |
116 |
| - @Test |
117 |
| - public void switchingToFrameByPageFragmentInvokesCorrectLocatorMethod() { |
118 |
| - PageFragment fragment = MockFactory.fragment().build(); |
119 |
| - cut.onFrame(fragment); |
120 |
| - verify(targetLocator).frame(fragment.webElement()); |
121 |
| - } |
122 |
| - |
123 |
| - @Test |
124 |
| - public void switchingToFrameByPageFragmentFiresEvent() { |
125 |
| - PageFragment fragment = MockFactory.fragment().withName("The Name").build(); |
126 |
| - EventCaptor.capture(eventSystem, SwitchedToFrameEvent.class) |
127 |
| - .execute(() -> cut.onFrame(fragment)) |
128 |
| - .assertEventWasFired() |
129 |
| - .assertEvent(event -> assertThat(event.getTarget()).isEqualTo("The Name")); |
130 |
| - } |
| 48 | + @Mock |
| 49 | + Configuration configuration; |
| 50 | + @Mock |
| 51 | + WebDriver webDriver; |
| 52 | + @Mock |
| 53 | + TargetLocator targetLocator; |
131 | 54 |
|
132 |
| - /* focusing on window by name or handle */ |
133 |
| - |
134 |
| - @Test |
135 |
| - public void switchingToWindowByNameOrHandleInvokesCorrectLocatorMethod() { |
136 |
| - executeOnWindowByNameOrHandle(); |
137 |
| - verify(targetLocator).window(NAME_OR_HANDLE); |
138 |
| - } |
139 |
| - |
140 |
| - @Test |
141 |
| - public void switchingToWindowByNameOrHandleFiresEvent() { |
142 |
| - EventCaptor.capture(eventSystem, SwitchedToWindowEvent.class) |
143 |
| - .execute(this::executeOnWindowByNameOrHandle) |
144 |
| - .assertEventWasFired() |
145 |
| - .assertEvent(event -> assertThat(event.getNameOrHandle()).isEqualTo(NAME_OR_HANDLE)); |
146 |
| - } |
147 |
| - |
148 |
| - @Test |
149 |
| - public void switchingToWindowByUnknownNameOrHandleThrowsException() { |
150 |
| - doThrow(mock(NoSuchWindowException.class)).when(targetLocator).window(anyString()); |
151 |
| - ExceptionEventCaptor.capture(eventSystem, NoSuchWindowException.class) |
152 |
| - .execute(this::executeOnWindowByNameOrHandle) |
153 |
| - .assertExceptionWasThrown() |
154 |
| - .assertExceptionEventWasFired(); |
155 |
| - } |
156 |
| - |
157 |
| - /* focusing on default content */ |
158 |
| - |
159 |
| - @Test |
160 |
| - public void switchingToDefaultContentInvokesCorrectLocatorMethod() { |
161 |
| - executeOnDefaultContent(); |
162 |
| - verify(targetLocator).defaultContent(); |
163 |
| - } |
164 |
| - |
165 |
| - @Test |
166 |
| - public void switchingToDefaultContentFiresEvent() { |
167 |
| - EventCaptor.capture(eventSystem, SwitchedToDefaultContentEvent.class) |
168 |
| - .execute(this::executeOnDefaultContent) |
169 |
| - .assertEventWasFired(); |
170 |
| - } |
171 |
| - |
172 |
| - /* method execution */ |
173 |
| - |
174 |
| - void executeOnFrameByIndex() { |
175 |
| - cut.onFrame(INDEX); |
176 |
| - } |
177 |
| - |
178 |
| - void executeOnFrameByNameOrId() { |
179 |
| - cut.onFrame(NAME_OR_ID); |
180 |
| - } |
181 |
| - |
182 |
| - void executeOnWindowByNameOrHandle() { |
183 |
| - cut.onWindow(NAME_OR_HANDLE); |
184 |
| - } |
| 55 | + Browser browser; |
| 56 | + EventSystem eventSystem; |
| 57 | + |
| 58 | + FocusSetter cut; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void init() throws IOException { |
| 62 | + doReturn(targetLocator).when(webDriver).switchTo(); |
| 63 | + doReturn(true).when(configuration).isEventSystemEnabled(); |
| 64 | + browser = WebDriverBrowser.forWebDriver(webDriver).withConfiguration(configuration).build(); |
| 65 | + eventSystem = browser.events(); |
| 66 | + cut = new FocusSetter(browser); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public static class FocusOnFrameByIndex extends AbstractFocusSetterTest { |
| 72 | + |
| 73 | + @Test |
| 74 | + public void switchingToFrameByIndexInvokesCorrectLocatorMethod() { |
| 75 | + cut.onFrame(INDEX); |
| 76 | + verify(targetLocator).frame(INDEX); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void switchingToFrameByIndexFiresEvent() { |
| 81 | + EventCaptor.capture(eventSystem, SwitchedToFrameEvent.class) |
| 82 | + .execute(() -> cut.onFrame(INDEX)) |
| 83 | + .assertEventWasFired() |
| 84 | + .assertEvent(event -> assertThat(event.getTarget()).isEqualTo(String.valueOf(INDEX))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void switchingToFrameByUnknownIndexThrowsException() { |
| 89 | + doThrow(mock(NoSuchFrameException.class)).when(targetLocator).frame(anyInt()); |
| 90 | + ExceptionEventCaptor.capture(eventSystem, NoSuchFrameException.class) |
| 91 | + .execute(() -> cut.onFrame(INDEX)) |
| 92 | + .assertExceptionWasThrown() |
| 93 | + .assertExceptionEventWasFired(); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + public static class FocusOnFrameByNameOrId extends AbstractFocusSetterTest { |
| 99 | + |
| 100 | + @Test |
| 101 | + public void switchingToFrameByNameOrIdInvokesCorrectLocatorMethod() { |
| 102 | + cut.onFrame(NAME_OR_ID); |
| 103 | + verify(targetLocator).frame(NAME_OR_ID); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void switchingToFrameByNameOrIdFiresEvent() { |
| 108 | + EventCaptor.capture(eventSystem, SwitchedToFrameEvent.class) |
| 109 | + .execute(() -> cut.onFrame(NAME_OR_ID)) |
| 110 | + .assertEventWasFired() |
| 111 | + .assertEvent(event -> assertThat(event.getTarget()).isEqualTo(NAME_OR_ID)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void switchingToFrameByUnknownNameOrIdThrowsException() { |
| 116 | + doThrow(mock(NoSuchFrameException.class)).when(targetLocator).frame(anyString()); |
| 117 | + ExceptionEventCaptor.capture(eventSystem, NoSuchFrameException.class) |
| 118 | + .execute(() -> cut.onFrame(NAME_OR_ID)) |
| 119 | + .assertExceptionWasThrown() |
| 120 | + .assertExceptionEventWasFired(); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + public static class FocusOnFrameByPageFragment extends AbstractFocusSetterTest { |
| 126 | + |
| 127 | + @Test |
| 128 | + public void switchingToFrameByPageFragmentInvokesCorrectLocatorMethod() { |
| 129 | + PageFragment fragment = MockFactory.fragment().build(); |
| 130 | + cut.onFrame(fragment); |
| 131 | + verify(targetLocator).frame(fragment.webElement()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void switchingToFrameByPageFragmentFiresEvent() { |
| 136 | + PageFragment fragment = MockFactory.fragment().withName("The Name").build(); |
| 137 | + EventCaptor.capture(eventSystem, SwitchedToFrameEvent.class) |
| 138 | + .execute(() -> cut.onFrame(fragment)) |
| 139 | + .assertEventWasFired() |
| 140 | + .assertEvent(event -> assertThat(event.getTarget()).isEqualTo("The Name")); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + public static class FocusOnWindowByNameOrHandle extends AbstractFocusSetterTest { |
| 146 | + |
| 147 | + @Test |
| 148 | + public void switchingToWindowByNameOrHandleInvokesCorrectLocatorMethod() { |
| 149 | + cut.onWindow(NAME_OR_HANDLE); |
| 150 | + verify(targetLocator).window(NAME_OR_HANDLE); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void switchingToWindowByNameOrHandleFiresEvent() { |
| 155 | + EventCaptor.capture(eventSystem, SwitchedToWindowEvent.class) |
| 156 | + .execute(() -> cut.onWindow(NAME_OR_HANDLE)) |
| 157 | + .assertEventWasFired() |
| 158 | + .assertEvent(event -> assertThat(event.getNameOrHandle()).isEqualTo(NAME_OR_HANDLE)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void switchingToWindowByUnknownNameOrHandleThrowsException() { |
| 163 | + doThrow(mock(NoSuchWindowException.class)).when(targetLocator).window(anyString()); |
| 164 | + ExceptionEventCaptor.capture(eventSystem, NoSuchWindowException.class) |
| 165 | + .execute(() -> cut.onWindow(NAME_OR_HANDLE)) |
| 166 | + .assertExceptionWasThrown() |
| 167 | + .assertExceptionEventWasFired(); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + public static class FocusOnDefaultContent extends AbstractFocusSetterTest { |
| 173 | + |
| 174 | + @Test |
| 175 | + public void switchingToDefaultContentInvokesCorrectLocatorMethod() { |
| 176 | + cut.onDefaultContent(); |
| 177 | + verify(targetLocator).defaultContent(); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void switchingToDefaultContentFiresEvent() { |
| 182 | + EventCaptor.capture(eventSystem, SwitchedToDefaultContentEvent.class) |
| 183 | + .execute(() -> cut.onDefaultContent()) |
| 184 | + .assertEventWasFired(); |
| 185 | + } |
185 | 186 |
|
186 |
| - void executeOnDefaultContent() { |
187 |
| - cut.onDefaultContent(); |
188 | 187 | }
|
189 | 188 |
|
190 | 189 | }
|
0 commit comments