blob: 43423c18d9bbf27ad68efcf676d79b38dae47657 [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');
114 await element(by.buttonText('Next')).click();
115
116 expect(await getHeadingText()).toEqual('TeleOp');
117 await element(by.buttonText('Next')).click();
118
119 expect(await getHeadingText()).toEqual('Climb');
Philipp Schrader5990fd32022-03-15 21:49:58 -0700120 await element(by.id('high')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800121 await element(by.buttonText('Next')).click();
122
Philipp Schradere279e1a2022-03-15 22:20:10 -0700123 expect(await getHeadingText()).toEqual('Other');
124 await element(by.id('no_show')).click();
125 await element(by.id('mechanically_broke')).click();
Philipp Schraderfa096932022-03-05 20:07:10 -0800126 await element(by.buttonText('Next')).click();
127
128 expect(await getHeadingText()).toEqual('Review and Submit');
129 expect(await getErrorMessage()).toEqual('');
130
131 // Validate Team Selection.
Philipp Schrader5f190012022-03-15 23:29:09 -0700132 await expectReviewFieldToBe('Match number', '2');
133 await expectReviewFieldToBe('Team number', '5254');
Philipp Schraderfa096932022-03-05 20:07:10 -0800134
135 // Validate Auto.
136 await expectNthReviewFieldToBe('Upper Shots Made', 0, '0');
137 await expectNthReviewFieldToBe('Lower Shots Made', 0, '0');
138 await expectNthReviewFieldToBe('Missed Shots', 0, '0');
139
140 // Validate TeleOp.
141 await expectNthReviewFieldToBe('Upper Shots Made', 1, '0');
142 await expectNthReviewFieldToBe('Lower Shots Made', 1, '0');
143 await expectNthReviewFieldToBe('Missed Shots', 1, '0');
144
145 // Validate Climb.
Philipp Schrader5990fd32022-03-15 21:49:58 -0700146 await expectReviewFieldToBe('Level', 'High');
Philipp Schraderfa096932022-03-05 20:07:10 -0800147
Philipp Schradere279e1a2022-03-15 22:20:10 -0700148 // Validate Other.
Yash Chainani43a81022022-03-12 16:46:47 -0800149 await expectReviewFieldToBe('Defense Played On Rating', '0');
150 await expectReviewFieldToBe('Defense Played Rating', '0');
Philipp Schradere279e1a2022-03-15 22:20:10 -0700151 await expectReviewFieldToBe('No show', 'true');
152 await expectReviewFieldToBe('Never moved', 'false');
153 await expectReviewFieldToBe('Battery died', 'false');
154 await expectReviewFieldToBe('Broke (mechanically)', 'true');
Philipp Schraderfa096932022-03-05 20:07:10 -0800155
Philipp Schrader5f190012022-03-15 23:29:09 -0700156 await element(by.buttonText('Submit')).click();
157 await browser.wait(EC.textToBePresentInElement(
158 element(by.css('.header')), 'Home'));
159
160 // TODO(phil): Make sure the data made its way to the database correctly.
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800161 });
Philipp Schrader577befe2022-03-15 00:00:49 -0700162
163 it('should: load all images successfully.', async () => {
164 await loadPage();
165
166 // Get to the Auto display with the field pictures.
167 expect(await getHeadingText()).toEqual('Team Selection');
168 await element(by.buttonText('Next')).click();
169 expect(await getHeadingText()).toEqual('Auto');
170
171 // We expect 2 fully loaded images.
172 browser.executeAsyncScript(function (callback) {
173 let images = document.getElementsByTagName('img');
174 let numLoaded = 0;
175 for (let i = 0; i < images.length; i += 1) {
176 if (images[i].naturalWidth > 0) {
177 numLoaded += 1;
178 }
179 }
180 callback(numLoaded);
181 }).then(function (numLoaded) {
182 expect(numLoaded).toBe(2);
183 });
184 });
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800185});