blob: 98e1d00b9242f2c43d0cdd962ab82270f762bad4 [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();
Philipp Schrader817cce32022-03-26 15:00:00 -070010 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() {
Philipp Schrader817cce32022-03-26 15:00:00 -070018 await browser.executeAsyncScript(function (callback) {
19 let block_alerts = document.getElementById(
20 'block_alerts'
21 ) as HTMLInputElement;
Philipp Schradercf915462022-03-16 23:42:22 -070022 block_alerts.checked = true;
23 callback();
24 });
25}
Philipp Schraderfa096932022-03-05 20:07:10 -080026// Returns the contents of the header that displays the "Auto", "TeleOp", and
27// "Climb" labels etc.
28function getHeadingText() {
29 return element(by.css('.header')).getText();
30}
Philipp Schraderd999c9f2022-02-27 15:48:58 -080031
Philipp Schrader5f190012022-03-15 23:29:09 -070032// Returns the currently displayed progress message on the screen. This only
33// exists on screens where the web page interacts with the web server.
34function getProgressMessage() {
35 return element(by.css('.progress_message')).getText();
36}
37
Philipp Schraderfa096932022-03-05 20:07:10 -080038// Returns the currently displayed error message on the screen. This only
39// exists on screens where the web page interacts with the web server.
40function getErrorMessage() {
41 return element(by.css('.error_message')).getText();
42}
Philipp Schraderd999c9f2022-02-27 15:48:58 -080043
Philipp Schraderfa096932022-03-05 20:07:10 -080044// Asserts that the field on the "Submit and Review" screen has a specific
45// value.
46function expectReviewFieldToBe(fieldName: string, expectedValue: string) {
47 return expectNthReviewFieldToBe(fieldName, 0, expectedValue);
48}
49
50// Asserts that the n'th instance of a field on the "Submit and Review"
51// screen has a specific value.
Ravago Jones2813c032022-03-16 23:44:11 -070052async function expectNthReviewFieldToBe(
Philipp Schrader817cce32022-03-26 15:00:00 -070053 fieldName: string,
54 n: number,
55 expectedValue: string
56) {
57 expect(
58 await element
59 .all(by.cssContainingText('li', `${fieldName}:`))
60 .get(n)
61 .getText()
62 ).toEqual(`${fieldName}: ${expectedValue}`);
Philipp Schraderd999c9f2022-02-27 15:48:58 -080063}
64
Philipp Schrader5f190012022-03-15 23:29:09 -070065// Sets a text field to the specified value.
66function setTextboxByIdTo(id: string, value: string) {
67 // Just sending "value" to the input fields is insufficient. We need to
68 // overwrite the text that is there. If we didn't hit CTRL-A to select all
69 // the text, we'd be appending to whatever is there already.
70 return element(by.id(id)).sendKeys(
Philipp Schrader817cce32022-03-26 15:00:00 -070071 protractor.Key.CONTROL,
72 'a',
73 protractor.Key.NULL,
74 value
75 );
Philipp Schrader5f190012022-03-15 23:29:09 -070076}
77
Philipp Schraderfa45d742022-03-18 19:29:05 -070078// Moves the nth slider left or right. A positive "adjustBy" value moves the
79// slider to the right. A negative value moves the slider to the left.
80//
81// negative/left <--- 0 ---> positive/right
82async function adjustNthSliderBy(n: number, adjustBy: number) {
Philipp Schrader5079fa12022-03-19 15:40:12 -070083 const slider = element.all(by.css('input[type=range]')).get(n);
84 const key =
Philipp Schrader817cce32022-03-26 15:00:00 -070085 adjustBy > 0 ? protractor.Key.ARROW_RIGHT : protractor.Key.ARROW_LEFT;
Philipp Schraderfa45d742022-03-18 19:29:05 -070086 for (let i = 0; i < Math.abs(adjustBy); i++) {
87 await slider.sendKeys(key);
88 }
89}
90
Philipp Schraderb99a8cc2022-03-18 21:00:02 -070091function getNthMatchLabel(n: number) {
92 return element.all(by.css('.badge')).get(n).getText();
93}
94
Philipp Schraderd999c9f2022-02-27 15:48:58 -080095describe('The scouting web page', () => {
Philipp Schradercf915462022-03-16 23:42:22 -070096 beforeAll(async () => {
97 await browser.get(browser.baseUrl);
Philipp Schrader817cce32022-03-26 15:00:00 -070098 expect(await browser.getTitle()).toEqual('FRC971 Scouting Application');
Philipp Schradercf915462022-03-16 23:42:22 -070099 await disableAlerts();
Philipp Schrader5f190012022-03-15 23:29:09 -0700100
101 // Import the match list before running any tests. Ideally this should be
102 // run in beforeEach(), but it's not worth doing that at this time. Our
103 // tests are basic enough not to require this.
Philipp Schrader817cce32022-03-26 15:00:00 -0700104 await element(
105 by.cssContainingText('.nav-link', 'Import Match List')
106 ).click();
Philipp Schrader5f190012022-03-15 23:29:09 -0700107 expect(await getHeadingText()).toEqual('Import Match List');
108 await setTextboxByIdTo('year', '2016');
109 await setTextboxByIdTo('event_code', 'nytr');
110 await element(by.buttonText('Import')).click();
111
Philipp Schrader817cce32022-03-26 15:00:00 -0700112 await browser.wait(
113 EC.textToBePresentInElement(
Ravago Jones2813c032022-03-16 23:44:11 -0700114 element(by.css('.progress_message')),
Philipp Schrader817cce32022-03-26 15:00:00 -0700115 'Successfully imported match list.'
116 )
117 );
Philipp Schradercf915462022-03-16 23:42:22 -0700118 });
119
Philipp Schraderb99a8cc2022-03-18 21:00:02 -0700120 it('should: show matches in chronological order.', async () => {
121 await loadPage();
122
Philipp Schrader5079fa12022-03-19 15:40:12 -0700123 expect(await getNthMatchLabel(0)).toEqual('Quals 1');
124 expect(await getNthMatchLabel(1)).toEqual('Quals 2');
125 expect(await getNthMatchLabel(2)).toEqual('Quals 3');
126 expect(await getNthMatchLabel(9)).toEqual('Quals 10');
Philipp Schraderb99a8cc2022-03-18 21:00:02 -0700127 // TODO(phil): Validate quarter finals and friends. Right now we don't
128 // distinguish between "sets". I.e. we display 4 "Quarter Final 1" matches
129 // without being able to distinguish between them.
Philipp Schrader5079fa12022-03-19 15:40:12 -0700130 expect(await getNthMatchLabel(87)).toEqual('Final 1');
Philipp Schraderb99a8cc2022-03-18 21:00:02 -0700131 });
132
Philipp Schrader5f190012022-03-15 23:29:09 -0700133 it('should: error on unknown match.', async () => {
134 await loadPage();
135
Ravago Jones2813c032022-03-16 23:44:11 -0700136 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
137
Philipp Schrader5f190012022-03-15 23:29:09 -0700138 // Pick a match that doesn't exist in the 2016nytr match list.
139 await setTextboxByIdTo('match_number', '3');
140 await setTextboxByIdTo('team_number', '971');
141
142 // Click Next until we get to the submit screen.
143 for (let i = 0; i < 5; i++) {
144 await element(by.buttonText('Next')).click();
145 }
146 expect(await getHeadingText()).toEqual('Review and Submit');
147
148 // Attempt to submit and validate the error.
149 await element(by.buttonText('Submit')).click();
Philipp Schrader817cce32022-03-26 15:00:00 -0700150 expect(await getErrorMessage()).toContain(
151 'Failed to find team 971 in match 3 in the schedule.'
152 );
Philipp Schrader5f190012022-03-15 23:29:09 -0700153 });
154
Philipp Schrader5079fa12022-03-19 15:40:12 -0700155 // Make sure that each page on the Entry tab has both "Next" and "Back"
156 // buttons. The only screens exempted from this are the first page and the
157 // last page.
158 it('should: have forwards and backwards buttons.', async () => {
159 await loadPage();
160
161 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
162
163 const expectedOrder = [
164 'Team Selection',
165 'Auto',
166 'TeleOp',
167 'Climb',
168 'Other',
169 'Review and Submit',
170 ];
171
172 // Go forward through the screens.
173 for (let i = 0; i < expectedOrder.length; i++) {
174 expect(await getHeadingText()).toEqual(expectedOrder[i]);
175 if (i != expectedOrder.length - 1) {
176 await element(by.buttonText('Next')).click();
177 }
178 }
179
180 // Go backwards through the screens.
181 for (let i = 0; i < expectedOrder.length; i++) {
Philipp Schrader817cce32022-03-26 15:00:00 -0700182 expect(await getHeadingText()).toEqual(
183 expectedOrder[expectedOrder.length - i - 1]
184 );
Philipp Schrader5079fa12022-03-19 15:40:12 -0700185 if (i != expectedOrder.length - 1) {
186 await element(by.buttonText('Back')).click();
187 }
188 }
189 });
Philipp Schrader5f190012022-03-15 23:29:09 -0700190
Philipp Schraderfa096932022-03-05 20:07:10 -0800191 it('should: review and submit correct data.', async () => {
Philipp Schrader577befe2022-03-15 00:00:49 -0700192 await loadPage();
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800193
Ravago Jones2813c032022-03-16 23:44:11 -0700194 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
195
Philipp Schrader5f190012022-03-15 23:29:09 -0700196 // Submit scouting data for a random team that attended 2016nytr.
Philipp Schraderfa096932022-03-05 20:07:10 -0800197 expect(await getHeadingText()).toEqual('Team Selection');
Philipp Schrader5f190012022-03-15 23:29:09 -0700198 await setTextboxByIdTo('match_number', '2');
199 await setTextboxByIdTo('team_number', '5254');
Philipp Schraderfa096932022-03-05 20:07:10 -0800200 await element(by.buttonText('Next')).click();
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800201
Philipp Schraderfa096932022-03-05 20:07:10 -0800202 expect(await getHeadingText()).toEqual('Auto');
Philipp Schradere7c252d2022-03-17 21:13:47 -0700203 await element(by.id('quadrant3')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800204 await element(by.buttonText('Next')).click();
205
206 expect(await getHeadingText()).toEqual('TeleOp');
207 await element(by.buttonText('Next')).click();
208
209 expect(await getHeadingText()).toEqual('Climb');
Philipp Schrader5990fd32022-03-15 21:49:58 -0700210 await element(by.id('high')).click();
Philipp Schrader5079fa12022-03-19 15:40:12 -0700211 await setTextboxByIdTo('comment', 'A very useful comment here.');
Philipp Schraderfa096932022-03-05 20:07:10 -0800212 await element(by.buttonText('Next')).click();
213
Philipp Schradere279e1a2022-03-15 22:20:10 -0700214 expect(await getHeadingText()).toEqual('Other');
Philipp Schraderfa45d742022-03-18 19:29:05 -0700215 await adjustNthSliderBy(0, 3);
216 await adjustNthSliderBy(1, 1);
Philipp Schradere279e1a2022-03-15 22:20:10 -0700217 await element(by.id('no_show')).click();
218 await element(by.id('mechanically_broke')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800219 await element(by.buttonText('Next')).click();
220
221 expect(await getHeadingText()).toEqual('Review and Submit');
222 expect(await getErrorMessage()).toEqual('');
223
224 // Validate Team Selection.
Philipp Schrader5f190012022-03-15 23:29:09 -0700225 await expectReviewFieldToBe('Match number', '2');
226 await expectReviewFieldToBe('Team number', '5254');
Philipp Schraderfa096932022-03-05 20:07:10 -0800227
228 // Validate Auto.
229 await expectNthReviewFieldToBe('Upper Shots Made', 0, '0');
230 await expectNthReviewFieldToBe('Lower Shots Made', 0, '0');
231 await expectNthReviewFieldToBe('Missed Shots', 0, '0');
Philipp Schradere7c252d2022-03-17 21:13:47 -0700232 await expectReviewFieldToBe('Quadrant', '3');
Philipp Schraderfa096932022-03-05 20:07:10 -0800233
234 // Validate TeleOp.
235 await expectNthReviewFieldToBe('Upper Shots Made', 1, '0');
236 await expectNthReviewFieldToBe('Lower Shots Made', 1, '0');
237 await expectNthReviewFieldToBe('Missed Shots', 1, '0');
238
239 // Validate Climb.
Philipp Schrader5990fd32022-03-15 21:49:58 -0700240 await expectReviewFieldToBe('Level', 'High');
Philipp Schraderfa45d742022-03-18 19:29:05 -0700241 await expectReviewFieldToBe('Comments', 'A very useful comment here.');
Philipp Schraderfa096932022-03-05 20:07:10 -0800242
Philipp Schradere279e1a2022-03-15 22:20:10 -0700243 // Validate Other.
Philipp Schraderfa45d742022-03-18 19:29:05 -0700244 await expectReviewFieldToBe('Defense Played On Rating', '3');
245 await expectReviewFieldToBe('Defense Played Rating', '1');
Philipp Schradere279e1a2022-03-15 22:20:10 -0700246 await expectReviewFieldToBe('No show', 'true');
247 await expectReviewFieldToBe('Never moved', 'false');
248 await expectReviewFieldToBe('Battery died', 'false');
249 await expectReviewFieldToBe('Broke (mechanically)', 'true');
Philipp Schraderfa096932022-03-05 20:07:10 -0800250
Philipp Schrader5f190012022-03-15 23:29:09 -0700251 await element(by.buttonText('Submit')).click();
Ravago Jones2813c032022-03-16 23:44:11 -0700252 await browser.wait(
Philipp Schrader817cce32022-03-26 15:00:00 -0700253 EC.textToBePresentInElement(element(by.css('.header')), 'Success')
254 );
Philipp Schrader5f190012022-03-15 23:29:09 -0700255
256 // TODO(phil): Make sure the data made its way to the database correctly.
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800257 });
Philipp Schrader577befe2022-03-15 00:00:49 -0700258
259 it('should: load all images successfully.', async () => {
260 await loadPage();
261
Ravago Jones2813c032022-03-16 23:44:11 -0700262 await element(by.cssContainingText('.nav-link', 'Data Entry')).click();
263
Philipp Schrader577befe2022-03-15 00:00:49 -0700264 // Get to the Auto display with the field pictures.
265 expect(await getHeadingText()).toEqual('Team Selection');
266 await element(by.buttonText('Next')).click();
267 expect(await getHeadingText()).toEqual('Auto');
268
269 // We expect 2 fully loaded images.
Ravago Jones2813c032022-03-16 23:44:11 -0700270 browser
Philipp Schrader817cce32022-03-26 15:00:00 -0700271 .executeAsyncScript(function (callback) {
272 let images = document.getElementsByTagName('img');
273 let numLoaded = 0;
274 for (let i = 0; i < images.length; i += 1) {
275 if (images[i].naturalWidth > 0) {
276 numLoaded += 1;
Ravago Jones2813c032022-03-16 23:44:11 -0700277 }
Philipp Schrader817cce32022-03-26 15:00:00 -0700278 }
279 callback(numLoaded);
280 })
281 .then(function (numLoaded) {
282 expect(numLoaded).toBe(2);
283 });
Philipp Schrader577befe2022-03-15 00:00:49 -0700284 });
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800285});