Skip to content

Instantly share code, notes, and snippets.

@arthurtsang
Last active August 30, 2019 17:08
Show Gist options
  • Save arthurtsang/b8f6ce0762a80dac4c44490426ff717d to your computer and use it in GitHub Desktop.
Save arthurtsang/b8f6ce0762a80dac4c44490426ff717d to your computer and use it in GitHub Desktop.
Simple utility class for protractor
import { browser, ElementFinder, ExpectedConditions, ProtractorBrowser, by } from 'protractor';
export const defaultTimeout = 10000;
export class AppPo {
browserWindow: ProtractorBrowser;
constructor(browserWindow: ProtractorBrowser = browser) {
this.browserWindow = browserWindow;
}
getBrowserWindow(): ProtractorBrowser {
if (!this.browserWindow) {
this.browserWindow = browser;
}
return this.browserWindow;
}
async newTab(url: string) {
await this.browserWindow.executeScript(
"window.open(arguments[0], '_blank')",
url
);
}
switchToTab(handle: string) {
return this.browserWindow.switchTo().window(handle);
}
closeTab(handle: string) {
this.switchToTab(handle);
return this.browserWindow.executeScript('window.close();');
}
async expandShadowRoot(shadowRootHost: WebElement) {
return (await this.browserWindow.driver.executeScript(
'return arguments[0].shadowRoot',
shadowRootHost
)) as WebElement;
}
async sendKeys(
elementToSendKey: ElementFinder,
textToSend: string,
clearFirst: boolean = true
) {
await this.waitForElementToBePresent(elementToSendKey);
await this.mouseMove(elementToSendKey);
await this.waitForElementToBeVisible(elementToSendKey);
if (clearFirst) await elementToSendKey.clear();
await elementToSendKey.sendKeys(textToSend);
}
async click(elementToClick: ElementFinder) {
await this.waitForElementToBePresent(elementToClick);
await this.mouseMove(elementToClick);
await this.waitForElementToBeClickable(elementToClick);
await elementToClick.click();
}
mouseMove(elementToMove: ElementFinder) {
return this.browserWindow
.actions()
.mouseMove(elementToMove)
.perform();
}
waitForElementToBeVisible(elementToWaitFor: ElementFinder) {
return this.browserWindow.wait(
ExpectedConditions.visibilityOf(elementToWaitFor),
defaultTimeout,
'Time out waiting for ' + elementToWaitFor.locator() + ' to be visible'
);
}
waitForElementToBePresent(elementToWaitFor: ElementFinder) {
return this.browserWindow.wait(
ExpectedConditions.presenceOf(elementToWaitFor),
defaultTimeout,
'Timeout waiting for ' + elementToWaitFor.locator()
);
}
waitForElementToBeClickable(elementToWaitFor: ElementFinder) {
return this.browserWindow.wait(
ExpectedConditions.elementToBeClickable(elementToWaitFor),
defaultTimeout,
'Timeout waiting for ' + elementToWaitFor.locator() + ' to be clickable'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment