|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const _ = require('lodash'); |
4 | | -const {withExisting, withNativeCtx, withTestCtxMemo} = require('./decorators'); |
5 | | -const {TOP_TOOLBAR_SIZE, BOTTOM_TOOLBAR_LOCATION, WEB_VIEW_SIZE, PIXEL_RATIO} = require('../test-context'); |
6 | | -const {TOP_TOOLBAR, BOTTOM_TOOLBAR, WEB_VIEW} = require('../../native-locators'); |
7 | | -const {isWdioLatest} = require('../../utils'); |
| 3 | +const {getSafariVersion} = require('../../utils'); |
| 4 | +const {NEW_SAFARI_VERSION} = require('../../constants'); |
8 | 5 |
|
9 | | -exports.getTopToolbarHeight = async (browser) => { |
10 | | - const action = {fn: browser.getElementSize, args: TOP_TOOLBAR, default: {width: 0, height: 0}}; |
11 | | - const existingWrapper = {fn: withExisting, args: action}; |
12 | | - const inNativeCtxWrapper = {fn: withNativeCtx, args: existingWrapper}; |
| 6 | +const Safari15Utils = require('./v15.0'); |
| 7 | +const SafariOldUtils = require('./<v15.0'); |
13 | 8 |
|
14 | | - return (await withTestCtxMemo.call(browser, inNativeCtxWrapper, TOP_TOOLBAR_SIZE)).height; |
15 | | -}; |
16 | | - |
17 | | -exports.getBottomToolbarY = async (browser) => { |
18 | | - const action = {fn: browser.getLocation, args: BOTTOM_TOOLBAR, default: {x: 0, y: 0}}; |
19 | | - const existingWrapper = {fn: withExisting, args: action}; |
20 | | - const inNativeCtxWrapper = {fn: withNativeCtx, args: existingWrapper}; |
21 | | - |
22 | | - return (await withTestCtxMemo.call(browser, inNativeCtxWrapper, BOTTOM_TOOLBAR_LOCATION)).y; |
23 | | -}; |
24 | | - |
25 | | -exports.getWebViewSize = async (browser) => { |
26 | | - const action = {fn: browser.getElementSize, args: WEB_VIEW}; |
27 | | - const inNativeCtxWrapper = {fn: withNativeCtx, args: action}; |
28 | | - |
29 | | - return await withTestCtxMemo.call(browser, inNativeCtxWrapper, WEB_VIEW_SIZE); |
30 | | -}; |
31 | | - |
32 | | -exports.getElemCoords = async (browser, selector) => { |
33 | | - const [size, location] = await Promise.all([browser.getElementSize(selector), browser.getLocation(selector)]); |
34 | | - const {width, height} = _.isArray(size) ? size[0] : size; |
35 | | - // wdio returns elements in reverse order, so we need to take the last element in the array to pick first element on the page |
36 | | - // https://github.com/webdriverio/webdriverio/blob/v4.14.1/lib/commands/getLocation.js#L48. |
37 | | - const {x, y} = _.isArray(location) ? location[location.length - 1] : location; |
38 | | - |
39 | | - const topToolbarHeight = await exports.getTopToolbarHeight(browser); |
40 | | - |
41 | | - return {width, height, x, y: y + topToolbarHeight}; |
42 | | -}; |
43 | | - |
44 | | -exports.getElemCenterLocation = async (browser, selector) => { |
45 | | - const {width, height, x, y} = await exports.getElemCoords(browser, selector); |
46 | | - |
47 | | - return { |
48 | | - x: Math.round(x + width / 2), |
49 | | - y: Math.round(y + height / 2) |
50 | | - }; |
51 | | -}; |
52 | | - |
53 | | -exports.getPixelRatio = async (browser) => { |
54 | | - const action = {fn: async () => { |
55 | | - const result = await browser.execute(() => window.devicePixelRatio); |
56 | | - |
57 | | - return isWdioLatest(browser) ? result : result.value; |
58 | | - }}; |
59 | | - |
60 | | - return await withTestCtxMemo.call(browser, action, PIXEL_RATIO); |
61 | | -}; |
| 9 | +exports.getElementUtils = (broConfig, nativeLocators) => { |
| 10 | + const currentVersion = getSafariVersion(broConfig); |
62 | 11 |
|
63 | | -exports.calcWebViewCoords = async (browser, {bodyWidth, pixelRatio = 1} = {}) => { |
64 | | - const [topToolbarHeight, bottomToolbarY, webViewSize] = await Promise.all([ |
65 | | - exports.getTopToolbarHeight(browser), |
66 | | - exports.getBottomToolbarY(browser), |
67 | | - exports.getWebViewSize(browser) |
68 | | - ]); |
69 | | - const bottomToolbarHeight = bottomToolbarY > 0 ? webViewSize.height - bottomToolbarY : 0; |
| 12 | + if (currentVersion < NEW_SAFARI_VERSION) { |
| 13 | + return new SafariOldUtils(nativeLocators); |
| 14 | + } |
70 | 15 |
|
71 | | - return { |
72 | | - width: Math.ceil(Math.min(webViewSize.width, bodyWidth) * pixelRatio), |
73 | | - height: Math.ceil((webViewSize.height - topToolbarHeight - bottomToolbarHeight) * pixelRatio), |
74 | | - left: Math.max(Math.floor((webViewSize.width - bodyWidth) / 2 * pixelRatio), 0), |
75 | | - top: Math.max(Math.floor(topToolbarHeight * pixelRatio), 0) |
76 | | - }; |
| 16 | + return new Safari15Utils(nativeLocators); |
77 | 17 | }; |
0 commit comments