2020-12-16 13:45:07 -05:00
|
|
|
import {browser, by, element, ElementArrayFinder, ElementFinder, ExpectedConditions} from 'protractor';
|
2020-09-22 14:33:50 -04:00
|
|
|
|
|
|
|
export class AppPage {
|
2020-11-23 10:46:27 -05:00
|
|
|
browser = browser;
|
|
|
|
|
2020-09-22 14:33:50 -04:00
|
|
|
navigateTo() {
|
|
|
|
return browser.get(browser.baseUrl) as Promise<any>;
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:45:07 -05:00
|
|
|
async clickAndExpectRoute(clickSelector: string, expectedRoute: string | RegExp) {
|
|
|
|
await this.waitForClickable(clickSelector);
|
|
|
|
await this.clickElement(clickSelector);
|
|
|
|
|
2020-09-22 14:33:50 -04:00
|
|
|
if (typeof expectedRoute === 'string') {
|
2020-12-16 13:45:07 -05:00
|
|
|
await expect(this.getRoute()).toEqual(expectedRoute);
|
2020-09-22 14:33:50 -04:00
|
|
|
} else {
|
2020-12-16 13:45:07 -05:00
|
|
|
const actualRoute = await this.getRoute();
|
|
|
|
await expect(actualRoute).toMatch(expectedRoute);
|
2020-09-22 14:33:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clickElement(selector: string) {
|
|
|
|
this.waitForClickable(selector);
|
|
|
|
this.scrollTo(selector);
|
|
|
|
this.focus(selector);
|
|
|
|
return this.getElement(selector).click();
|
|
|
|
}
|
|
|
|
|
|
|
|
closeTab() {
|
|
|
|
return browser.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
focus(selector: string) {
|
|
|
|
return browser.controlFlow().execute(() => {
|
|
|
|
return browser.executeScript('arguments[0].focus()', this.getElement(selector).getWebElement());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getElement(selector: string): ElementFinder {
|
|
|
|
return element.all(by.css(selector)).first();
|
|
|
|
}
|
|
|
|
|
|
|
|
getElements(selector: string): ElementArrayFinder {
|
|
|
|
return element.all(by.css(selector));
|
|
|
|
}
|
|
|
|
|
|
|
|
getLocalStorageVar(name: string) {
|
|
|
|
return browser.executeScript(`return window.localStorage.getItem('${name}');`);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNumTabs() {
|
|
|
|
return browser.getAllWindowHandles().then(wh => {
|
|
|
|
return wh.length;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRoute() {
|
|
|
|
const url = await this.getUrl();
|
|
|
|
return '/' + url.split(browser.baseUrl)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
getText(selector: string) {
|
|
|
|
return element(by.css(selector)).getText() as Promise<string>;
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:45:07 -05:00
|
|
|
async getUrl(): Promise<string> {
|
|
|
|
return await browser.executeScript('return location.href') as string;
|
2020-09-22 14:33:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
scrollTo(selector: string) {
|
|
|
|
browser.controlFlow().execute(() => {
|
|
|
|
browser.executeScript('arguments[0].scrollIntoView(false)', this.getElement(selector).getWebElement());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setLocalStorageVar(name: string, value: string) {
|
|
|
|
return browser.executeScript(`return window.localStorage.setItem('${name}','${value}');`);
|
|
|
|
}
|
|
|
|
|
|
|
|
switchFocusToTab(tabIndex: number) {
|
|
|
|
return browser.getAllWindowHandles().then(wh => {
|
|
|
|
return wh.forEach((h, i) => {
|
2020-12-16 13:45:07 -05:00
|
|
|
if (i === tabIndex) {
|
|
|
|
return browser.switchTo().window(h);
|
|
|
|
}
|
2020-09-22 14:33:50 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
waitFor(t: number) {
|
|
|
|
return browser.sleep(t);
|
|
|
|
}
|
2020-12-16 13:45:07 -05:00
|
|
|
|
2020-11-23 10:46:27 -05:00
|
|
|
waitForAngularEnabled(enabled: boolean) {
|
|
|
|
return browser.waitForAngularEnabled(enabled);
|
|
|
|
}
|
2020-09-22 14:33:50 -04:00
|
|
|
|
|
|
|
waitForClickable(selector: string) {
|
|
|
|
const e = this.getElement(selector);
|
|
|
|
return browser.wait(ExpectedConditions.elementToBeClickable(e), 5000);
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:45:07 -05:00
|
|
|
// Waits up to 5 seconds for the element to become invisible.
|
2020-09-22 14:33:50 -04:00
|
|
|
waitForNotVisible(selector: string) {
|
2020-12-16 13:45:07 -05:00
|
|
|
return browser.wait(
|
|
|
|
ExpectedConditions.invisibilityOf(this.getElement(selector)),
|
|
|
|
5000,
|
|
|
|
`Element "${selector}" is still visible after waiting for 5 seconds.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.`
|
|
|
|
);
|
2020-11-23 10:46:27 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 13:45:07 -05:00
|
|
|
// 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) {
|
2020-11-23 10:46:27 -05:00
|
|
|
expect(this.getElements(selector).count()).toEqual(1);
|
|
|
|
const field = this.getElement(selector);
|
2020-12-16 13:45:07 -05:00
|
|
|
// await this.waitForAngularEnabled(true);
|
|
|
|
await this.waitForVisible(selector);
|
2020-11-23 10:46:27 -05:00
|
|
|
|
|
|
|
if (clearFirst) {
|
2020-12-16 13:45:07 -05:00
|
|
|
await field.clear();
|
|
|
|
await expect(field.getAttribute('value')).toEqual('');
|
2020-11-23 10:46:27 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 13:45:07 -05:00
|
|
|
await field.sendKeys(textToEnter);
|
|
|
|
await expect(field.getAttribute('value')).toEqual(textToEnter);
|
2020-09-22 14:33:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|