chore: cleanup navigation-helper.ts (#8834)

This commit is contained in:
tarunama
2020-09-10 12:27:07 +09:00
committed by GitHub
parent f7713c40a6
commit bd04a4710f

View File

@ -1,4 +1,10 @@
import { AppiumDriver, IRectangle, logInfo, logWarn, Point } from "nativescript-dev-appium"; import {
AppiumDriver,
IRectangle,
logInfo,
logWarn,
Point
} from "nativescript-dev-appium";
export enum ElementCacheStrategy { export enum ElementCacheStrategy {
allAtOnce, allAtOnce,
@ -18,12 +24,15 @@ export class NavigationHelper {
private _cachedElements: ICachedElement; private _cachedElements: ICachedElement;
private _currentSuite: ICachedElement; private _currentSuite: ICachedElement;
constructor(protected _driver: AppiumDriver, protected _suitMainPageNavigationLinks: Array<string>, private _elemtsCacheStrategy: ElementCacheStrategy = ElementCacheStrategy.onload) { constructor(
} protected _driver: AppiumDriver,
protected _suitMainPageNavigationLinks: Array<string>,
private _elementsCacheStrategy: ElementCacheStrategy = ElementCacheStrategy.onload
) {}
async initSuite() { async initSuite(): Promise<void> {
if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce
|| this._elemtsCacheStrategy === ElementCacheStrategy.onload) { || this._elementsCacheStrategy === ElementCacheStrategy.onload) {
if (this._currentSuite) { if (this._currentSuite) {
while (this._currentSuite.parent) { while (this._currentSuite.parent) {
this._currentSuite = this._currentSuite.parent; this._currentSuite = this._currentSuite.parent;
@ -31,7 +40,7 @@ export class NavigationHelper {
} else { } else {
if (!this._cachedElements || this._cachedElements.children.size === 0) { if (!this._cachedElements || this._cachedElements.children.size === 0) {
this._cachedElements = { name: "initSuite", children: new Map<string, ICachedElement>() }; this._cachedElements = { name: "initSuite", children: new Map<string, ICachedElement>() };
if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce) { if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce) {
await this.cacheAllElements(this._cachedElements); await this.cacheAllElements(this._cachedElements);
} }
} }
@ -43,12 +52,12 @@ export class NavigationHelper {
await this.navigateToSuitMainPage(); await this.navigateToSuitMainPage();
} }
async endSuite() { async endSuite(): Promise<void> {
logInfo("End of suit 'button' tests!"); logInfo(`End of suit 'button' tests!`);
await this._driver.takeScreenshot("end_button_suit"); await this._driver.takeScreenshot("end_button_suit");
} }
async navigateToSuitMainPage() { async navigateToSuitMainPage(): Promise<void> {
for (let index = 0; index < this._suitMainPageNavigationLinks.length; index++) { for (let index = 0; index < this._suitMainPageNavigationLinks.length; index++) {
const mainPage = this._suitMainPageNavigationLinks[index]; const mainPage = this._suitMainPageNavigationLinks[index];
await this.navigateToSample(mainPage); await this.navigateToSample(mainPage);
@ -59,7 +68,7 @@ export class NavigationHelper {
logInfo(`Navigate to ${sample}`); logInfo(`Navigate to ${sample}`);
const sampleName = sample.toLowerCase(); const sampleName = sample.toLowerCase();
if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce) { if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce) {
if (!this._currentSuite.children.has(sampleName)) { if (!this._currentSuite.children.has(sampleName)) {
await this.cacheAllElements(this._currentSuite); await this.cacheAllElements(this._currentSuite);
} }
@ -69,7 +78,7 @@ export class NavigationHelper {
const sampleElement = this._currentSuite.children.get(sampleName).rect; const sampleElement = this._currentSuite.children.get(sampleName).rect;
await this._driver.clickPoint(sampleElement.x + (sampleElement.width / 2), sampleElement.y + (sampleElement.height / 2)); await this._driver.clickPoint(sampleElement.x + (sampleElement.width / 2), sampleElement.y + (sampleElement.height / 2));
this._currentSuite = this._currentSuite.children.get(sampleName); this._currentSuite = this._currentSuite.children.get(sampleName);
} else if (this._elemtsCacheStrategy === ElementCacheStrategy.onload) { } else if (this._elementsCacheStrategy === ElementCacheStrategy.onload) {
if (this._currentSuite.children.has(sampleName)) { if (this._currentSuite.children.has(sampleName)) {
const sampleElement = this._currentSuite.children.get(sampleName).rect; const sampleElement = this._currentSuite.children.get(sampleName).rect;
await this._driver.clickPoint(sampleElement.x + (sampleElement.width / 2), sampleElement.y + (sampleElement.height / 2)); await this._driver.clickPoint(sampleElement.x + (sampleElement.width / 2), sampleElement.y + (sampleElement.height / 2));
@ -94,14 +103,14 @@ export class NavigationHelper {
async navigateBackToSuitMainPage() { async navigateBackToSuitMainPage() {
logInfo(`Navigate to back`); logInfo(`Navigate to back`);
if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce
|| this._elemtsCacheStrategy === ElementCacheStrategy.onload) { || this._elementsCacheStrategy === ElementCacheStrategy.onload) {
this._currentSuite = this._currentSuite && this._currentSuite.parent; this._currentSuite = this._currentSuite && this._currentSuite.parent;
} }
await this._driver.navBack(); await this._driver.navBack();
} }
async swipeBackToSuitMainPage() { async swipeBackToSuitMainPage(): Promise<void> {
logInfo(`Swipe to back`); logInfo(`Swipe to back`);
const startPoint = <Point>{}; const startPoint = <Point>{};
const endPoint = <Point>{}; const endPoint = <Point>{};
@ -115,20 +124,29 @@ export class NavigationHelper {
await this._driver.swipe(startPoint, endPoint); await this._driver.swipe(startPoint, endPoint);
} else { } else {
logWarn("Swipe back is not supported from android!"); logWarn(`Swipe back is not supported from android!`);
} }
} }
private async cacheAllElements(cachedElements: ICachedElement) { private async cacheAllElements(cachedElements: ICachedElement): Promise<void> {
const allSamples = await this._driver.findElementsByClassName(this._driver.locators.button); const allSamples = await this._driver.findElementsByClassName(this._driver.locators.button);
for (let index = 0; index < allSamples.length; index++) { for (let index = 0; index < allSamples.length; index++) {
const element = allSamples[index]; const element = allSamples[index];
const rect = await element.getRectangle(); const rect = await element.getRectangle();
const text = (await element.text()).toLowerCase(); const text = (await element.text()).toLowerCase();
if (!cachedElements.children.has(text)) {
console.log(text); if (cachedElements.children.has(text)) continue
cachedElements.children.set(text, { parent: cachedElements, name: text, rect: rect, children: new Map() });
} logInfo(text);
cachedElements.children.set(
text,
{
parent: cachedElements,
name: text,
rect: rect,
children: new Map()
}
);
} }
} }
} }