blob: 9a7ecc65ab8952153699b9e5f5b042dc2c18a3ce [file] [log] [blame]
Philipp Schraderfa096932022-03-05 20:07:10 -08001import {browser, by, element, protractor} from 'protractor';
Philipp Schraderd999c9f2022-02-27 15:48:58 -08002
Philipp Schrader5f190012022-03-15 23:29:09 -07003const EC = protractor.ExpectedConditions;
4
Philipp Schrader577befe2022-03-15 00:00:49 -07005// Loads the page (or reloads it) and deals with the "Are you sure you want to
6// leave this page" popup.
7async function loadPage() {
Philipp Schradercf915462022-03-16 23:42:22 -07008 await disableAlerts();
9 await browser.navigate().refresh();
10 expect((await browser.getTitle())).toEqual('FRC971 Scouting Application');
Philipp Schrader5f190012022-03-15 23:29:09 -070011 await disableAlerts();
Philipp Schrader577befe2022-03-15 00:00:49 -070012}
13
Philipp Schradercf915462022-03-16 23:42:22 -070014// Disables alert popups. They are extremely tedious to deal with in
15// Protractor since they're not angular elements. We achieve this by checking
16// an invisible checkbox that's off-screen.
17async function disableAlerts() {
Ravago Jones2813c032022-03-16 23:44:11 -070018 await browser.executeAsyncScript(function(callback) {
19 let block_alerts =
20 document.getElementById('block_alerts') as HTMLInputElement;
Philipp Schradercf915462022-03-16 23:42:22 -070021 block_alerts.checked = true;
22 callback();
23 });
24}
Philipp Schraderfa096932022-03-05 20:07:10 -080025// Returns the contents of the header that displays the "Auto", "TeleOp", and
26// "Climb" labels etc.
27function getHeadingText() {
28 return element(by.css('.header')).getText();
29}
Philipp Schraderd999c9f2022-02-27 15:48:58 -080030
Philipp Schrader5f190012022-03-15 23:29:09 -070031// Returns the currently displayed progress message on the screen. This only
32// exists on screens where the web page interacts with the web server.
33function getProgressMessage() {
34 return element(by.css('.progress_message')).getText();
35}
36
Philipp Schraderfa096932022-03-05 20:07:10 -080037// Returns the currently displayed error message on the screen. This only
38// exists on screens where the web page interacts with the web server.
39function getErrorMessage() {
40 return element(by.css('.error_message')).getText();
41}
Philipp Schraderd999c9f2022-02-27 15:48:58 -080042
Philipp Schraderfa096932022-03-05 20:07:10 -080043// Asserts that the field on the "Submit and Review" screen has a specific
44// value.
45function expectReviewFieldToBe(fieldName: string, expectedValue: string) {
46 return expectNthReviewFieldToBe(fieldName, 0, expectedValue);
47}
48
49// Asserts that the n'th instance of a field on the "Submit and Review"
50// screen has a specific value.
Ravago Jones2813c032022-03-16 23:44:11 -070051async function expectNthReviewFieldToBe(
52 fieldName: string, n: number, expectedValue: string) {
53 expect(await element.all(by.cssContainingText('li', `${fieldName}:`))
54 .get(n)
55 .getText())
Philipp Schraderfa096932022-03-05 20:07:10 -080056 .toEqual(`${fieldName}: ${expectedValue}`);
Philipp Schraderd999c9f2022-02-27 15:48:58 -080057}
58
Philipp Schrader5f190012022-03-15 23:29:09 -070059// Sets a text field to the specified value.
60function setTextboxByIdTo(id: string, value: string) {
61 // Just sending "value" to the input fields is insufficient. We need to
62 // overwrite the text that is there. If we didn't hit CTRL-A to select all
63 // the text, we'd be appending to whatever is there already.
64 return element(by.id(id)).sendKeys(
Ravago Jones2813c032022-03-16 23:44:11 -070065 protractor.Key.CONTROL, 'a', protractor.Key.NULL, value);
Philipp Schrader5f190012022-03-15 23:29:09 -070066}
67
Philipp Schraderfa45d742022-03-18 19:29:05 -070068// Moves the nth slider left or right. A positive "adjustBy" value moves the
69// slider to the right. A negative value moves the slider to the left.
70//
71// negative/left <--- 0 ---> positive/right
72async function adjustNthSliderBy(n: number, adjustBy: number) {
Philipp Schrader5079fa12022-03-19 15:40:12 -070073 const slider = element.all(by.css('input[type=range]')).get(n);
74 const key =
75 adjustBy > 0 ? protractor.Key.ARROW_RIGHT : protractor.Key.ARROW_LEFT;
Philipp Schraderfa45d742022-03-18 19:29:05 -070076 for (let i = 0; i < Math.abs(adjustBy); i++) {
77 await slider.sendKeys(key);
78 }
79}
80
Philipp Schraderb99a8cc2022-03-18 21:00:02 -070081function getNthMatchLabel(n: number) {
82 return element.all(by.css('.badge')).get(n).getText();
83}
84
Philipp Schraderd999c9f2022-02-27 15:48:58 -080085describe('The scouting web page', () => {
Philipp Schradercf915462022-03-16 23:42:22 -070086 beforeAll(async () => {
87 await browser.get(browser.baseUrl);
88 expect((await browser.getTitle())).toEqual('FRC971 Scouting Application');
89 await disableAlerts();
Philipp Schrader5f190012022-03-15 23:29:09 -070090
91 // Import the match list before running any tests. Ideally this should be
92 // run in beforeEach(), but it's not worth doing that at this time. Our
93 // tests are basic enough not to require this.
Ravago Jones2813c032022-03-16 23:44:11 -070094 await element(by.cssContainingText('.nav-link', 'Import Match List'))
95 .click();
Philipp Schrader5f190012022-03-15 23:29:09 -070096 expect(await getHeadingText()).toEqual('Import Match List');
97 await setTextboxByIdTo('year', '2016');
98 await setTextboxByIdTo('event_code', 'nytr');
99 await element(by.buttonText('Import')).click();
100
101 await browser.wait(EC.textToBePresentInElement(
Ravago Jones2813c032022-03-16 23:44:11 -0700102 element(by.css('.progress_message')),
103 'Successfully imported match list.'));
Philipp Schradercf915462022-03-16 23:42:22 -0700104 });
105
Philipp Schraderb99a8cc2022-03-18 21:00:02 -0700106 it('should: show matches in chronological order.', async () => {
107 await loadPage();
108
Philipp Schrader5079fa12022-03-19 15:40:12 -0700109 expect(await getNthMatchLabel(0)).toEqual('Quals 1');
110 expect(await getNthMatchLabel(1)).toEqual('Quals 2');
111 expect(await getNthMatchLabel(2)).toEqual('Quals 3');
112 expect(await getNthMatchLabel(9)).toEqual('Quals 10');
Philipp Schraderb99a8cc2022-03-18 21:00:02 -0700113 // TODO(phil): Validate quarter finals and friends. Right now we don't
114 // distinguish between "sets". I.e. we display 4 "Quarter Final 1" matches
115 // without being able to distinguish between them.
Philipp Schrader5079fa12022-03-19 15:40:12 -0700116 expect(await getNthMatchLabel(87)).toEqual('Final 1');
Philipp Schraderb99a8cc2022-03-18 21:00:02 -0700117 });
118
Philipp Schrader5f190012022-03-15 23:29:09 -0700119 it('should: error on unknown match.', async () => {
120 await loadPage();
121
Ravago Jones2813c032022-03-16 23:44:11 -0700122 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
123
Philipp Schrader5f190012022-03-15 23:29:09 -0700124 // Pick a match that doesn't exist in the 2016nytr match list.
125 await setTextboxByIdTo('match_number', '3');
126 await setTextboxByIdTo('team_number', '971');
127
128 // Click Next until we get to the submit screen.
129 for (let i = 0; i < 5; i++) {
130 await element(by.buttonText('Next')).click();
131 }
132 expect(await getHeadingText()).toEqual('Review and Submit');
133
134 // Attempt to submit and validate the error.
135 await element(by.buttonText('Submit')).click();
Ravago Jones2813c032022-03-16 23:44:11 -0700136 expect(await getErrorMessage())
137 .toContain('Failed to find team 971 in match 3 in the schedule.');
Philipp Schrader5f190012022-03-15 23:29:09 -0700138 });
139
Philipp Schrader5079fa12022-03-19 15:40:12 -0700140 // Make sure that each page on the Entry tab has both "Next" and "Back"
141 // buttons. The only screens exempted from this are the first page and the
142 // last page.
143 it('should: have forwards and backwards buttons.', async () => {
144 await loadPage();
145
146 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
147
148 const expectedOrder = [
149 'Team Selection',
150 'Auto',
151 'TeleOp',
152 'Climb',
153 'Other',
154 'Review and Submit',
155 ];
156
157 // Go forward through the screens.
158 for (let i = 0; i < expectedOrder.length; i++) {
159 expect(await getHeadingText()).toEqual(expectedOrder[i]);
160 if (i != expectedOrder.length - 1) {
161 await element(by.buttonText('Next')).click();
162 }
163 }
164
165 // Go backwards through the screens.
166 for (let i = 0; i < expectedOrder.length; i++) {
167 expect(await getHeadingText())
168 .toEqual(expectedOrder[expectedOrder.length - i - 1]);
169 if (i != expectedOrder.length - 1) {
170 await element(by.buttonText('Back')).click();
171 }
172 }
173 });
Philipp Schrader5f190012022-03-15 23:29:09 -0700174
Philipp Schraderfa096932022-03-05 20:07:10 -0800175 it('should: review and submit correct data.', async () => {
Philipp Schrader577befe2022-03-15 00:00:49 -0700176 await loadPage();
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800177
Ravago Jones2813c032022-03-16 23:44:11 -0700178 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
179
Philipp Schrader5f190012022-03-15 23:29:09 -0700180 // Submit scouting data for a random team that attended 2016nytr.
Philipp Schraderfa096932022-03-05 20:07:10 -0800181 expect(await getHeadingText()).toEqual('Team Selection');
Philipp Schrader5f190012022-03-15 23:29:09 -0700182 await setTextboxByIdTo('match_number', '2');
183 await setTextboxByIdTo('team_number', '5254');
Philipp Schraderfa096932022-03-05 20:07:10 -0800184 await element(by.buttonText('Next')).click();
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800185
Philipp Schraderfa096932022-03-05 20:07:10 -0800186 expect(await getHeadingText()).toEqual('Auto');
Philipp Schradere7c252d2022-03-17 21:13:47 -0700187 await element(by.id('quadrant3')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800188 await element(by.buttonText('Next')).click();
189
190 expect(await getHeadingText()).toEqual('TeleOp');
191 await element(by.buttonText('Next')).click();
192
193 expect(await getHeadingText()).toEqual('Climb');
Philipp Schrader5990fd32022-03-15 21:49:58 -0700194 await element(by.id('high')).click();
Philipp Schrader5079fa12022-03-19 15:40:12 -0700195 await setTextboxByIdTo('comment', 'A very useful comment here.');
Philipp Schraderfa096932022-03-05 20:07:10 -0800196 await element(by.buttonText('Next')).click();
197
Philipp Schradere279e1a2022-03-15 22:20:10 -0700198 expect(await getHeadingText()).toEqual('Other');
Philipp Schraderfa45d742022-03-18 19:29:05 -0700199 await adjustNthSliderBy(0, 3);
200 await adjustNthSliderBy(1, 1);
Philipp Schradere279e1a2022-03-15 22:20:10 -0700201 await element(by.id('no_show')).click();
202 await element(by.id('mechanically_broke')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800203 await element(by.buttonText('Next')).click();
204
205 expect(await getHeadingText()).toEqual('Review and Submit');
206 expect(await getErrorMessage()).toEqual('');
207
208 // Validate Team Selection.
Philipp Schrader5f190012022-03-15 23:29:09 -0700209 await expectReviewFieldToBe('Match number', '2');
210 await expectReviewFieldToBe('Team number', '5254');
Philipp Schraderfa096932022-03-05 20:07:10 -0800211
212 // Validate Auto.
213 await expectNthReviewFieldToBe('Upper Shots Made', 0, '0');
214 await expectNthReviewFieldToBe('Lower Shots Made', 0, '0');
215 await expectNthReviewFieldToBe('Missed Shots', 0, '0');
Philipp Schradere7c252d2022-03-17 21:13:47 -0700216 await expectReviewFieldToBe('Quadrant', '3');
Philipp Schraderfa096932022-03-05 20:07:10 -0800217
218 // Validate TeleOp.
219 await expectNthReviewFieldToBe('Upper Shots Made', 1, '0');
220 await expectNthReviewFieldToBe('Lower Shots Made', 1, '0');
221 await expectNthReviewFieldToBe('Missed Shots', 1, '0');
222
223 // Validate Climb.
Philipp Schrader5990fd32022-03-15 21:49:58 -0700224 await expectReviewFieldToBe('Level', 'High');
Philipp Schraderfa45d742022-03-18 19:29:05 -0700225 await expectReviewFieldToBe('Comments', 'A very useful comment here.');
Philipp Schraderfa096932022-03-05 20:07:10 -0800226
Philipp Schradere279e1a2022-03-15 22:20:10 -0700227 // Validate Other.
Philipp Schraderfa45d742022-03-18 19:29:05 -0700228 await expectReviewFieldToBe('Defense Played On Rating', '3');
229 await expectReviewFieldToBe('Defense Played Rating', '1');
Philipp Schradere279e1a2022-03-15 22:20:10 -0700230 await expectReviewFieldToBe('No show', 'true');
231 await expectReviewFieldToBe('Never moved', 'false');
232 await expectReviewFieldToBe('Battery died', 'false');
233 await expectReviewFieldToBe('Broke (mechanically)', 'true');
Philipp Schraderfa096932022-03-05 20:07:10 -0800234
Philipp Schrader5f190012022-03-15 23:29:09 -0700235 await element(by.buttonText('Submit')).click();
Ravago Jones2813c032022-03-16 23:44:11 -0700236 await browser.wait(
237 EC.textToBePresentInElement(element(by.css('.header')), 'Success'));
Philipp Schrader5f190012022-03-15 23:29:09 -0700238
239 // TODO(phil): Make sure the data made its way to the database correctly.
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800240 });
Philipp Schrader577befe2022-03-15 00:00:49 -0700241
242 it('should: load all images successfully.', async () => {
243 await loadPage();
244
Ravago Jones2813c032022-03-16 23:44:11 -0700245 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
246
Philipp Schrader577befe2022-03-15 00:00:49 -0700247 // Get to the Auto display with the field pictures.
248 expect(await getHeadingText()).toEqual('Team Selection');
249 await element(by.buttonText('Next')).click();
250 expect(await getHeadingText()).toEqual('Auto');
251
252 // We expect 2 fully loaded images.
Ravago Jones2813c032022-03-16 23:44:11 -0700253 browser
254 .executeAsyncScript(function(callback) {
255 let images = document.getElementsByTagName('img');
256 let numLoaded = 0;
257 for (let i = 0; i < images.length; i += 1) {
258 if (images[i].naturalWidth > 0) {
259 numLoaded += 1;
260 }
261 }
262 callback(numLoaded);
263 })
264 .then(function(numLoaded) {
265 expect(numLoaded).toBe(2);
266 });
Philipp Schrader577befe2022-03-15 00:00:49 -0700267 });
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800268});