blob: fda3834ad1232cc4a9cfb1ccaffb732c9dff5535 [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() {
18 await browser.executeAsyncScript(function (callback) {
Philipp Schrader5f190012022-03-15 23:29:09 -070019 let block_alerts = document.getElementById('block_alerts') as HTMLInputElement;
Philipp Schradercf915462022-03-16 23:42:22 -070020 block_alerts.checked = true;
21 callback();
22 });
23}
Philipp Schraderfa096932022-03-05 20:07:10 -080024// Returns the contents of the header that displays the "Auto", "TeleOp", and
25// "Climb" labels etc.
26function getHeadingText() {
27 return element(by.css('.header')).getText();
28}
Philipp Schraderd999c9f2022-02-27 15:48:58 -080029
Philipp Schrader5f190012022-03-15 23:29:09 -070030// Returns the currently displayed progress message on the screen. This only
31// exists on screens where the web page interacts with the web server.
32function getProgressMessage() {
33 return element(by.css('.progress_message')).getText();
34}
35
Philipp Schraderfa096932022-03-05 20:07:10 -080036// Returns the currently displayed error message on the screen. This only
37// exists on screens where the web page interacts with the web server.
38function getErrorMessage() {
39 return element(by.css('.error_message')).getText();
40}
Philipp Schraderd999c9f2022-02-27 15:48:58 -080041
Philipp Schraderfa096932022-03-05 20:07:10 -080042// Asserts that the field on the "Submit and Review" screen has a specific
43// value.
44function expectReviewFieldToBe(fieldName: string, expectedValue: string) {
45 return expectNthReviewFieldToBe(fieldName, 0, expectedValue);
46}
47
48// Asserts that the n'th instance of a field on the "Submit and Review"
49// screen has a specific value.
50async function expectNthReviewFieldToBe(fieldName: string, n: number, expectedValue: string) {
51 expect(await element.all(by.cssContainingText('li', `${fieldName}:`)).get(n).getText())
52 .toEqual(`${fieldName}: ${expectedValue}`);
Philipp Schraderd999c9f2022-02-27 15:48:58 -080053}
54
Philipp Schrader5f190012022-03-15 23:29:09 -070055// Sets a text field to the specified value.
56function setTextboxByIdTo(id: string, value: string) {
57 // Just sending "value" to the input fields is insufficient. We need to
58 // overwrite the text that is there. If we didn't hit CTRL-A to select all
59 // the text, we'd be appending to whatever is there already.
60 return element(by.id(id)).sendKeys(
61 protractor.Key.CONTROL, 'a', protractor.Key.NULL,
62 value);
63}
64
Philipp Schraderd999c9f2022-02-27 15:48:58 -080065describe('The scouting web page', () => {
Philipp Schradercf915462022-03-16 23:42:22 -070066 beforeAll(async () => {
67 await browser.get(browser.baseUrl);
68 expect((await browser.getTitle())).toEqual('FRC971 Scouting Application');
69 await disableAlerts();
Philipp Schrader5f190012022-03-15 23:29:09 -070070
71 // Import the match list before running any tests. Ideally this should be
72 // run in beforeEach(), but it's not worth doing that at this time. Our
73 // tests are basic enough not to require this.
74 await element(by.cssContainingText('.nav-link', 'Import Match List')).click();
75 expect(await getHeadingText()).toEqual('Import Match List');
76 await setTextboxByIdTo('year', '2016');
77 await setTextboxByIdTo('event_code', 'nytr');
78 await element(by.buttonText('Import')).click();
79
80 await browser.wait(EC.textToBePresentInElement(
81 element(by.css('.progress_message')), 'Successfully imported match list.'));
Philipp Schradercf915462022-03-16 23:42:22 -070082 });
83
Philipp Schrader5f190012022-03-15 23:29:09 -070084 it('should: error on unknown match.', async () => {
85 await loadPage();
86
87 // Pick a match that doesn't exist in the 2016nytr match list.
88 await setTextboxByIdTo('match_number', '3');
89 await setTextboxByIdTo('team_number', '971');
90
91 // Click Next until we get to the submit screen.
92 for (let i = 0; i < 5; i++) {
93 await element(by.buttonText('Next')).click();
94 }
95 expect(await getHeadingText()).toEqual('Review and Submit');
96
97 // Attempt to submit and validate the error.
98 await element(by.buttonText('Submit')).click();
99 expect(await getErrorMessage()).toContain(
100 'Failed to find team 971 in match 3 in the schedule.');
101 });
102
103
Philipp Schraderfa096932022-03-05 20:07:10 -0800104 it('should: review and submit correct data.', async () => {
Philipp Schrader577befe2022-03-15 00:00:49 -0700105 await loadPage();
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800106
Philipp Schrader5f190012022-03-15 23:29:09 -0700107 // Submit scouting data for a random team that attended 2016nytr.
Philipp Schraderfa096932022-03-05 20:07:10 -0800108 expect(await getHeadingText()).toEqual('Team Selection');
Philipp Schrader5f190012022-03-15 23:29:09 -0700109 await setTextboxByIdTo('match_number', '2');
110 await setTextboxByIdTo('team_number', '5254');
Philipp Schraderfa096932022-03-05 20:07:10 -0800111 await element(by.buttonText('Next')).click();
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800112
Philipp Schraderfa096932022-03-05 20:07:10 -0800113 expect(await getHeadingText()).toEqual('Auto');
Philipp Schradere7c252d2022-03-17 21:13:47 -0700114 await element(by.id('quadrant3')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800115 await element(by.buttonText('Next')).click();
116
117 expect(await getHeadingText()).toEqual('TeleOp');
118 await element(by.buttonText('Next')).click();
119
120 expect(await getHeadingText()).toEqual('Climb');
Philipp Schrader5990fd32022-03-15 21:49:58 -0700121 await element(by.id('high')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800122 await element(by.buttonText('Next')).click();
123
Philipp Schradere279e1a2022-03-15 22:20:10 -0700124 expect(await getHeadingText()).toEqual('Other');
125 await element(by.id('no_show')).click();
126 await element(by.id('mechanically_broke')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800127 await element(by.buttonText('Next')).click();
128
129 expect(await getHeadingText()).toEqual('Review and Submit');
130 expect(await getErrorMessage()).toEqual('');
131
132 // Validate Team Selection.
Philipp Schrader5f190012022-03-15 23:29:09 -0700133 await expectReviewFieldToBe('Match number', '2');
134 await expectReviewFieldToBe('Team number', '5254');
Philipp Schraderfa096932022-03-05 20:07:10 -0800135
136 // Validate Auto.
137 await expectNthReviewFieldToBe('Upper Shots Made', 0, '0');
138 await expectNthReviewFieldToBe('Lower Shots Made', 0, '0');
139 await expectNthReviewFieldToBe('Missed Shots', 0, '0');
Philipp Schradere7c252d2022-03-17 21:13:47 -0700140 await expectReviewFieldToBe('Quadrant', '3');
Philipp Schraderfa096932022-03-05 20:07:10 -0800141
142 // Validate TeleOp.
143 await expectNthReviewFieldToBe('Upper Shots Made', 1, '0');
144 await expectNthReviewFieldToBe('Lower Shots Made', 1, '0');
145 await expectNthReviewFieldToBe('Missed Shots', 1, '0');
146
147 // Validate Climb.
Philipp Schrader5990fd32022-03-15 21:49:58 -0700148 await expectReviewFieldToBe('Level', 'High');
Philipp Schraderfa096932022-03-05 20:07:10 -0800149
Philipp Schradere279e1a2022-03-15 22:20:10 -0700150 // Validate Other.
Yash Chainani43a81022022-03-12 16:46:47 -0800151 await expectReviewFieldToBe('Defense Played On Rating', '0');
152 await expectReviewFieldToBe('Defense Played Rating', '0');
Philipp Schradere279e1a2022-03-15 22:20:10 -0700153 await expectReviewFieldToBe('No show', 'true');
154 await expectReviewFieldToBe('Never moved', 'false');
155 await expectReviewFieldToBe('Battery died', 'false');
156 await expectReviewFieldToBe('Broke (mechanically)', 'true');
Philipp Schraderfa096932022-03-05 20:07:10 -0800157
Philipp Schrader5f190012022-03-15 23:29:09 -0700158 await element(by.buttonText('Submit')).click();
159 await browser.wait(EC.textToBePresentInElement(
Philipp Schradera3c4e262022-03-17 09:04:12 -0700160 element(by.css('.header')), 'Success'));
Philipp Schrader5f190012022-03-15 23:29:09 -0700161
162 // TODO(phil): Make sure the data made its way to the database correctly.
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800163 });
Philipp Schrader577befe2022-03-15 00:00:49 -0700164
165 it('should: load all images successfully.', async () => {
166 await loadPage();
167
168 // Get to the Auto display with the field pictures.
169 expect(await getHeadingText()).toEqual('Team Selection');
170 await element(by.buttonText('Next')).click();
171 expect(await getHeadingText()).toEqual('Auto');
172
173 // We expect 2 fully loaded images.
174 browser.executeAsyncScript(function (callback) {
175 let images = document.getElementsByTagName('img');
176 let numLoaded = 0;
177 for (let i = 0; i < images.length; i += 1) {
178 if (images[i].naturalWidth > 0) {
179 numLoaded += 1;
180 }
181 }
182 callback(numLoaded);
183 }).then(function (numLoaded) {
184 expect(numLoaded).toBe(2);
185 });
186 });
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800187});