Created
July 27, 2023 07:23
-
-
Save Jalson1982/73e469d7c15dac753f10154c72b64f91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable jest/no-jasmine-globals */ | |
/* eslint-disable no-undef */ | |
// eslint-disable-next-line import/no-extraneous-dependencies | |
import wd from 'wd'; | |
import drivers from './drivers'; | |
jest.setTimeout(300000); | |
const PORT = 4723; | |
const driver = drivers.ios(wd.promiseChainRemote('localhost', PORT)); | |
const customCommands = { | |
type: async (accessibilityLabel, text) => { | |
const element = await driver.elementByAccessibilityId(accessibilityLabel); | |
await element.type(text); | |
}, | |
click: async (accessibilityLabel, coordinates) => { | |
const element = await driver.elementByAccessibilityId(accessibilityLabel); | |
await element.click(coordinates); | |
}, | |
expectElement: async (accessibilityLabel, milliseconds, failFn = fail) => { | |
if (milliseconds < 0) { | |
return failFn(`Could not find element ${accessibilityLabel}`); | |
} | |
if (await driver.hasElementByAccessibilityId(accessibilityLabel)) { | |
return; | |
} | |
await driver.sleep(milliseconds); | |
await driver.expectElement(accessibilityLabel, milliseconds - 200, failFn); | |
}, | |
expectAndClickElement: async (accessibilityLabel, waitingTime) => { | |
await driver.expectElement(accessibilityLabel, waitingTime); | |
await driver.click(accessibilityLabel); | |
}, | |
logout: async () => { | |
await driver.expectElement('home_feed_screen', 1000, () => {}); | |
if (await driver.hasElementByAccessibilityId('close_button_news')) { | |
await driver.click('close_button_news'); | |
} | |
if (await driver.hasElementByAccessibilityId('home_feed_screen')) { | |
await driver.click('TimelineStack_tab_button'); | |
await driver.expectElement('settings_button', 2000); | |
await driver.click('settings_button'); | |
await driver.expectElement('account_settings', 2000); | |
await driver.click('account_settings'); | |
await driver.expectElement('logout_button', 2000); | |
await driver.click('logout_button'); | |
} | |
}, | |
scrollDown: (distance = 200) => { | |
const initialY = 200; | |
const finalY = (initialY - distance); | |
return (new wd.TouchAction(driver)) | |
.longPress({ | |
x: 10, | |
y: initialY, | |
}) | |
.moveTo({ | |
x: 10, | |
y: finalY < 0 ? 0 : finalY, | |
}) | |
.release() | |
.perform(); | |
}, | |
scrollUp: (distance = 200) => { | |
const initialY = 200; | |
const finalY = (initialY - distance); | |
return (new wd.TouchAction(driver)) | |
.longPress({ | |
x: 10, | |
y: initialY, | |
}) | |
.moveTo({ | |
x: 10, | |
y: finalY < 0 ? 0 : finalY, | |
}) | |
.release() | |
.perform(); | |
}, | |
scrollDownToExpectedElement: async (accessibilityLabel, distance = 200, delay = 500) => { | |
let foundElement = await driver.hasElementByAccessibilityId(accessibilityLabel, delay); | |
while (!foundElement) { | |
// eslint-disable-next-line no-await-in-loop | |
const hasElement = await driver.hasElementByAccessibilityId(accessibilityLabel, delay); | |
if (hasElement) { | |
// eslint-disable-next-line no-await-in-loop | |
const element = await driver.elementById(accessibilityLabel); | |
// eslint-disable-next-line no-await-in-loop | |
const isDisplayed = await element.isDisplayed(); | |
// eslint-disable-next-line no-await-in-loop | |
const isEnabled = await element.isEnabled(); | |
if (isDisplayed && isEnabled) { foundElement = true; } | |
} | |
// eslint-disable-next-line no-await-in-loop | |
await driver.scrollDown(distance); | |
} | |
}, | |
scrollUp: (distance = 800) => (new wd.TouchAction(driver)) | |
.longPress({ | |
x: 10, | |
y: 250, | |
}) | |
.moveTo({ | |
x: 10, | |
y: distance, | |
}) | |
.release() | |
.perform(), | |
hideDeviceKeyboardNoFail: async () => { | |
try { | |
await driver.hideDeviceKeyboard(); | |
} catch (e) { | |
console.warn('Hiding device keyboard failed, scrolling down'); | |
await driver.scrollDown(); | |
await driver.sleep(200); | |
} | |
}, | |
}; | |
Object.entries(customCommands).forEach(([customCommand, implementation]) => { | |
wd.addPromiseChainMethod(customCommand, implementation); | |
}); | |
export { | |
driver, | |
}; | |
// eslint-disable-next-line import/no-extraneous-dependencies | |
import wd from 'wd'; | |
import iosConfig from './ios.config'; | |
import androidConfig from './android.config'; | |
const addInitialize = (config) => (driver) => { | |
wd.addPromiseChainMethod('initialize', () => { | |
return driver.init(config); | |
}); | |
return driver; | |
}; | |
export default { | |
ios: addInitialize(iosConfig), | |
android: addInitialize(androidConfig), | |
}; | |
import { findLastUsedSimulatorApp } from './appFinder'; | |
export default { | |
platformName: 'iOS', | |
platformVersion: '14.4', | |
deviceName: 'iPhone Simulator', | |
app: findLastUsedSimulatorApp(), | |
newCommandTimeout: 3000, | |
}; | |
export default { | |
platformName: 'android', | |
platformVersion: '11', | |
deviceName: 'emulator-5554', | |
app: './android/app/build/outputs/apk/development/debug/app-development-debug.apk', | |
androidInstallTimeout: 300000, | |
avd: 'Pixel_3a_API_30_x86', | |
newCommandTimeout: 120000, | |
automationName: 'Espresso', | |
useKeystore: true, | |
keystorePath: '/Users/freekvandooren/code/woc-mobile/android/app/debug.keystore', | |
keystorePassword: 'android', | |
keyAlias: 'androiddebugkey', | |
keyPassword: 'android', | |
additionalAppDependencies: [ | |
'androidx.lifecycle:lifecycle-extensions:2.2.0', | |
], | |
} | |
import Os from 'os'; | |
import fs from 'fs'; | |
// eslint-disable-next-line import/no-extraneous-dependencies | |
import glob from 'glob'; | |
export const findLastUsedSimulatorApp = () => { | |
const home = Os.homedir(); | |
const appFiles = glob.sync(`${home}/Library/Developer/Xcode/DerivedData/wocmobile*/Build/Products/*simulator/wocmobile-development.app`); | |
const lastAccessedAppBinary = appFiles | |
.map(filePath => [fs.statSync(filePath), filePath]) | |
.sort(([{ atime: accessTimeA }], [{ atime: accessTimeB }]) => accessTimeB - accessTimeA) | |
.map(([, filePath]) => filePath)[0]; | |
return lastAccessedAppBinary; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment