Skip to content

Commit bb9b2a5

Browse files
author
Zdravko
authored
Zbranzov/scroll (#212)
* fix: scrollTo performance on iOS * fix: add error in case of wrong imagesPath provided instead of stalling the run
1 parent c823472 commit bb9b2a5

File tree

6 files changed

+31
-25
lines changed

6 files changed

+31
-25
lines changed

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export async function createDriver(args?: INsCapabilitiesArgs) {
8989
return appiumDriver;
9090
}
9191
if (!appiumServer.server && !nsCapabilities.isSauceLab) {
92-
logInfo("Server is not available! To start appium server programaticlly use startServer()!");
92+
logInfo("Server is not available! To start appium server programmaticаlly use startServer()!");
9393
}
9494
if (!nsCapabilities.appiumCapsLocation) {
9595
throw new Error("Provided path to appium capabilities is not correct!");

lib/appium-driver.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export declare class AppiumDriver {
155155
* @param xOffset
156156
* @param retryCount
157157
*/
158-
scrollTo(direction: Direction, element: any, startPoint: Point, yOffset: number, xOffset?: number, retryCount?: number): Promise<any>;
158+
scrollTo(direction: Direction, element: any, startPoint: Point, yOffset: number, xOffset?: number, retryCount?: number): Promise<UIElement>;
159159
/**
160160
* Swipe from point with offset and inertia according to duatio
161161
* @param y

lib/appium-driver.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class AppiumDriver {
291291
*/
292292
public async findElementByXPath(xPath: string, waitForElement: number = this.defaultWaitTime) {
293293
const searchM = "waitForElementByXPath";
294-
return await new UIElement(await this._driver.waitForElementByXPath(xPath, waitForElement), this._driver, this._wd, this._webio, this._args, searchM, xPath);
294+
return new UIElement(await this._driver.waitForElementByXPath(xPath, waitForElement), this._driver, this._wd, this._webio, this._args, searchM, xPath);
295295
}
296296

297297
/**
@@ -446,20 +446,18 @@ export class AppiumDriver {
446446
* @param retryCount
447447
*/
448448
public async scrollTo(direction: Direction, element: any, startPoint: Point, yOffset: number, xOffset: number = 0, retryCount: number = 7) {
449-
let el = null;
450-
while (el === null && retryCount > 0) {
449+
let el: UIElement = null;
450+
let isDisplayed: boolean = false;
451+
while ((el === null || !isDisplayed) && retryCount > 0) {
451452
try {
452453
el = await element();
453-
if (!(await el.isDisplayed())) {
454+
isDisplayed = await el.isDisplayed();
455+
if (!isDisplayed) {
454456
await scroll(this._wd, this._driver, direction, this._webio.isIOS, startPoint.y, startPoint.x, yOffset, xOffset, this._args.verbose);
457+
el = null;
455458
}
456459
} catch (error) {
457-
await scroll(this._wd, this._driver, direction, this._webio.isIOS, startPoint.y, startPoint.x, yOffset, xOffset, this._args.verbose);
458-
}
459-
if (el !== null && (await el.isDisplayed())) {
460-
break;
461-
} else {
462-
el = null;
460+
console.log("scrollTo Error: " + error);
463461
}
464462

465463
retryCount--;
@@ -750,7 +748,9 @@ export class AppiumDriver {
750748
* Hides device keyboard
751749
*/
752750
public async hideDeviceKeyboard() {
753-
await this._driver.hideDeviceKeyboard();
751+
try {
752+
await this._driver.hideDeviceKeyboard();
753+
} catch (error) {}
754754
}
755755

756756
public async isKeyboardShown() {
@@ -892,7 +892,7 @@ export class AppiumDriver {
892892
const element = await this._driver.elementByXPathIfExists(xPath, waitForElement);
893893
if (element) {
894894
const searchMethod = "elementByXPathIfExists";
895-
return await new UIElement(element, this._driver, this._wd, this._webio, this._args, searchMethod, xPath);
895+
return new UIElement(element, this._driver, this._wd, this._webio, this._args, searchMethod, xPath);
896896
} else {
897897
return undefined;
898898
}

lib/ui-element.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export declare class UIElement {
110110
*/
111111
scrollTo(direction: Direction, elementToSearch: () => Promise<UIElement>, yOffset?: number, xOffset?: number, retries?: number): Promise<UIElement>;
112112
/**
113-
* Scroll with offset from element with minimum inertia
113+
* Drag element with specific offset
114114
* @param direction
115115
* @param yOffset
116-
* @param xOffset
116+
* @param xOffset - default value 0
117117
*/
118118
drag(direction: Direction, yOffset: number, xOffset?: number): Promise<void>;
119119
/**

lib/ui-element.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,23 @@ export class UIElement {
162162
*/
163163
public async element() {
164164
this._element = await this.refetch();
165-
return await this._element;
165+
return this._element;
166166
}
167167

168168
/**
169169
* Shows if element is displayed. Returns true or false. If the element doesn't exist it will return false
170170
*/
171171
public async isDisplayed() {
172172
const displaySize = await this._driver.getWindowSize();
173-
const el = (await this.element());
173+
const el = this._element;
174174
let isDisplayed = true;
175175
if (!el || el === null) {
176176
return false;
177177
}
178-
179178
try {
180-
const elemCoordinates = await this.getRectangle();
181-
isDisplayed = (await el.isDisplayed()) && elemCoordinates.x >= 0 && elemCoordinates.x < displaySize.width
179+
const elemCoordinates = await el.getLocation();
180+
const isDisplayedWebDriver = await el.isDisplayed();
181+
isDisplayed = isDisplayedWebDriver && elemCoordinates.x >= 0 && elemCoordinates.x < displaySize.width
182182
&& elemCoordinates.y >= 0 && elemCoordinates.y < displaySize.height;
183183
} catch (error) {
184184
console.log("ERROR: " + error);
@@ -322,10 +322,10 @@ export class UIElement {
322322
}
323323

324324
/**
325-
* Scroll with offset from element with minimum inertia
325+
* Drag element with specific offset
326326
* @param direction
327327
* @param yOffset
328-
* @param xOffset
328+
* @param xOffset - default value 0
329329
*/
330330
public async drag(direction: Direction, yOffset: number, xOffset: number = 0) {
331331
const location = await this.location();
@@ -455,7 +455,7 @@ export class UIElement {
455455
if (this._args.isAndroid) {
456456
const action = new this._wd.TouchAction(this._driver);
457457
action.press({ x: centerX, y: centerY })
458-
.wait(100)
458+
.wait(200)
459459
.moveTo({ x: swipeX, y: centerY })
460460
.release();
461461
await action.perform();

lib/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ export function getStorageByDeviceName(args: INsCapabilities) {
213213
if (args.imagesPath) {
214214
const segments = args.imagesPath.split(/[\/\\]+/);
215215
storage = join(storage, segments.join(sep));
216-
return storage;
216+
if (existsSync(storage)) {
217+
return storage;
218+
} else {
219+
const error = `Current imagesPath (${args.imagesPath}) does not exist !!!`;
220+
logError(error)
221+
throw new Error(error);
222+
}
217223
}
218224
const appName = resolveSauceLabAppName(getAppName(args));
219225
storage = createStorageFolder(storage, appName);

0 commit comments

Comments
 (0)