mirror of
				https://github.com/NativeScript/NativeScript.git
				synced 2025-11-04 12:58:38 +08:00 
			
		
		
		
	chore: cleanup navigation-helper.ts (#8834)
This commit is contained in:
		@ -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 {
 | 
			
		||||
    allAtOnce,
 | 
			
		||||
@ -18,12 +24,15 @@ export class NavigationHelper {
 | 
			
		||||
    private _cachedElements: 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() {
 | 
			
		||||
        if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce
 | 
			
		||||
            || this._elemtsCacheStrategy === ElementCacheStrategy.onload) {
 | 
			
		||||
    async initSuite(): Promise<void> {
 | 
			
		||||
        if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce
 | 
			
		||||
            || this._elementsCacheStrategy === ElementCacheStrategy.onload) {
 | 
			
		||||
            if (this._currentSuite) {
 | 
			
		||||
                while (this._currentSuite.parent) {
 | 
			
		||||
                    this._currentSuite = this._currentSuite.parent;
 | 
			
		||||
@ -31,7 +40,7 @@ export class NavigationHelper {
 | 
			
		||||
            } else {
 | 
			
		||||
                if (!this._cachedElements || this._cachedElements.children.size === 0) {
 | 
			
		||||
                    this._cachedElements = { name: "initSuite", children: new Map<string, ICachedElement>() };
 | 
			
		||||
                    if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce) {
 | 
			
		||||
                    if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce) {
 | 
			
		||||
                        await this.cacheAllElements(this._cachedElements);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
@ -43,12 +52,12 @@ export class NavigationHelper {
 | 
			
		||||
        await this.navigateToSuitMainPage();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async endSuite() {
 | 
			
		||||
        logInfo("End of suit 'button' tests!");
 | 
			
		||||
		async endSuite(): Promise<void> {
 | 
			
		||||
        logInfo(`End of suit 'button' tests!`);
 | 
			
		||||
        await this._driver.takeScreenshot("end_button_suit");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async navigateToSuitMainPage() {
 | 
			
		||||
    async navigateToSuitMainPage(): Promise<void> {
 | 
			
		||||
        for (let index = 0; index < this._suitMainPageNavigationLinks.length; index++) {
 | 
			
		||||
            const mainPage = this._suitMainPageNavigationLinks[index];
 | 
			
		||||
            await this.navigateToSample(mainPage);
 | 
			
		||||
@ -59,7 +68,7 @@ export class NavigationHelper {
 | 
			
		||||
        logInfo(`Navigate to ${sample}`);
 | 
			
		||||
        const sampleName = sample.toLowerCase();
 | 
			
		||||
 | 
			
		||||
        if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce) {
 | 
			
		||||
        if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce) {
 | 
			
		||||
            if (!this._currentSuite.children.has(sampleName)) {
 | 
			
		||||
                await this.cacheAllElements(this._currentSuite);
 | 
			
		||||
            }
 | 
			
		||||
@ -69,7 +78,7 @@ export class NavigationHelper {
 | 
			
		||||
            const sampleElement = this._currentSuite.children.get(sampleName).rect;
 | 
			
		||||
            await this._driver.clickPoint(sampleElement.x + (sampleElement.width / 2), sampleElement.y + (sampleElement.height / 2));
 | 
			
		||||
            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)) {
 | 
			
		||||
                const sampleElement = this._currentSuite.children.get(sampleName).rect;
 | 
			
		||||
                await this._driver.clickPoint(sampleElement.x + (sampleElement.width / 2), sampleElement.y + (sampleElement.height / 2));
 | 
			
		||||
@ -94,14 +103,14 @@ export class NavigationHelper {
 | 
			
		||||
 | 
			
		||||
    async navigateBackToSuitMainPage() {
 | 
			
		||||
        logInfo(`Navigate to back`);
 | 
			
		||||
        if (this._elemtsCacheStrategy === ElementCacheStrategy.allAtOnce
 | 
			
		||||
            || this._elemtsCacheStrategy === ElementCacheStrategy.onload) {
 | 
			
		||||
        if (this._elementsCacheStrategy === ElementCacheStrategy.allAtOnce
 | 
			
		||||
            || this._elementsCacheStrategy === ElementCacheStrategy.onload) {
 | 
			
		||||
            this._currentSuite = this._currentSuite && this._currentSuite.parent;
 | 
			
		||||
        }
 | 
			
		||||
        await this._driver.navBack();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async swipeBackToSuitMainPage() {
 | 
			
		||||
    async swipeBackToSuitMainPage(): Promise<void> {
 | 
			
		||||
        logInfo(`Swipe to back`);
 | 
			
		||||
        const startPoint = <Point>{};
 | 
			
		||||
        const endPoint = <Point>{};
 | 
			
		||||
@ -115,20 +124,29 @@ export class NavigationHelper {
 | 
			
		||||
 | 
			
		||||
            await this._driver.swipe(startPoint, endPoint);
 | 
			
		||||
        } else {
 | 
			
		||||
            logWarn("Swipe back is not supported from android!");
 | 
			
		||||
            logWarn(`Swipe back is not supported from android!`);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async cacheAllElements(cachedElements: ICachedElement) {
 | 
			
		||||
        const allSamples = await this._driver.findElementsByClassName(this._driver.locators.button);
 | 
			
		||||
    private async cacheAllElements(cachedElements: ICachedElement): Promise<void> {
 | 
			
		||||
				const allSamples = await this._driver.findElementsByClassName(this._driver.locators.button);
 | 
			
		||||
        for (let index = 0; index < allSamples.length; index++) {
 | 
			
		||||
            const element = allSamples[index];
 | 
			
		||||
            const rect = await element.getRectangle();
 | 
			
		||||
            const text = (await element.text()).toLowerCase();
 | 
			
		||||
            if (!cachedElements.children.has(text)) {
 | 
			
		||||
                console.log(text);
 | 
			
		||||
                cachedElements.children.set(text, { parent: cachedElements, name: text, rect: rect, children: new Map() });
 | 
			
		||||
            }
 | 
			
		||||
						const text = (await element.text()).toLowerCase();
 | 
			
		||||
 | 
			
		||||
						if (cachedElements.children.has(text)) continue
 | 
			
		||||
 | 
			
		||||
						logInfo(text);
 | 
			
		||||
						cachedElements.children.set(
 | 
			
		||||
							  text,
 | 
			
		||||
							  {
 | 
			
		||||
								    parent: cachedElements,
 | 
			
		||||
								    name: text,
 | 
			
		||||
								    rect: rect,
 | 
			
		||||
								    children: new Map()
 | 
			
		||||
							  }
 | 
			
		||||
					  );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user