microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/smoke/package/src/debugAndroid.test.ts
247lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as assert from "assert"; |
| 5 | import * as path from "path"; |
| 6 | import { AppiumHelper, Platform, AppiumClient } from "./helpers/appiumHelper"; |
| 7 | import { AndroidEmulatorHelper } from "./helpers/androidEmulatorHelper"; |
| 8 | import { sleep, findStringInFile, findExpoURLInLogFile, ExpoLaunch, findExpoSuccessAndFailurePatterns } from "./helpers/utilities"; |
| 9 | import { SmokeTestsConstants } from "./helpers/smokeTestsConstants"; |
| 10 | import { ExpoWorkspacePath, pureRNWorkspacePath, RNworkspacePath, prepareReactNativeProjectForHermesTesting, runVSCode } from "./main"; |
| 11 | import { SetupEnvironmentHelper } from "./helpers/setupEnvironmentHelper"; |
| 12 | import { TestRunArguments } from "./helpers/configHelper"; |
| 13 | import { Application } from "../../automation"; |
| 14 | |
| 15 | const RN_APP_PACKAGE_NAME = "com.latestrnapp"; |
| 16 | const RN_APP_ACTIVITY_NAME = "com.latestrnapp.MainActivity"; |
| 17 | const EXPO_APP_PACKAGE_NAME = SetupEnvironmentHelper.expoPackageName; |
| 18 | const EXPO_APP_ACTIVITY_NAME = `${EXPO_APP_PACKAGE_NAME}.experience.HomeActivity`; |
| 19 | const RNDebugConfigName = "Debug Android"; |
| 20 | const RNHermesDebugConfigName = "Debug Android (Hermes) - Experimental"; |
| 21 | const RNHermesAttachConfigName = "Attach to Hermes application - Experimental"; |
| 22 | const ExpoDebugConfigName = "Debug in Exponent"; |
| 23 | |
| 24 | const RNSetBreakpointOnLine = 1; |
| 25 | const RNHermesSetBreakpointOnLine = 11; |
| 26 | const ExpoSetBreakpointOnLine = 1; |
| 27 | const PureRNExpoSetBreakpointOnLine = 1; |
| 28 | // Time for Android Debug Test before it reaches timeout |
| 29 | const debugAndroidTestTime = SmokeTestsConstants.androidAppBuildAndInstallTimeout + 100 * 1000; |
| 30 | // Time for Android Expo Debug Test before it reaches timeout |
| 31 | const debugExpoTestTime = SmokeTestsConstants.expoAppBuildAndInstallTimeout + 400 * 1000; |
| 32 | |
| 33 | export function setup(testParameters?: TestRunArguments) { |
| 34 | describe("Debugging Android", () => { |
| 35 | let app: Application; |
| 36 | let clientInited: AppiumClient; |
| 37 | |
| 38 | afterEach(async () => { |
| 39 | if (app) { |
| 40 | await app.stop(); |
| 41 | } |
| 42 | if (clientInited) { |
| 43 | clientInited.closeApp(); |
| 44 | clientInited.endAll(); |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | it("RN app Debug test", async function () { |
| 49 | this.timeout(debugAndroidTestTime); |
| 50 | app = await runVSCode(RNworkspacePath); |
| 51 | await app.workbench.explorer.openExplorerView(); |
| 52 | await app.workbench.explorer.openFile("App.js"); |
| 53 | await app.workbench.editors.scrollTop(); |
| 54 | console.log("Android Debug test: App.js file is opened"); |
| 55 | await app.workbench.debug.setBreakpointOnLine(RNSetBreakpointOnLine); |
| 56 | console.log(`Android Debug test: Breakpoint is set on line ${RNSetBreakpointOnLine}`); |
| 57 | await app.workbench.debug.openDebugViewlet(); |
| 58 | console.log(`Android Debug test: Chosen debug configuration: ${RNDebugConfigName}`); |
| 59 | console.log("Android Debug test: Starting debugging"); |
| 60 | await app.workbench.debug.runDebugScenario(RNDebugConfigName); |
| 61 | const opts = AppiumHelper.prepareAttachOptsForAndroidActivity(RN_APP_PACKAGE_NAME, RN_APP_ACTIVITY_NAME, AndroidEmulatorHelper.androidEmulatorName); |
| 62 | await AndroidEmulatorHelper.checkIfAppIsInstalled(RN_APP_PACKAGE_NAME, SmokeTestsConstants.androidAppBuildAndInstallTimeout); |
| 63 | let client = AppiumHelper.webdriverAttach(opts); |
| 64 | clientInited = client.init(); |
| 65 | await AppiumHelper.enableRemoteDebugJS(clientInited, Platform.Android); |
| 66 | await app.workbench.debug.waitForDebuggingToStart(); |
| 67 | console.log("Android Debug test: Debugging started"); |
| 68 | await app.workbench.debug.waitForStackFrame(sf => sf.name === "App.js" && sf.lineNumber === RNSetBreakpointOnLine, `looking for App.js and line ${RNSetBreakpointOnLine}`); |
| 69 | console.log("Android Debug test: Stack frame found"); |
| 70 | await app.workbench.debug.stepOver(); |
| 71 | // await for our debug string renders in debug console |
| 72 | await sleep(SmokeTestsConstants.debugConsoleSearchTimeout); |
| 73 | console.log("Android Debug test: Searching for \"Test output from debuggee\" string in console"); |
| 74 | let found = await app.workbench.debug.waitForOutput(output => output.some(line => line.indexOf("Test output from debuggee") >= 0)); |
| 75 | console.log(found); |
| 76 | assert.notStrictEqual(found, false, "\"Test output from debuggee\" string is missing in debug console"); |
| 77 | console.log("Android Debug test: \"Test output from debuggee\" string is found"); |
| 78 | await app.workbench.debug.stopDebugging(); |
| 79 | console.log("Android Debug test: Debugging is stopped"); |
| 80 | }); |
| 81 | |
| 82 | it("Hermes RN app Debug test", async function () { |
| 83 | this.timeout(debugAndroidTestTime); |
| 84 | prepareReactNativeProjectForHermesTesting(); |
| 85 | AndroidEmulatorHelper.uninstallTestAppFromEmulator(RN_APP_PACKAGE_NAME); |
| 86 | app = await runVSCode(RNworkspacePath); |
| 87 | await app.workbench.explorer.openExplorerView(); |
| 88 | await app.workbench.explorer.openFile("AppTestButton.js"); |
| 89 | await app.workbench.editors.scrollTop(); |
| 90 | console.log("Android Debug Hermes test: AppTestButton.js file is opened"); |
| 91 | await app.workbench.debug.setBreakpointOnLine(RNHermesSetBreakpointOnLine); |
| 92 | console.log(`Android Debug Hermes test: Breakpoint is set on line ${RNHermesSetBreakpointOnLine}`); |
| 93 | await app.workbench.debug.openDebugViewlet(); |
| 94 | console.log(`Android Debug Hermes test: Debug Viewlet opened`); |
| 95 | console.log(`Android Debug Hermes test: Chosen debug configuration: ${RNHermesDebugConfigName}`); |
| 96 | console.log("Android Debug Hermes test: Starting debugging"); |
| 97 | await app.workbench.debug.runDebugScenario(RNHermesDebugConfigName); |
| 98 | const opts = AppiumHelper.prepareAttachOptsForAndroidActivity(RN_APP_PACKAGE_NAME, RN_APP_ACTIVITY_NAME, AndroidEmulatorHelper.androidEmulatorName); |
| 99 | await AndroidEmulatorHelper.checkIfAppIsInstalled(RN_APP_PACKAGE_NAME, SmokeTestsConstants.androidAppBuildAndInstallTimeout); |
| 100 | let client = AppiumHelper.webdriverAttach(opts); |
| 101 | clientInited = client.init(); |
| 102 | await app.workbench.debug.waitForDebuggingToStart(); |
| 103 | console.log("Android Debug Hermes test: Debugging started"); |
| 104 | console.log("Android Debug Hermes test: Checking for Hermes mark"); |
| 105 | let isHermesWorking = await AppiumHelper.isHermesWorking(clientInited); |
| 106 | assert.equal(isHermesWorking, true); |
| 107 | console.log("Android Debug Hermes test: Reattaching to Hermes app"); |
| 108 | await app.workbench.debug.stopDebugging(); |
| 109 | await app.workbench.debug.runDebugScenario(RNHermesAttachConfigName); |
| 110 | console.log("Android Debug Hermes test: Reattached successfully"); |
| 111 | await sleep(7000); |
| 112 | console.log("Android Debug Hermes test: Click Test Button"); |
| 113 | await AppiumHelper.clickTestButtonHermes(clientInited); |
| 114 | await app.workbench.debug.waitForStackFrame(sf => sf.name === "AppTestButton.js" && sf.lineNumber === RNHermesSetBreakpointOnLine, `looking for AppTestButton.js and line ${RNHermesSetBreakpointOnLine}`); |
| 115 | console.log("Android Debug Hermes test: Stack frame found"); |
| 116 | await app.workbench.debug.continue(); |
| 117 | // await for our debug string renders in debug console |
| 118 | await sleep(SmokeTestsConstants.debugConsoleSearchTimeout); |
| 119 | if (process.env.REACT_NATIVE_TOOLS_LOGS_DIR) { |
| 120 | console.log("Android Debug Hermes test: Searching for \"Test output from Hermes debuggee\" string in output file"); |
| 121 | let found = findStringInFile(path.join(process.env.REACT_NATIVE_TOOLS_LOGS_DIR, SmokeTestsConstants.ChromeDebugCoreLogFileName), "Test output from Hermes debuggee"); |
| 122 | assert.notStrictEqual(found, false, "\"Test output from Hermes debuggee\" string is missing in output file"); |
| 123 | console.log("Android Debug test: \"Test output from Hermes debuggee\" string is found"); |
| 124 | } |
| 125 | await app.workbench.debug.disconnectFromDebugger(); |
| 126 | console.log("Android Debug Hermes test: Debugging is stopped"); |
| 127 | }); |
| 128 | |
| 129 | it("Expo app Debug test", async function () { |
| 130 | if (testParameters && testParameters.RunBasicTests) { |
| 131 | this.skip(); |
| 132 | } |
| 133 | this.timeout(debugExpoTestTime); |
| 134 | app = await runVSCode(ExpoWorkspacePath); |
| 135 | console.log(`Android Expo Debug test: ${ExpoWorkspacePath} directory is opened in VS Code`); |
| 136 | await app.workbench.explorer.openExplorerView(); |
| 137 | await app.workbench.explorer.openFile("App.js"); |
| 138 | await app.workbench.editors.scrollTop(); |
| 139 | console.log("Android Expo Debug test: App.js file is opened"); |
| 140 | await app.workbench.debug.setBreakpointOnLine(ExpoSetBreakpointOnLine); |
| 141 | console.log(`Android Expo Debug test: Breakpoint is set on line ${ExpoSetBreakpointOnLine}`); |
| 142 | await app.workbench.debug.openDebugViewlet(); |
| 143 | console.log(`Android Expo Debug test: Chosen debug configuration: ${ExpoDebugConfigName}`); |
| 144 | console.log("Android Expo Debug test: Starting debugging"); |
| 145 | await app.workbench.debug.runDebugScenario(ExpoDebugConfigName); |
| 146 | if (process.env.REACT_NATIVE_TOOLS_LOGS_DIR) { |
| 147 | let expoLaunchStatus: ExpoLaunch; |
| 148 | expoLaunchStatus = await findExpoSuccessAndFailurePatterns(path.join(process.env.REACT_NATIVE_TOOLS_LOGS_DIR, SmokeTestsConstants.ReactNativeLogFileName), SmokeTestsConstants.ExpoSuccessPattern, SmokeTestsConstants.ExpoFailurePattern); |
| 149 | if (expoLaunchStatus.failed) { |
| 150 | console.log("First attempt to start failed, retrying..."); |
| 151 | await app.workbench.debug.runDebugScenario(ExpoDebugConfigName); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | await app.workbench.editors.waitForTab("Expo QR Code"); |
| 156 | await app.workbench.editors.waitForActiveTab("Expo QR Code"); |
| 157 | console.log("Android Expo Debug test: 'Expo QR Code' tab found"); |
| 158 | |
| 159 | let expoURL; |
| 160 | if (process.env.REACT_NATIVE_TOOLS_LOGS_DIR) { |
| 161 | expoURL = findExpoURLInLogFile(path.join(process.env.REACT_NATIVE_TOOLS_LOGS_DIR, SmokeTestsConstants.ReactNativeRunExpoLogFileName)); |
| 162 | } |
| 163 | assert.notStrictEqual(expoURL, null, "Expo URL pattern is not found"); |
| 164 | expoURL = expoURL as string; |
| 165 | const opts = AppiumHelper.prepareAttachOptsForAndroidActivity(EXPO_APP_PACKAGE_NAME, EXPO_APP_ACTIVITY_NAME, AndroidEmulatorHelper.androidEmulatorName); |
| 166 | let client = AppiumHelper.webdriverAttach(opts); |
| 167 | clientInited = client.init(); |
| 168 | // TODO Add listener to trigger that main expo app has been ran |
| 169 | await AppiumHelper.openExpoApplication(Platform.Android, clientInited, expoURL, ExpoWorkspacePath); |
| 170 | // TODO Add listener to trigger that child expo app has been ran instead of using timeout |
| 171 | console.log(`Android Expo Debug test: Waiting ${SmokeTestsConstants.expoAppBuildAndInstallTimeout}ms until Expo app is ready...`); |
| 172 | await sleep(SmokeTestsConstants.expoAppBuildAndInstallTimeout); |
| 173 | await AppiumHelper.enableRemoteDebugJS(clientInited, Platform.Android); |
| 174 | await app.workbench.debug.waitForDebuggingToStart(); |
| 175 | console.log("Android Expo Debug test: Debugging started"); |
| 176 | await app.workbench.debug.waitForStackFrame(sf => sf.name === "App.js" && sf.lineNumber === ExpoSetBreakpointOnLine, `looking for App.js and line ${ExpoSetBreakpointOnLine}`); |
| 177 | console.log("Android Expo Debug test: Stack frame found"); |
| 178 | await app.workbench.debug.stepOver(); |
| 179 | // Wait for debug string to be rendered in debug console |
| 180 | await sleep(SmokeTestsConstants.debugConsoleSearchTimeout); |
| 181 | console.log("Android Expo Debug test: Searching for \"Test output from debuggee\" string in console"); |
| 182 | let found = await app.workbench.debug.waitForOutput(output => output.some(line => line.indexOf("Test output from debuggee") >= 0)); |
| 183 | assert.notStrictEqual(found, false, "\"Test output from debuggee\" string is missing in debug console"); |
| 184 | console.log("Android Expo Debug test: \"Test output from debuggee\" string is found"); |
| 185 | await app.workbench.debug.stopDebugging(); |
| 186 | console.log("Android Expo Debug test: Debugging is stopped"); |
| 187 | }); |
| 188 | |
| 189 | it("Pure RN app Expo test", async function () { |
| 190 | if (testParameters && testParameters.RunBasicTests) { |
| 191 | this.skip(); |
| 192 | } |
| 193 | this.timeout(debugExpoTestTime); |
| 194 | app = await runVSCode(pureRNWorkspacePath); |
| 195 | console.log(`Android pure RN Expo test: ${pureRNWorkspacePath} directory is opened in VS Code`); |
| 196 | await app.workbench.explorer.openExplorerView(); |
| 197 | await app.workbench.explorer.openFile("App.js"); |
| 198 | await app.workbench.editors.scrollTop(); |
| 199 | console.log("Android pure RN Expo test: App.js file is opened"); |
| 200 | await app.workbench.debug.setBreakpointOnLine(PureRNExpoSetBreakpointOnLine); |
| 201 | console.log(`Android pure RN Expo test: Breakpoint is set on line ${PureRNExpoSetBreakpointOnLine}`); |
| 202 | await app.workbench.debug.openDebugViewlet(); |
| 203 | console.log(`Android pure RN Expo test: Chosen debug configuration: ${ExpoDebugConfigName}`); |
| 204 | console.log("Android pure RN Expo test: Starting debugging"); |
| 205 | await app.workbench.debug.runDebugScenario(ExpoDebugConfigName); |
| 206 | if (process.env.REACT_NATIVE_TOOLS_LOGS_DIR) { |
| 207 | let expoLaunchStatus: ExpoLaunch; |
| 208 | expoLaunchStatus = await findExpoSuccessAndFailurePatterns(path.join(process.env.REACT_NATIVE_TOOLS_LOGS_DIR, SmokeTestsConstants.ReactNativeLogFileName), SmokeTestsConstants.ExpoSuccessPattern, SmokeTestsConstants.ExpoFailurePattern); |
| 209 | if (expoLaunchStatus.failed) { |
| 210 | console.log("First attempt to start failed, retrying..."); |
| 211 | await app.workbench.debug.runDebugScenario(ExpoDebugConfigName); |
| 212 | } |
| 213 | } |
| 214 | await app.workbench.editors.waitForTab("Expo QR Code"); |
| 215 | await app.workbench.editors.waitForActiveTab("Expo QR Code"); |
| 216 | console.log("Android pure RN Expo test: 'Expo QR Code' tab found"); |
| 217 | |
| 218 | let expoURL; |
| 219 | if (process.env.REACT_NATIVE_TOOLS_LOGS_DIR) { |
| 220 | expoURL = findExpoURLInLogFile(path.join(process.env.REACT_NATIVE_TOOLS_LOGS_DIR, SmokeTestsConstants.ReactNativeRunExpoLogFileName)); |
| 221 | } |
| 222 | |
| 223 | assert.notStrictEqual(expoURL, null, "Expo URL pattern is not found"); |
| 224 | expoURL = expoURL as string; |
| 225 | const opts = AppiumHelper.prepareAttachOptsForAndroidActivity(EXPO_APP_PACKAGE_NAME, EXPO_APP_ACTIVITY_NAME, AndroidEmulatorHelper.androidEmulatorName); |
| 226 | let client = AppiumHelper.webdriverAttach(opts); |
| 227 | clientInited = client.init(); |
| 228 | await AppiumHelper.openExpoApplication(Platform.Android, clientInited, expoURL, pureRNWorkspacePath); |
| 229 | console.log(`Android pure RN Expo test: Waiting ${SmokeTestsConstants.expoAppBuildAndInstallTimeout}ms until Expo app is ready...`); |
| 230 | await sleep(SmokeTestsConstants.expoAppBuildAndInstallTimeout); |
| 231 | await AppiumHelper.enableRemoteDebugJS(clientInited, Platform.Android); |
| 232 | await app.workbench.debug.waitForDebuggingToStart(); |
| 233 | console.log("Android pure RN Expo test: Debugging started"); |
| 234 | await app.workbench.debug.waitForStackFrame(sf => sf.name === "App.js" && sf.lineNumber === PureRNExpoSetBreakpointOnLine, `looking for App.js and line ${PureRNExpoSetBreakpointOnLine}`); |
| 235 | console.log("Android pure RN Expo test: Stack frame found"); |
| 236 | await app.workbench.debug.stepOver(); |
| 237 | // Wait for debug string to be rendered in debug console |
| 238 | await sleep(SmokeTestsConstants.debugConsoleSearchTimeout); |
| 239 | console.log("Android pure RN Expo test: Searching for \"Test output from debuggee\" string in console"); |
| 240 | let found = await app.workbench.debug.waitForOutput(output => output.some(line => line.indexOf("Test output from debuggee") >= 0)); |
| 241 | assert.notStrictEqual(found, false, "\"Test output from debuggee\" string is missing in debug console"); |
| 242 | console.log("Android pure RN Expo test: \"Test output from debuggee\" string is found"); |
| 243 | await app.workbench.debug.stopDebugging(); |
| 244 | console.log("Android pure RN Expo test: Debugging is stopped"); |
| 245 | }); |
| 246 | }); |
| 247 | } |
| 248 | |