|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Used in run-ci-e2e-test.js and executed in Travis and Circle CI. |
| 5 | + * E2e test that verifies that init app can be installed, compiled, started and Hot Module reloading and Chrome debugging work. |
| 6 | + * For other examples of appium refer to: https://github.com/appium/sample-code/tree/master/sample-code/examples/node and |
| 7 | + * https://www.npmjs.com/package/wd-android |
| 8 | + * |
| 9 | + * |
| 10 | + * To set up: |
| 11 | + |
| 12 | + * - cp <this file> <to app installation path> |
| 13 | + * - keytool -genkey -v -keystore android/keystores/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US |
| 14 | + * |
| 15 | + * To run this test: |
| 16 | + * - npm start |
| 17 | + * - node node_modules/.bin/appium |
| 18 | + * - (cd android && ./gradlew :app:copyDownloadableDepsToLibs) |
| 19 | + * - buck build android/app |
| 20 | + * - node ../node_modules/.bin/_mocha ../android-e2e-test.js |
| 21 | + */ |
| 22 | + |
| 23 | +const wd = require('wd'); |
| 24 | +const path = require('path'); |
| 25 | +const fs = require('fs'); |
| 26 | +const pd = require('pretty-data2').pd; |
| 27 | +require('colors'); |
| 28 | +// value in ms to print out screen contents, set this value in CI to debug if tests are failing |
| 29 | +const appiumDebugInterval = process.env.APPIUM_DEBUG_INTERVAL; |
| 30 | + |
| 31 | +describe('Android Test App', function () { |
| 32 | + this.timeout(600000); |
| 33 | + let driver; |
| 34 | + let debugIntervalId; |
| 35 | + |
| 36 | + before(function () { |
| 37 | + driver = wd.promiseChainRemote({ |
| 38 | + host: 'localhost', |
| 39 | + port: 4723 |
| 40 | + }); |
| 41 | + driver.on('status', function (info) { |
| 42 | + console.log(info.cyan); |
| 43 | + }); |
| 44 | + driver.on('command', function (method, path, data) { |
| 45 | + if (path === 'source()' && data) { |
| 46 | + console.log(' > ' + method.yellow, 'Screen contents'.grey, '\n', pd.xml(data).yellow); |
| 47 | + } else { |
| 48 | + console.log(' > ' + method.yellow, path.grey, data || ''); |
| 49 | + } |
| 50 | + }); |
| 51 | + driver.on('http', function (method, path, data) { |
| 52 | + console.log(' > ' + method.magenta, path, (data || '').grey); |
| 53 | + }); |
| 54 | + |
| 55 | + // every interval print what is on the screen |
| 56 | + if (appiumDebugInterval) { |
| 57 | + debugIntervalId = setInterval(() => { |
| 58 | + // it driver.on('command') will log the screen contents |
| 59 | + driver.source(); |
| 60 | + }, appiumDebugInterval); |
| 61 | + } |
| 62 | + |
| 63 | + const desired = { |
| 64 | + platformName: 'Android', |
| 65 | + deviceName: 'Android Emulator', |
| 66 | + app: path.resolve('buck-out/gen/android/app/app.apk') |
| 67 | + }; |
| 68 | + |
| 69 | + // React Native in dev mode often starts with Red Box "Can't fibd variable __fbBatchedBridge..." |
| 70 | + // This is fixed by clicking Reload JS which will trigger a request to packager server |
| 71 | + return driver |
| 72 | + .init(desired) |
| 73 | + .setImplicitWaitTimeout(5000) |
| 74 | + .waitForElementByXPath('//android.widget.Button[@text="Reload JS"]'). |
| 75 | + then((elem) => { |
| 76 | + elem.click(); |
| 77 | + }, (err) => { |
| 78 | + // ignoring if Reload JS button can't be located |
| 79 | + }) |
| 80 | + .setImplicitWaitTimeout(150000); |
| 81 | + }); |
| 82 | + |
| 83 | + after(function () { |
| 84 | + if (debugIntervalId) { |
| 85 | + clearInterval(debugIntervalId); |
| 86 | + } |
| 87 | + return driver.quit(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should have Hot Module Reloading working', function () { |
| 91 | + const androidAppCode = fs.readFileSync('index.android.js', 'utf-8'); |
| 92 | + let intervalToUpdate; |
| 93 | + return driver |
| 94 | + .waitForElementByXPath('//android.widget.TextView[starts-with(@text, "Welcome to React Native!")]') |
| 95 | + // http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_MENU |
| 96 | + .pressDeviceKey(82) |
| 97 | + .elementByXPath('//android.widget.TextView[starts-with(@text, "Enable Hot Reloading")]') |
| 98 | + .click() |
| 99 | + .waitForElementByXPath('//android.widget.TextView[starts-with(@text, "Welcome to React Native!")]') |
| 100 | + .then(() => { |
| 101 | + let iteration = 0; |
| 102 | + // CI environment can be quite slow and we can't guarantee that it can consistently motice a file change |
| 103 | + // so we change the file every few seconds just in case |
| 104 | + intervalToUpdate = setInterval(() => { |
| 105 | + fs.writeFileSync('index.android.js', androidAppCode.replace('Welcome to React Native!', 'Welcome to React Native with HMR!' + iteration), 'utf-8'); |
| 106 | + }, 3000); |
| 107 | + }) |
| 108 | + .waitForElementByXPath('//android.widget.TextView[starts-with(@text, "Welcome to React Native with HMR!")]') |
| 109 | + .finally(() => { |
| 110 | + clearInterval(intervalToUpdate); |
| 111 | + fs.writeFileSync('index.android.js', androidAppCode, 'utf-8'); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + |
| 116 | + it('should have Debug In Chrome working', function () { |
| 117 | + const androidAppCode = fs.readFileSync('index.android.js', 'utf-8'); |
| 118 | + // http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_MENU |
| 119 | + return driver |
| 120 | + .waitForElementByXPath('//android.widget.TextView[starts-with(@text, "Welcome to React Native!")]') |
| 121 | + .pressDeviceKey(82) |
| 122 | + .elementByXPath('//android.widget.TextView[starts-with(@text, "Debug")]') |
| 123 | + .click() |
| 124 | + .waitForElementByXPath('//android.widget.TextView[starts-with(@text, "Welcome to React Native!")]'); |
| 125 | + }); |
| 126 | +}); |
0 commit comments