about summary refs log tree commit diff
path: root/__e2e__/util.ts
blob: f5bb728151532440b4ce87a7ba5c8c87f019f8de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {resolveConfig} from 'detox/internals'
import {execSync} from 'child_process'

const platform = device.getPlatform()

export async function openApp(opts: any) {
  opts = opts || {}
  const config = await resolveConfig()

  if (device.getPlatform() === 'ios') {
    // disable password autofill
    execSync(
      `plutil -replace restrictedBool.allowPasswordAutoFill.value -bool NO ~/Library/Developer/CoreSimulator/Devices/${device.id}/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles/Library/ConfigurationProfiles/UserSettings.plist`,
    )
    execSync(
      `plutil -replace restrictedBool.allowPasswordAutoFill.value -bool NO ~/Library/Developer/CoreSimulator/Devices/${device.id}/data/Library/UserConfigurationProfiles/EffectiveUserSettings.plist`,
    )
    execSync(
      `plutil -replace restrictedBool.allowPasswordAutoFill.value -bool NO ~/Library/Developer/CoreSimulator/Devices/${device.id}/data/Library/UserConfigurationProfiles/PublicInfo/PublicEffectiveUserSettings.plist`,
    )
  }
  if (config.configurationName.split('.').includes('debug')) {
    return await openAppForDebugBuild(platform, opts)
  } else {
    return await device.launchApp({
      ...opts,
      newInstance: true,
    })
  }
}

export async function isVisible(id: string) {
  try {
    await expect(element(by.id(id))).toBeVisible()
    return true
  } catch (e) {
    return false
  }
}

export async function login(
  service: string,
  username: string,
  password: string,
  {takeScreenshots} = {takeScreenshots: false},
) {
  await element(by.id('signInButton')).tap()
  if (takeScreenshots) {
    await device.takeScreenshot('1- opened sign-in screen')
  }
  if (await isVisible('chooseAccountForm')) {
    await element(by.id('chooseNewAccountBtn')).tap()
  }
  await element(by.id('loginSelectServiceButton')).tap()
  if (takeScreenshots) {
    await device.takeScreenshot('2- opened service selector')
  }
  await element(by.id('customServerTextInput')).typeText(service)
  await element(by.id('customServerTextInput')).tapReturnKey()
  await element(by.id('customServerSelectBtn')).tap()
  if (takeScreenshots) {
    await device.takeScreenshot('3- input custom service')
  }
  await element(by.id('loginUsernameInput')).typeText(username)
  await element(by.id('loginPasswordInput')).typeText(password)
  if (takeScreenshots) {
    await device.takeScreenshot('4- entered username and password')
  }
  await element(by.id('loginNextButton')).tap()
}

async function openAppForDebugBuild(platform: string, opts: any) {
  const deepLinkUrl = // Local testing with packager
    /*process.env.EXPO_USE_UPDATES
    ? // Testing latest published EAS update for the test_debug channel
      getDeepLinkUrl(getLatestUpdateUrl())
    : */ getDeepLinkUrl(getDevLauncherPackagerUrl(platform))

  if (platform === 'ios') {
    await device.launchApp({
      ...opts,
      newInstance: true,
    })
    sleep(3000)
    await device.openURL({
      url: deepLinkUrl,
    })
  } else {
    await device.launchApp({
      ...opts,
      newInstance: true,
      url: deepLinkUrl,
    })
  }

  await sleep(3000)
}

export async function createServer(path = '') {
  const res = await fetch(`http://localhost:1986/${path}`, {method: 'POST'})
  const resBody = await res.text()
  return resBody
}

const getDeepLinkUrl = (url: string) =>
  `expo+bluesky://expo-development-client/?url=${encodeURIComponent(url)}`

const getDevLauncherPackagerUrl = (platform: string) =>
  `http://localhost:8081/index.bundle?platform=${platform}&dev=true&minify=false&disableOnboarding=1`

export const sleep = (t: number) => new Promise(res => setTimeout(res, t))