Skip to content

Commit

Permalink
Fix: Android build for Detox e2e (#8362)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserfaraazkhan authored Dec 9, 2024
1 parent 854d620 commit 63889c0
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 12 deletions.
7 changes: 6 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ repositories {
maven {
url 'https://maven.google.com'
}
maven { url 'https://jitpack.io' }
}

dependencies {
Expand All @@ -203,8 +204,11 @@ dependencies {
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "com.google.firebase:firebase-messaging:$firebaseVersion"

androidTestImplementation('com.wix:detox:+')
androidTestImplementation 'androidx.test:core:1.6.0'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'com.wix:detox:20.26.2'

implementation project(':reactnativenotifications')
implementation project(':watermelondb-jsi')

Expand All @@ -217,6 +221,7 @@ dependencies {

configurations.all {
resolutionStrategy {
force 'androidx.test:core:1.6.0'
eachDependency { DependencyResolveDetails details ->
if (details.requested.name == 'play-services-base') {
details.useTarget group: details.requested.group, name: details.requested.name, version: '18.2.0'
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ buildscript {
allprojects {
repositories {
maven {
url "$rootDir/../node_modules/detox/Detox-android"
url "$rootDir/../detox/node_modules/detox/Detox-android"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx512m -XX:MaxMetaspaceSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m
org.gradle.jvmargs=-Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
Expand Down
35 changes: 35 additions & 0 deletions detox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# How to Run Detox Tests

## Android

### Install Dependencies

From the root directory, run the following command to install the necessary dependencies:

```sh
npm install
```

### Inject Detox Settings

To inject the Detox settings into your project, navigate to the `detox` directory and run the following command:

```sh
npm run inject-detox-settings
```

### Update `minSdkVersion` for `react-native-image-picker`

On macOS machines, update the `minSdkVersion` of `react-native-image-picker` to 23 by running the following command from the root directory:

```sh
sed -i '' 's/minSdkVersion 21/minSdkVersion 23/' ./node_modules/react-native-image-picker/android/build.gradle
```

### Build detox android app

From the `detox` folder run:

```
npm run e2e:android-build
```
19 changes: 13 additions & 6 deletions detox/e2e/support/ui/screen/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LoginScreen {
signinButtonDisabled = element(by.id(this.testID.signinButtonDisabled));

toBeVisible = async () => {
await wait(timeouts.ONE_SEC);
await wait(timeouts.FOUR_SEC);
await waitFor(this.loginScreen).toExist().withTimeout(timeouts.TEN_SEC);
await waitFor(this.usernameInput).toBeVisible().withTimeout(timeouts.TEN_SEC);

Expand All @@ -60,18 +60,25 @@ class LoginScreen {

login = async (user: any = {}) => {
await this.toBeVisible();
await this.usernameInput.typeText(`${user.newUser.email}\n`);
await this.passwordInput.typeText(`${user.newUser.password}\n`);

await this.usernameInput.tap({x: 150, y: 10});
await this.usernameInput.replaceText(user.newUser.email);
await this.passwordInput.tap();
await this.passwordInput.replaceText(user.newUser.password);
await this.signinButton.tap();

await wait(timeouts.FOUR_SEC);
};

loginAsAdmin = async (user: any = {}) => {
await this.toBeVisible();
await this.usernameInput.typeText(user.username);
await this.passwordInput.typeText(`${user.password}\n`);
await this.usernameInput.tap({x: 150, y: 10});

await wait(timeouts.ONE_SEC);
await this.usernameInput.replaceText(user.username);
await this.passwordInput.tap();
await this.passwordInput.replaceText(user.password);
await this.signinButton.tap();
await wait(timeouts.FOUR_SEC);
};
}

Expand Down
5 changes: 2 additions & 3 deletions detox/e2e/support/ui/screen/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ class ServerScreen {
connectToServer = async (serverUrl: string, serverDisplayName: string) => {
await this.toBeVisible();
await this.serverUrlInput.replaceText(serverUrl);
await this.serverUrlInput.tapReturnKey();
await this.serverDisplayNameInput.replaceText(serverDisplayName);
if (isAndroid()) {
await this.serverDisplayNameInput.tapReturnKey();
await this.tapConnectButton();
}
if (isIos()) {
await this.tapConnectButton();
Expand All @@ -67,7 +66,7 @@ class ServerScreen {
}
}
}
await waitFor(this.usernameInput).toExist().withTimeout(timeouts.ONE_SEC);
await waitFor(this.usernameInput).toExist().withTimeout(timeouts.FOUR_SEC);
};

close = async () => {
Expand Down
72 changes: 72 additions & 0 deletions detox/inject-detox-settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');

// Paths to files
const androidManifestPath = path.resolve(
__dirname,
'../android/app/src/debug/AndroidManifest.xml',
);
const settingsGradlePath = path.resolve(__dirname, '../android/settings.gradle');

// Detox code to add to settings.gradle
const detoxSettings = `
include ':detox'
project(':detox').projectDir = new File(rootProject.projectDir, '../detox/node_modules/detox/android')
`;

// Updated AndroidManifest.xml content
const updatedManifest = `<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mattermost.rnbeta">
<application
android:usesCleartextTraffic="true"
tools:targetApi="28"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity"
android:exported="true"
tools:node="replace"/>
<activity
android:name="androidx.test.core.app.InstrumentationActivityInvoker$EmptyActivity"
android:exported="true"
tools:node="replace"/>
<activity
android:name="androidx.test.core.app.InstrumentationActivityInvoker$EmptyFloatingActivity"
android:exported="true"
tools:node="replace"/>
</application>
</manifest>`;

// Update AndroidManifest.xml
function updateAndroidManifest() {
try {
fs.writeFileSync(androidManifestPath, updatedManifest, 'utf-8');
console.log('AndroidManifest.xml updated successfully.');
} catch (err) {
console.error(`Failed to update AndroidManifest.xml: ${err.message}`);
}
}

// Update settings.gradle
function updateSettingsGradle() {
try {
const content = fs.readFileSync(settingsGradlePath, 'utf-8');
if (content.includes("include ':detox'")) {
console.log('Detox settings already present in settings.gradle.');
return;
}
fs.writeFileSync(settingsGradlePath, content + detoxSettings, 'utf-8');
console.log('settings.gradle updated successfully.');
} catch (err) {
console.error(`Failed to update settings.gradle: ${err.message}`);
}
}

// Run updates
updateAndroidManifest();
updateSettingsGradle();
1 change: 1 addition & 0 deletions detox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"xml2js": "0.6.2"
},
"scripts": {
"e2e:android-inject-settings": "node inject-detox-settings.js",
"e2e:android-create-emulator": "./create_android_emulator.sh",
"e2e:android-build": "detox build -c android.emu.debug",
"e2e:android-test": "detox test -c android.emu.debug",
Expand Down

0 comments on commit 63889c0

Please sign in to comment.