Skip to content

Commit 3c6dbec

Browse files
kelsetfacebook-github-bot
authored andcommitted
add RNTester-E2E: tests for iOS and Android via Appium, WDIO and Jest (#36267)
Summary: The motivation is to create an E2E testing solution for the RNTester app that can be used to flag when things break and the release crew can use to validate more of the codebase ahead of a release. This first PR wants to just showcase the main flow (how tests are made, where the E2E folder lives, how it's used by CircleCI) and then we can build on top of it with subsequent PRs, with the help of an umbrella issue to get the community involved in making more tests! This work is being done cooperation with Callstack developers (mateuszm22 , adzironman, and others) - reason why the branch [lives in a fork](https://github.com/mateuszm22/react-native/tree/k%2Bm/new-rn-tester-E2E). ### [Read the RFC for more details ](react-native-community/discussions-and-proposals#684) ### Extra notes We are still on WebDriverIO 7 because during the exploratory phase, WDIO was a bit problematic and the workarounds needed to make it work don't seem very nice - see this PR: kelset/react-native-e2e-jest-appium-webdriverio#4 --- this will be revisited in the future, once there's a better path for upgrading. ### Things that will be handled as follow up work * upgrade to WDIO 8 * [an automated yarn run-e2e-test script](#36267 (comment)) * [a small refactoring into a js script](#36267 (comment)) ## Changelog: [INTERNAL] [ADDED] - Added first working configuration for e2e testing Pull Request resolved: #36267 Test Plan: You can play with this locally by [follow the readme](https://github.com/mateuszm22/react-native/blob/k+m/new-rn-tester-E2E/packages/rn-tester-e2e/README.md). CircleCI jobs will be added. Reviewed By: cipolleschi Differential Revision: D47763012 Pulled By: cortinico fbshipit-source-id: 6eb9674182b8ee97aea4784158c69bf017f379e5
1 parent ad91518 commit 3c6dbec

File tree

16 files changed

+3786
-907
lines changed

16 files changed

+3786
-907
lines changed

.circleci/config.yml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version: 2.1
66

77
orbs:
88
win: circleci/[email protected]
9+
android: circleci/[email protected]
910

1011
# -------------------------
1112
# REFERENCES
@@ -724,6 +725,136 @@ jobs:
724725
path: ./reports/junit
725726

726727
# -------------------------
728+
# JOBS: iOS E2E Tests
729+
# -------------------------
730+
test_e2e_ios:
731+
executor: reactnativeios
732+
parameters:
733+
ruby_version:
734+
default: "2.7.7"
735+
description: The version of ruby that must be used
736+
type: string
737+
steps:
738+
- checkout_code_with_cache
739+
- run_yarn
740+
- attach_workspace:
741+
at: .
742+
- run:
743+
name: Install appium
744+
command: npm install [email protected] -g
745+
- run:
746+
name: Install appium drivers
747+
command: |
748+
appium driver install uiautomator2
749+
appium driver install xcuitest
750+
- run:
751+
name: Start Appium server
752+
command: appium --base-path /wd/hub
753+
background: true
754+
- run:
755+
name: Start Metro
756+
command: |
757+
cd packages/rn-tester
758+
yarn start
759+
background: true
760+
- brew_install:
761+
package: cmake
762+
- setup_ruby:
763+
ruby_version: << parameters.ruby_version >>
764+
- run:
765+
name: Install Bundler
766+
command: |
767+
cd packages/rn-tester
768+
bundle check || bundle install
769+
bundle exec pod setup
770+
bundle exec pod install --verbose
771+
- run:
772+
name: Boot iOS Simulator
773+
command: source scripts/.tests.env && xcrun simctl boot "$IOS_DEVICE" || true
774+
- run:
775+
name: Build app
776+
command: |
777+
xcodebuild build \
778+
-workspace packages/rn-tester/RNTesterPods.xcworkspace \
779+
-configuration Debug \
780+
-scheme RNTester \
781+
-sdk iphonesimulator \
782+
-derivedDataPath /tmp/e2e/
783+
- run:
784+
name: Move app to correct directory
785+
command: mv /tmp/e2e/Build/Products/Debug-iphonesimulator/RNTester.app packages/rn-tester-e2e/apps/rn-tester.app
786+
- run:
787+
name: Check Appium server status
788+
command: |
789+
if ! nc -z 127.0.0.1 4723; then
790+
echo Could not find Appium server!
791+
exit 1
792+
fi
793+
- run:
794+
name: Run E2E tests
795+
command: |
796+
cd packages/rn-tester-e2e
797+
yarn test-ios-e2e
798+
799+
# -------------------------
800+
# JOBS: Android E2E Tests
801+
# -------------------------
802+
test_e2e_android:
803+
executor:
804+
name: android/android-machine
805+
tag: 2023.07.1
806+
steps:
807+
- checkout_code_with_cache
808+
- run_yarn
809+
- android/create-avd:
810+
avd-name: e2e_emulator
811+
system-image: system-images;android-33;google_apis;x86_64
812+
install: true
813+
- android/start-emulator:
814+
avd-name: e2e_emulator
815+
no-window: true
816+
restore-gradle-cache-prefix: v1a
817+
post-emulator-launch-assemble-command: ""
818+
- run:
819+
name: Install appium
820+
command: npm install [email protected] -g
821+
- run:
822+
name: Install appium drivers
823+
command: |
824+
appium driver install uiautomator2
825+
appium driver install xcuitest
826+
- run:
827+
name: Start Appium server
828+
command: appium --base-path /wd/hub
829+
background: true
830+
- run:
831+
name: Start Metro
832+
command: |
833+
cd packages/rn-tester
834+
yarn start
835+
background: true
836+
- attach_workspace:
837+
at: .
838+
- run:
839+
name: Build app
840+
command: |
841+
./gradlew :packages:rn-tester:android:app:assembleHermesDebug -PreactNativeArchitectures=x86_64
842+
- run:
843+
name: Move app to correct directory
844+
command: mv packages/rn-tester/android/app/build/outputs/apk/hermes/debug/app-hermes-x86_64-debug.apk packages/rn-tester-e2e/apps/rn-tester.apk
845+
- run:
846+
name: Check Appium server status
847+
command: |
848+
if ! nc -z 127.0.0.1 4723; then
849+
echo Could not find Appium server
850+
exit 1
851+
fi
852+
- run:
853+
name: Run E2E tests
854+
command: |
855+
cd packages/rn-tester-e2e
856+
yarn test-android-e2e
857+
# -------------------------
727858
# JOBS: Test Android
728859
# -------------------------
729860
test_android:
@@ -1646,6 +1777,9 @@ workflows:
16461777
run_disabled_tests: false
16471778
- test_android
16481779
- test_android_docker_image
1780+
- test_e2e_ios:
1781+
ruby_version: "2.7.7"
1782+
- test_e2e_android
16491783
- test_android_template:
16501784
requires:
16511785
- build_npm_package

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,7 @@ package-lock.json
147147

148148
# Temporary files created by Metro to check the health of the file watcher
149149
.metro-health-check*
150+
151+
# E2E files
152+
/packages/rn-tester-e2e/apps/*.apk
153+
/packages/rn-tester-e2e/apps/*.app

packages/rn-tester-e2e/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# RNTester E2E folder
2+
3+
In this folder we have a the setup for running E2E testing in RNTester via the usage of [Appium](https://appium.io/) and [WebDriverIO](https://webdriver.io/) and [Jest](https://jestjs.io/).
4+
5+
- [Setting up locally](#setting-up-locally)
6+
- [(one-off) Setting up Appium](#one-off-setting-up-appium)
7+
- [Building RNTester app](#building-rntester-app)
8+
- [Building for iOS](#building-for-ios)
9+
- [Building for Android](#building-for-android)
10+
- [Setting up the RNTester E2E folder](#setting-up-the-rntester-e2e-folder)
11+
- [Testing the RNTester app E2E](#testing-the-rntester-app-e2e)
12+
- [Adding new tests (and project structure)](#adding-new-tests-and-project-structure)
13+
14+
## Setting up locally
15+
16+
### (one-off) Setting up Appium
17+
18+
The first step you need to do is to ensure to install the tooling:
19+
20+
```bash
21+
npm install [email protected] -g
22+
appium driver install uiautomator2
23+
appium driver install xcuitest
24+
```
25+
26+
> More details about drivers in Appium [here](https://appium.github.io/appium/docs/en/2.0/guides/managing-exts/) and [here](https://appium.github.io/appium/docs/en/2.0/quickstart/uiauto2-driver/)
27+
28+
You should not need to run install commands for drivers separately more than once, even if you bump the dep in package.json.
29+
30+
### Building RNTester app
31+
32+
Building manually *.app* and *.apk* is required to run automation tests on local environment.
33+
34+
0. *(optional)* If you previously built RNTester, you may need to clean up build files and Pods:
35+
36+
```bash
37+
yarn test-e2e-local-clean && yarn install
38+
```
39+
40+
1. Step 1: install packages for the repository, then navigate in the rn-tester folder
41+
42+
```bash
43+
cd react-native
44+
yarn install
45+
cd packages/rn-tester
46+
```
47+
48+
Now, depending on the platform, there are some specific steps
49+
50+
#### Building for iOS
51+
52+
0. Make sure you have Bundler `gem install bundler` - we use it ensure installing the right version of CocoaPods locally.
53+
1. Install Bundler and CocoaPods dependencies: `bundle install` then `bundle exec pod install` or `yarn setup-ios-hermes` for RNTester with Hermes. In order to use JSC instead of Hermes engine, run: `USE_HERMES=0 bundle exec pod install` or `setup-ios-jsc` instead.
54+
2. You can build app with React Native CLI or manually with Xcode:
55+
1. To build with React Native CLI:
56+
1. Run `npx react-native build-ios --mode Debug --scheme RNTester --buildFolder /path/to/build-folder`, replace `/path/to/build-folder` with the real path.
57+
2. Copy the built app using `mv` - `mv /path/to/build-folder/Build/Products/Debug-iphonesimulator/RNTester.app ~/react-native/packages/rn-tester-e2e/apps` or manually.
58+
2. To build with Xcode, open the generated `RNTester.xcworkspace` and build.
59+
1. Find the **RNTester.app** in `~/Library/Developer/Xcode/DerivedData/RNTesterPods-{id}/Build/Products/Debug-iphonesimulator`
60+
2. Copy the app to the following directory `~/react-native/packages/rn-tester-e2e/apps`.
61+
3. Change its name to: `rn-tester.app`
62+
63+
#### Building for Android
64+
65+
0. You'll need to have all the [prerequisites](https://reactnative.dev/contributing/how-to-build-from-source#prerequisites) (SDK, NDK) for Building React Native installed.
66+
1. Start an Android emulator.
67+
2. Build the app via
68+
69+
```bash
70+
# In order to not use Hermes engine, run `yarn install-android-jsc` instead.
71+
yarn install-android-hermes
72+
yarn start
73+
```
74+
75+
*Note: Building for the first time can take a while.*
76+
77+
3. Find the **app-*-debug.apk** in `~/react-native/packages/rn-tester/android/app/build/outputs/apk/hermes/debug`
78+
4. Copy the app `app-*-debug.apk` to the following directory `~/react-native/packages/rn-tester-e2e/apps`
79+
5. Change its name to: `rn-tester.apk`
80+
81+
### Setting up the RNTester E2E folder
82+
83+
In `react-native/packages/rn-tester-e2e` open the following file
84+
85+
```bash
86+
/react-native/packages/rn-tester-e2e/e2e-config.js
87+
```
88+
89+
And modify lines L24->L39 to reflect your local setup configuration (ex. `platformVersion`, `deviceName`). Make sure to **not** commit this change if you send a PR to add tests.
90+
91+
## Testing the RNTester app E2E
92+
93+
After you have done all the above correctly, and you have the Android/iOS apps in the `rn-tester-e2e/apps` folder, in a dedicated terminal window, run:
94+
95+
```bash
96+
appium --base-path /wd/hub
97+
```
98+
99+
This will start the Appium server - you will need this to keep running.
100+
101+
Then open a second terminal window and start the Metro terminal from the `packages/rn-tester` folder, via `yarn start --reset-cache`. This terminal window also needs to keep running.
102+
103+
Now, make sure that the iOS simulator/the Android emulator is up and running.
104+
105+
Finally, you can open a third terminal window and run:
106+
107+
```bash
108+
yarn test-android-e2e # for android
109+
yarn test-ios-e2e # for ios
110+
```
111+
112+
Now you should see the RNTester app being open, and the defined test being run.
113+
114+
## Adding new tests (and project structure)
115+
116+
This project has 2 main folders:
117+
118+
- `apps`, where, as you have seen above, the iOS/Android RNTester apps need to be put so that appium will pick them and install in the emulator/simulator consistently.
119+
120+
- `tests`, where the tests and referencing files all live. The substructure is as follows:
121+
- `screens` -> in this folder, you will find `*.screen.js` files, where each file represents a navigation screen for RNTester. So there are 3 root ones (`apis`, `bookmarks`, `components`) and then for subscreens, there's a folder with the same name - currently, that's only `components` that contains `buttonComponent.screen.js`. The content of these files is what was earlier mentioned as "references": they provide an easy way to define all elements present in said screen, so that they can be used for tests.
122+
- `specs` -> this folder follows a similar 1:1 mapping to the RNTester screens, but for the tests: for each screen (or subscreen) there's a dedicated `*.test.js` file (such as `buttonComponentScreen.test.js`). Ideally, in this file the Jest tests are standard, leveraging the `*.screen.js` counterpart for the details of defining how Appium/WDIO can reach those elements on screen.
123+
124+
When adding a new test, please ensure that you follow this pattern and add the relevant test in the right screen file / screen test file. Use the files mentioned above as examples.

packages/rn-tester-e2e/apps/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Put the *.app and *.apk files here.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
module.exports = {
12+
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
13+
plugins: ['@babel/plugin-transform-flow-strip-types'],
14+
};

packages/rn-tester-e2e/e2e-config.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
const path = require('path');
12+
13+
type Capabilities = {
14+
platformName: 'Android' | 'iOS',
15+
'appium:platformVersion': string,
16+
'appium:deviceName': string,
17+
'appium:app': string,
18+
'appium:automationName': 'UiAutomator2' | 'XCUITest',
19+
'appium:newCommandTimeout'?: number,
20+
};
21+
22+
let capabilities: Capabilities;
23+
24+
const android = {
25+
platformName: 'Android',
26+
'appium:platformVersion': '13.0',
27+
'appium:deviceName': 'Android Emulator',
28+
'appium:app': path.join(process.cwd(), '/apps/rn-tester.apk'),
29+
'appium:automationName': 'UiAutomator2',
30+
'appium:newCommandTimeout': 240,
31+
};
32+
33+
const ios = {
34+
platformName: 'iOS',
35+
'appium:platformVersion': '16.4',
36+
'appium:deviceName': 'iPhone 14',
37+
'appium:automationName': 'XCUITest',
38+
'appium:app': path.join(process.cwd(), '/apps/rn-tester.app'),
39+
};
40+
41+
// check that E2E_DEVICE exists, is a string and its either "ios" or "android"
42+
if (!process.env.E2E_DEVICE) {
43+
throw new Error('E2E_DEVICE environment variable is not defined');
44+
} else if (typeof process.env.E2E_DEVICE !== 'string') {
45+
throw new Error('E2E_DEVICE environment variable is not a string');
46+
} else if (
47+
process.env.E2E_DEVICE !== 'ios' &&
48+
process.env.E2E_DEVICE !== 'android'
49+
) {
50+
throw new Error('E2E_DEVICE environment variable is not "ios" or "android"');
51+
}
52+
53+
if (process.env.E2E_DEVICE === 'android') {
54+
capabilities = android;
55+
}
56+
57+
if (process.env.E2E_DEVICE === 'ios') {
58+
capabilities = ios;
59+
}
60+
61+
export default capabilities;

packages/rn-tester-e2e/jest.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
module.exports = {
12+
testTimeout: 120000,
13+
bail: 0,
14+
setupFilesAfterEnv: ['./jest.setup.js'],
15+
testMatch: ['**/specs/**/*.js'],
16+
maxWorkers: 1,
17+
};

0 commit comments

Comments
 (0)