1
1
'use strict' ;
2
2
3
- const _ = require ( 'lodash' ) ;
4
3
const { withExisting, withNativeCtx, withTestCtxMemo} = require ( '../decorators' ) ;
5
4
const { WEB_VIEW_SIZE , BOTTOM_TOOLBAR_LOCATION , PIXEL_RATIO } = require ( '../../test-context' ) ;
6
- const { isWdioLatest} = require ( '../../../utils' ) ;
7
5
8
6
module . exports = class CommonUtils {
9
7
constructor ( nativeLocators ) {
10
8
this . _nativeLocators = nativeLocators ;
11
9
}
12
10
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
+
13
21
async getBottomToolbarY ( browser ) {
14
22
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 } } ;
16
24
const existingWrapper = { fn : withExisting , args : action } ;
17
25
const inNativeCtxWrapper = { fn : withNativeCtx , args : existingWrapper } ;
18
26
19
27
return ( await withTestCtxMemo . call ( browser , inNativeCtxWrapper , BOTTOM_TOOLBAR_LOCATION ) ) . y ;
20
28
}
21
29
22
- async getWebViewSize ( browser ) {
30
+ getWebViewSize ( browser ) {
23
31
const { WEB_VIEW } = this . _nativeLocators ;
24
- const action = { fn : browser . getElementSize , args : WEB_VIEW } ;
32
+ const action = { fn : this . getElementSize , args : [ browser , WEB_VIEW ] } ;
25
33
const inNativeCtxWrapper = { fn : withNativeCtx , args : action } ;
26
34
27
- return await withTestCtxMemo . call ( browser , inNativeCtxWrapper , WEB_VIEW_SIZE ) ;
35
+ return withTestCtxMemo . call ( browser , inNativeCtxWrapper , WEB_VIEW_SIZE ) ;
28
36
}
29
37
30
38
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 ;
36
42
37
43
const topToolbarHeight = await this . getTopToolbarHeight ( browser ) ;
38
44
@@ -48,14 +54,12 @@ module.exports = class CommonUtils {
48
54
} ;
49
55
}
50
56
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 ) ;
56
60
} } ;
57
61
58
- return await withTestCtxMemo . call ( browser , action , PIXEL_RATIO ) ;
62
+ return withTestCtxMemo . call ( browser , action , PIXEL_RATIO ) ;
59
63
}
60
64
61
65
async calcWebViewCoords ( browser , { bodyWidth, pixelRatio = 1 } = { } ) {
0 commit comments