Skip to content

Commit

Permalink
test: add ui testing
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilveirah committed Dec 29, 2023
1 parent ced1633 commit ce7f425
Show file tree
Hide file tree
Showing 10 changed files with 1,187 additions and 419 deletions.
83 changes: 83 additions & 0 deletions .detoxrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/** @type {Detox.DetoxConfig} */
module.exports = {
testRunner: {
args: {
'$0': 'jest',
config: 'e2e/jest.config.js'
},
jest: {
setupTimeout: 120000
}
},
apps: {
'ios.debug': {
type: 'ios.app',
binaryPath: 'example/ios/build/Build/Products/Debug-iphonesimulator/SourcepointCmpExample.app',
build: 'xcodebuild -workspace example/ios/SourcepointCmpExample.xcworkspace -scheme SourcepointCmpExample -configuration Debug -sdk iphonesimulator -derivedDataPath example/ios/build'
},
// 'ios.release': {
// type: 'ios.app',
// binaryPath: 'example/ios/build/Build/Products/Release-iphonesimulator/SourcepointCmpExample.app',
// build: 'xcodebuild -workspace example/ios/SourcepointCmpExample.xcworkspace -scheme SourcepointCmpExample -configuration Release -sdk iphonesimulator -derivedDataPath example/ios/build'
// },
'android.debug': {
type: 'android.apk',
binaryPath: 'example/android/app/build/outputs/apk/debug/app-debug.apk',
build: 'cd example/android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug',
reversePorts: [
8081
]
},
// 'android.release': {
// type: 'android.apk',
// binaryPath: 'example/android/app/build/outputs/apk/release/app-release.apk',
// build: 'cd android && ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release'
// }
},
devices: {
simulator: {
type: 'ios.simulator',
device: {
type: 'iPhone 15'
}
},
// attached: {
// type: 'android.attached',
// device: {
// adbName: '.*'
// }
// },
emulator: {
type: 'android.emulator',
device: {
avdName: 'Medium_Phone_API_29'
}
}
},
configurations: {
'ios.sim.debug': {
device: 'simulator',
app: 'ios.debug'
},
// 'ios.sim.release': {
// device: 'simulator',
// app: 'ios.release'
// },
// 'android.att.debug': {
// device: 'attached',
// app: 'android.debug'
// },
// 'android.att.release': {
// device: 'attached',
// app: 'android.release'
// },
'android.emu.debug': {
device: 'emulator',
app: 'android.debug'
},
// 'android.emu.release': {
// device: 'emulator',
// app: 'android.release'
// }
}
};
3 changes: 3 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ buildscript {
repositories {
google()
mavenCentral()
maven {
url("$rootDir/../node_modules/detox/Detox-android")
}
}

dependencies {
Expand Down
138 changes: 138 additions & 0 deletions e2e/full.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import jestExpect from 'expect'
import { web, element, by, expect, waitFor, device } from 'detox'

const app = {
_timeout: 5000, //ms
loadMessagesButton: element(by.text(/load messages/i)),
loadGDPRPMButton: element(by.text(/load gdpr pm/i)),
loadCCPAPMButton: element(by.text(/load ccpa pm/i)),
clearDataButton: element(by.text(/clear all/i)),
sdkElement: element(by.id('sdkStatus')),
presentingStatusText: 'Presenting',
finishedStatusText: 'Finished',
messageTitle: web.element(by.web.cssSelector('#notice > p')),
acceptAllButton: web.element(by.web.cssSelector('.sp_choice_type_11')),
rejectAllButton: web.element(by.web.cssSelector('.sp_choice_type_13')),
pmCancelButton: web.element(by.web.cssSelector('.sp_choice_type_CANCEL')),
pmToggleOn: web.element(by.web.cssSelector('button[aria-checked=true]')),
pmToggleOff: web.element(by.web.cssSelector('button[aria-checked=false]')),

getGDPRUUID: async function () {
return (await element(by.id('gdpr.uuid')).getAttributes()).text
},

getCCPAUUID: async function () {
return (await element(by.id('ccpa.uuid')).getAttributes()).text
},

forSDKToBePresenting: async function () {
await waitFor(this.sdkElement)
.toHaveText(this.presentingStatusText)
.withTimeout(this._timeout)
},

forSDKToBeFinished: async function () {
await waitFor(this.sdkElement)
.toHaveText(this.finishedStatusText)
.withTimeout(this._timeout)
},

loadGDPRPM: async function () {
await this.loadGDPRPMButton.tap()
},

loadCCPAPM: async function () {
await this.loadCCPAPMButton.tap()
},

dismissPM: async function () {
await this.pmCancelButton.tap()
},

assertNoMessageShow: async function () {
await this.reloadMessages({ clearData: false })
await this.forSDKToBeFinished()
},

reloadMessages: async function ({ clearData }) {
if (clearData) await this.clearDataButton.tap()
await this.loadMessagesButton.tap()
},

acceptAllGDPRMessage: async function (withTitle = 'GDPR Message') {
await expect(app.messageTitle).toHaveText(withTitle)
await app.acceptAllButton.tap()
},

acceptAllCCPAMessage: async function (withTitle = 'CCPA Message') {
await expect(app.messageTitle).toHaveText(withTitle)
await app.acceptAllButton.tap()
},

rejectAllGDPRMessage: async function (withTitle = 'GDPR Message') {
await expect(app.messageTitle).toHaveText(withTitle)
await app.rejectAllButton.tap()
},

rejectAllCCPAMessage: async function (withTitle = 'CCPA Message') {
await expect(app.messageTitle).toHaveText(withTitle)
await app.rejectAllButton.tap()
},
}

const assertUUIDsDidntChange = async () => {
const gdprUUIDBeforeReloading = app.gdprUUID
const ccpaUUIDBeforeReloading = app.ccpaUUID
await app.assertNoMessageShow()
jestExpect(gdprUUIDBeforeReloading).toEqual(app.gdprUUID)
jestExpect(ccpaUUIDBeforeReloading).toEqual(app.ccpaUUID)
}

const assertAllPMToggles = async (loadPMFn, { toggled, togglesCount }) => {
await loadPMFn()
if (toggled) {
await expect(app.pmToggleOn.atIndex(togglesCount - 1)).toExist()
} else {
await expect(app.pmToggleOff.atIndex(togglesCount - 1)).toExist()
}
await app.dismissPM()
}

describe('SourcepointSDK', () => {
beforeAll(async () => {
await device.launchApp()
})

it('Accepting All, works', async () => {
await app.forSDKToBePresenting()
await app.acceptAllGDPRMessage()
await app.acceptAllCCPAMessage()
await app.forSDKToBeFinished()
await assertUUIDsDidntChange()
await assertAllPMToggles(() => app.loadGDPRPM(), {
toggled: true,
togglesCount: 5,
})
await assertAllPMToggles(() => app.loadCCPAPM(), {
toggled: true,
togglesCount: 3,
})
})

it('Rejecting All, works', async () => {
await app.reloadMessages({ clearData: true })
await app.forSDKToBePresenting()
await app.rejectAllGDPRMessage()
await app.rejectAllCCPAMessage()
await app.forSDKToBeFinished()
await assertUUIDsDidntChange()
await assertAllPMToggles(() => app.loadGDPRPM(), {
toggled: false,
togglesCount: 5,
})
await assertAllPMToggles(() => app.loadCCPAPM(), {
toggled: false,
togglesCount: 3,
})
})
})
12 changes: 12 additions & 0 deletions e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
rootDir: '..',
testMatch: ['<rootDir>/e2e/**/*.test.js'],
testTimeout: 120000,
maxWorkers: 1,
globalSetup: 'detox/runners/jest/globalSetup',
globalTeardown: 'detox/runners/jest/globalTeardown',
reporters: ['detox/runners/jest/reporter'],
testEnvironment: 'detox/runners/jest/testEnvironment',
verbose: true,
}
18 changes: 18 additions & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ android {
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testBuildType System.getProperty('testBuildType', 'debug')
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
signingConfigs {
debug {
Expand All @@ -100,6 +102,7 @@ android {
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
proguardFile "../../../node_modules/detox/android/detox/proguard-rules-app.pro"
}
}
}
Expand All @@ -109,6 +112,13 @@ dependencies {
implementation("com.facebook.react:react-android")
implementation("com.facebook.react:flipper-integration")


androidTestImplementation('com.wix:detox:+')
androidTestImplementation "androidx.test.ext:junit:1.1.5"
androidTestImplementation "androidx.fragment:fragment-testing:1.6.2"
androidTestImplementation "androidx.test:core:1.5.0"
implementation 'androidx.appcompat:appcompat:1.6.1'

if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
} else {
Expand All @@ -117,3 +127,11 @@ dependencies {
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

allprojects {
repositories {
maven {
url("../../../node_modules/detox/Detox-android")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sourcepointcmpexample;

import com.wix.detox.Detox;
import com.wix.detox.config.DetoxConfig;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class DetoxTest {
@Rule // (2)
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, false, false);

@Test
public void runDetoxTests() {
DetoxConfig detoxConfig = new DetoxConfig();
detoxConfig.idlePolicyConfig.masterTimeoutSec = 90;
detoxConfig.idlePolicyConfig.idleResourceTimeoutSec = 60;
detoxConfig.rnContextLoadTimeoutSec = (BuildConfig.DEBUG ? 180 : 60);

Detox.runTests(mActivityRule, detoxConfig);
}
}

3 changes: 2 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@
"@types/react-native": "0.70.0",
"commitlint": "^17.0.2",
"del-cli": "^5.0.0",
"detox": "21.0.0-rc.11",
"detox-cli": "^20.0.0",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^28.1.1",
"jest": "^29.7.0",
"pod-install": "^0.1.0",
"prettier": "^2.0.5",
"react": "18.2.0",
Expand Down
Loading

0 comments on commit ce7f425

Please sign in to comment.