11'use strict' ;
22
3- const _ = require ( 'lodash' ) ;
43const { withExisting, withNativeCtx, withTestCtxMemo} = require ( '../decorators' ) ;
54const { WEB_VIEW_SIZE , BOTTOM_TOOLBAR_LOCATION , PIXEL_RATIO } = require ( '../../test-context' ) ;
6- const { isWdioLatest} = require ( '../../../utils' ) ;
75
86module . exports = class CommonUtils {
97 constructor ( nativeLocators ) {
108 this . _nativeLocators = nativeLocators ;
119 }
1210
11+ async getLocation ( browser , selector ) {
12+ const elem = await browser . $ ( selector ) ;
13+ return elem . getLocation ( ) ;
14+ }
15+
16+ async getElementSize ( browser , selector ) {
17+ const elem = await browser . $ ( selector ) ;
18+ return elem . getSize ( ) ;
19+ }
20+
1321 async getBottomToolbarY ( browser ) {
1422 const { BOTTOM_TOOLBAR } = this . _nativeLocators ;
15- const action = { fn : browser . getLocation , args : BOTTOM_TOOLBAR , default : { x : 0 , y : 0 } } ;
23+ const action = { fn : this . getLocation , args : [ browser , BOTTOM_TOOLBAR ] , default : { x : 0 , y : 0 } } ;
1624 const existingWrapper = { fn : withExisting , args : action } ;
1725 const inNativeCtxWrapper = { fn : withNativeCtx , args : existingWrapper } ;
1826
1927 return ( await withTestCtxMemo . call ( browser , inNativeCtxWrapper , BOTTOM_TOOLBAR_LOCATION ) ) . y ;
2028 }
2129
22- async getWebViewSize ( browser ) {
30+ getWebViewSize ( browser ) {
2331 const { WEB_VIEW } = this . _nativeLocators ;
24- const action = { fn : browser . getElementSize , args : WEB_VIEW } ;
32+ const action = { fn : this . getElementSize , args : [ browser , WEB_VIEW ] } ;
2533 const inNativeCtxWrapper = { fn : withNativeCtx , args : action } ;
2634
27- return await withTestCtxMemo . call ( browser , inNativeCtxWrapper , WEB_VIEW_SIZE ) ;
35+ return withTestCtxMemo . call ( browser , inNativeCtxWrapper , WEB_VIEW_SIZE ) ;
2836 }
2937
3038 async getElemCoords ( browser , selector ) {
31- const [ size , location ] = await Promise . all ( [ browser . getElementSize ( selector ) , browser . getLocation ( selector ) ] ) ;
32- const { width, height} = _ . isArray ( size ) ? size [ 0 ] : size ;
33- // wdio returns elements in reverse order, so we need to take the last element in the array to pick first element on the page
34- // https://github.com/webdriverio/webdriverio/blob/v4.14.1/lib/commands/getLocation.js#L48.
35- const { x, y} = _ . isArray ( location ) ? location [ location . length - 1 ] : location ;
39+ const [ size , location ] = await Promise . all ( [ this . getElementSize ( browser , selector ) , this . getLocation ( browser , selector ) ] ) ;
40+ const { width, height} = size ;
41+ const { x, y} = location ;
3642
3743 const topToolbarHeight = await this . getTopToolbarHeight ( browser ) ;
3844
@@ -48,14 +54,12 @@ module.exports = class CommonUtils {
4854 } ;
4955 }
5056
51- async getPixelRatio ( browser ) {
52- const action = { fn : async ( ) => {
53- const result = await browser . execute ( ( ) => window . devicePixelRatio ) ;
54-
55- return isWdioLatest ( browser ) ? result : result . value ;
57+ getPixelRatio ( browser ) {
58+ const action = { fn : ( ) => {
59+ return browser . execute ( ( ) => window . devicePixelRatio ) ;
5660 } } ;
5761
58- return await withTestCtxMemo . call ( browser , action , PIXEL_RATIO ) ;
62+ return withTestCtxMemo . call ( browser , action , PIXEL_RATIO ) ;
5963 }
6064
6165 async calcWebViewCoords ( browser , { bodyWidth, pixelRatio = 1 } = { } ) {
0 commit comments