Fixes busted E2E tests.

This commit is contained in:
Aaron Louie 2020-12-16 13:45:07 -05:00
parent 3d605c619c
commit 218c8a138c
2 changed files with 59 additions and 64 deletions

View File

@ -28,7 +28,7 @@ describe('COVID19 Testing Kiosk App', () => {
page.clickAndExpectRoute('#nav_settings', '/settings');
});
it('should change location', async () => {
it('should change location', () => {
page.inputText('.location-input input', '9999', true);
});
@ -53,31 +53,34 @@ describe('COVID19 Testing Kiosk App', () => {
const locSelector = '.location-input input';
const dropdownSelector = 'mat-select-trigger.selected-label-layout';
const locIdBefore = await page.getElement(locSelector).getAttribute('value');
const layoutBefore = await page.getElement(dropdownSelector).getAttribute('value');
const layoutBefore = await page.getElement(dropdownSelector).getText();
page.clickAndExpectRoute('#btn_save', '/');
await page.clickAndExpectRoute('#btn_save', '/');
const navLocation = await page.getElement('#nav_location').getText();
expect(navLocation).toContain(locIdBefore);
page.clickAndExpectRoute('#nav_settings', '/settings');
await page.clickAndExpectRoute('#nav_settings', '/settings');
await page.waitForVisible(locSelector);
await page.waitForVisible(dropdownSelector);
const locIdAfter = await page.getElement(locSelector).getAttribute('value');
expect(locIdBefore).toEqual(locIdAfter);
const layoutAfter = await page.getElement(dropdownSelector).getAttribute('value');
const layoutAfter = await page.getElement(dropdownSelector).getText();
expect(layoutBefore).toEqual(layoutAfter);
page.clickAndExpectRoute('#btn_save', '/');
await page.clickAndExpectRoute('#btn_save', '/');
});
xit('should enter a sample', async () => {
it('should enter a sample', async () => {
const studentId = '987654321';
const computingId = 'ABC123';
const idFieldSelector = '.cardnum-input input';
const initialsFieldSelector = '.initials-input input';
page.inputText(idFieldSelector, studentId);
page.inputText(initialsFieldSelector, computingId);
page.clickAndExpectRoute('#btn_next', /print/);
page.waitForVisible('#btn_print');
const labelText = page.getElement('app-print-layout').getText();
await page.inputText(idFieldSelector, studentId);
await page.inputText(initialsFieldSelector, computingId);
await page.clickAndExpectRoute('#btn_next', /print/);
await page.waitForVisible('#btn_print');
const labelText = await page.getElement('app-print-layout').getText();
expect(labelText).toContain(studentId);
expect(labelText).toContain(computingId);
});

View File

@ -1,4 +1,4 @@
import {browser, by, element, ElementArrayFinder, ElementFinder, ExpectedConditions, protractor} from 'protractor';
import {browser, by, element, ElementArrayFinder, ElementFinder, ExpectedConditions} from 'protractor';
export class AppPage {
browser = browser;
@ -7,13 +7,15 @@ export class AppPage {
return browser.get(browser.baseUrl) as Promise<any>;
}
clickAndExpectRoute(clickSelector: string, expectedRoute: string | RegExp) {
this.waitForClickable(clickSelector);
this.clickElement(clickSelector);
async clickAndExpectRoute(clickSelector: string, expectedRoute: string | RegExp) {
await this.waitForClickable(clickSelector);
await this.clickElement(clickSelector);
if (typeof expectedRoute === 'string') {
expect(this.getRoute()).toEqual(expectedRoute);
await expect(this.getRoute()).toEqual(expectedRoute);
} else {
expect(this.getRoute()).toMatch(expectedRoute);
const actualRoute = await this.getRoute();
await expect(actualRoute).toMatch(expectedRoute);
}
}
@ -61,8 +63,8 @@ export class AppPage {
return element(by.css(selector)).getText() as Promise<string>;
}
getUrl() {
return browser.getCurrentUrl();
async getUrl(): Promise<string> {
return await browser.executeScript('return location.href') as string;
}
scrollTo(selector: string) {
@ -78,24 +80,17 @@ export class AppPage {
switchFocusToTab(tabIndex: number) {
return browser.getAllWindowHandles().then(wh => {
return wh.forEach((h, i) => {
if (i === tabIndex) { return browser.switchTo().window(h); }
if (i === tabIndex) {
return browser.switchTo().window(h);
}
});
});
}
async switchFocusToPrintDialog() {
const browserWindowHandles = await browser.getAllWindowHandles();
console.log('browserWindowHandles', browserWindowHandles);
const driver = browser.driver;
const windowHandles = await driver.getAllWindowHandles();
console.log('windowHandles', windowHandles);
return driver.switchTo().window(windowHandles[0]);
}
waitFor(t: number) {
return browser.sleep(t);
}
waitForAngularEnabled(enabled: boolean) {
return browser.waitForAngularEnabled(enabled);
}
@ -105,50 +100,47 @@ export class AppPage {
return browser.wait(ExpectedConditions.elementToBeClickable(e), 5000);
}
// Waits up to 5 seconds for the element to become invisible.
waitForNotVisible(selector: string) {
const e = this.getElement(selector);
return browser.wait(ExpectedConditions.invisibilityOf(e), 5000);
return browser.wait(
ExpectedConditions.invisibilityOf(this.getElement(selector)),
5000,
`Element "${selector}" is still visible after waiting for 5 seconds.`
);
}
// If given CSS selector is found on the page, waits 5 seconds for the element to become visible. If it's not found
// on the page, recursively calls itself maxLoops number of times, waiting 1 second between each call, until the
// element becomes present.
async waitForVisible(selector: string, maxLoops = 5) {
const numElements = await this.getElements(selector).count();
if (numElements > 0) {
const e = await this.getElement(selector);
return browser.wait(
ExpectedConditions.visibilityOf(e),
5000,
`Element "${selector}" is still not visible after waiting for 5 seconds.`
);
} else if (maxLoops > 0) {
await this.waitFor(1000);
await this.waitForVisible(selector, maxLoops - 1);
} else {
expect(numElements).toBeGreaterThan(0, `Element "${selector}" is not present on the page.`);
}
// Waits up to 5 seconds for the element to become present.
waitForPresent(selector: string) {
return browser.wait(
ExpectedConditions.presenceOf(this.getElement(selector)),
5000,
`Element "${selector}" is still not present after waiting for 5 seconds.`
);
}
inputText(selector: string, textToEnter: string, clearFirst?: boolean) {
// Waits up to 5 seconds for the element to become visible.
waitForVisible(selector: string) {
this.waitForAngularEnabled(false);
return browser.wait(
ExpectedConditions.visibilityOf(element(by.css(selector))),
5000,
`Element "${selector}" is still not visible after waiting for 5 seconds.`
);
}
async inputText(selector: string, textToEnter: string, clearFirst?: boolean) {
expect(this.getElements(selector).count()).toEqual(1);
const field = this.getElement(selector);
this.waitForAngularEnabled(true);
this.waitForVisible(selector, 100);
// await this.waitForAngularEnabled(true);
await this.waitForVisible(selector);
if (clearFirst) {
field.clear();
expect(field.getAttribute('value')).toEqual('');
await field.clear();
await expect(field.getAttribute('value')).toEqual('');
}
field.sendKeys(textToEnter);
expect(field.getAttribute('value')).toEqual(textToEnter);
await field.sendKeys(textToEnter);
await expect(field.getAttribute('value')).toEqual(textToEnter);
}
async pressTabKey100Times() {
for (let i = 0; i < 100; i++) {
await browser.actions().sendKeys(protractor.Key.TAB).perform();
}
}
}