<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Avinash-Kannan</title>
    <description>The latest articles on DEV Community by Avinash-Kannan (@avinashkannan).</description>
    <link>https://dev.to/avinashkannan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F871953%2F8997b28e-72e2-4ef5-84e3-6bcc45643758.png</url>
      <title>DEV Community: Avinash-Kannan</title>
      <link>https://dev.to/avinashkannan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/avinashkannan"/>
    <language>en</language>
    <item>
      <title>Detox : Gray box end-to-end test automation framework for react-native apps</title>
      <dc:creator>Avinash-Kannan</dc:creator>
      <pubDate>Fri, 03 Jun 2022 11:03:22 +0000</pubDate>
      <link>https://dev.to/avinashkannan/detox-gray-box-end-to-end-test-automation-framework-for-react-native-apps-5860</link>
      <guid>https://dev.to/avinashkannan/detox-gray-box-end-to-end-test-automation-framework-for-react-native-apps-5860</guid>
      <description>&lt;h1&gt;
  
  
  Pre Requisites :
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nBs2VJXr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hfbbabyr8og0aheichy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nBs2VJXr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hfbbabyr8og0aheichy.png" alt="Image description" width="880" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Node.js
&lt;/h3&gt;

&lt;p&gt;Download &lt;a href="https://nodejs.org/en/download/"&gt;Node js&lt;/a&gt; or use Homebrew command :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install node&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(Note : Install Node.js v12.0 or above. Use nvm if you need to manage your node versions installed on a single machine)&lt;/p&gt;

&lt;h3&gt;
  
  
  Android Studio
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://wix.github.io/Detox/docs/introduction/android-dev-env"&gt;Setting up Android Studio&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create and Launch Emulators:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ANDROID_HOME/tools/bin/avdmanager create avd -n &amp;lt;emulatorname&amp;gt; -d pixel - package "system-images;android-30;google_apis;x86"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ANDROID_HOME/emulator/emulator -verbose @&amp;lt;emulatorname&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Xcode
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://wix.github.io/Detox/docs/introduction/ios-dev-env"&gt;Setting Up an iOS Development &amp;amp; Testing Environment&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create and Launch Simulators:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;xcrun simctl list&lt;/code&gt;&lt;br&gt;
&lt;code&gt;open -a Simulator - args -CurrentDeviceUDID &amp;lt;UDID&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Detox Command Line Tools (detox-cli)
&lt;/h3&gt;

&lt;p&gt;detox-cli should be installed globally, enabling usage of the command line tools outside your npm scripts.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install -g detox-cli&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Application under Test :
&lt;/h1&gt;

&lt;p&gt;If you already have an application in which detox framework to be set up, Kindly ignore this section. For folks practising hands-on, Please feel free to use this sample react-native feedback app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q8bNVnVg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pz5tnenthss1h0imk0e7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q8bNVnVg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pz5tnenthss1h0imk0e7.png" alt="Image description" width="880" height="1158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a react-native project by running the following command:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npx react-native init &amp;lt;project_name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install below packages as Dev dependencies :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install -D @react-native-community/checkbox&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm install -D react-native-radio-buttons-group&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add the below code in your App.js :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://github.com/Avinash-Kannan/Detox-MediumBlog-Repo/blob/main/App.js"&gt;App.js file Github Link&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run your application in Simulator / Emulator :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npx react-native run-ios&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npx react-native run-android&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Setting up detox in react-native project
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Install Detox node module as the first step:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install -D detox&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a Test Runner (use Jest / Mocha ) :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install -D jest&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up Test-code Scaffolds :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npx detox init -r jest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LePrvTqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf7kibqctp13wssnlq6r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LePrvTqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf7kibqctp13wssnlq6r.png" alt="Image description" width="880" height="111"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Detox iOS and Android config (.detoxrc.json) :
&lt;/h1&gt;

&lt;p&gt;Configuring detox for iOS includes only updating the ".detoxrc.json" file , where as android requires few additional set up which we will be adding in following steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"testRunner": "jest",
  "runnerConfig": "e2e/config.json",
  "skipLegacyWorkersInjection": true,
  "apps": {
    "ios": {
      "type": "ios.app",
      "binaryPath": "ios/Build/Products/Debug-iphonesimulator/&amp;lt;YourProject&amp;gt;.app",
      "build": "xcodebuild -workspace ios/&amp;lt;YourProject&amp;gt;.xcworkspace -configuration Debug -scheme &amp;lt;YourProject&amp;gt; -derivedDataPath ios/Build"

    },
    "android": {
      "type": "android.apk",
      "binaryPath": "android/app/build/outputs/apk/debug/app-debug.apk",
      "build": "cd android &amp;amp;&amp;amp; ./gradlew assembleDebug assembleDebugAndroidTest -DtestBuildType=debug &amp;amp;&amp;amp; cd /.."
    }
  },
  "devices": {
    "simulator": {
      "type": "ios.simulator",
      "device": {
        "type": "iPhone 13"
      }
    },
    "emulator": {
      "type": "android.emulator",
      "device": {
        "avdName": "&amp;lt;EmulatorName&amp;gt;"
      }
    }
  },
  "configurations": {
    "ios": {
      "device": "simulator",
      "app": "ios"
    },
    "android": {
      "device": "emulator",
      "app": "android"
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Under the key "binaryPath", you should provide the path of the .app bundle to use. Under the key "build", specify the xcodebuild command for your project. The 'build' command would output the app bundle in the path specified under 'binaryPath'.&lt;/p&gt;

&lt;h1&gt;
  
  
  Additional Set-up for Android :
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Add below code in your root buildscript (i.e. android/build.gradle )
// Note: add the 'allproject' section if it doesn't exist
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;allprojects {
    repositories {
        // ...
        google()
        maven {
            // All of Detox' artifacts are provided via the npm module
            url "$rootDir/../node_modules/detox/Detox-android"
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add below code in your app's buildscript (i.e. android/app/build.gradle)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies {
    // ...
    androidTestImplementation('com.wix:detox:+')
}
and add this to the defaultConfig subsection
android {
  // ...

  defaultConfig {
      // ...
      testBuildType System.getProperty('testBuildType', 'debug')  // This will later be used to control the test apk build type
      testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a Detox-Test Class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Detox requires a dummy implementation of a single Android-native test.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Add a new file to your project, under this path and name =&amp;gt; android/app/src/androidTest/java/com/[your.package]/DetoxTest.java. Double-check that the path is correct!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy &amp;amp; paste the content of the equivalent file from the &lt;a href="https://github.com/wix/Detox/blob/master/examples/demo-react-native/android/app/src/androidTest/java/com/example/DetoxTest.java"&gt;detox example app for RN&lt;/a&gt;, into it. Don't forget to change the package name to your project's package name.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Writing detox test scripts :
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Locator strategy&lt;/strong&gt; :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To eliminate difficulties involved in locating elements in Android/iOS using various Inspectors, it is recommended to define and use testID ( common for both Android/iOS ). In the below video, we will define testID for the elements( app header , checkbox, submit button in sample feedback app) to be automated.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/WLCysmZq0Vw"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Adding test cases&lt;/strong&gt; :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will now automate steps to fill and submit feedback.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/_hQgLgGk8yI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://github.com/Avinash-Kannan/Detox-MediumBlog-Repo/blob/540fbdf2740f2442ecf79ac994e48a7caaacbcb1/e2e/basicTest.e2e.ts"&gt;test script&lt;/a&gt; will work smoothly for both android and iOS without any alterations specific to platform.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Running tests in Emulator/Simulator &lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the below video, I have explained the configurations in .detoxrc.json file once again.Used the following commands to build app bundle and run test scripts in Android and iOS.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;detox build -c android&lt;/code&gt;&lt;br&gt;
&lt;code&gt;detox test -c android&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/0vfEBFcHzuU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;For generating test artifacts, run following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;detox test --record-videos all --take-screenshots all -c ios&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Exploring API's provided by Detox, Pros and issues&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is an optional section explaining additional information on writing effective test scripts using various API's and docs provided by detox.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/8N47kieCXpE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;For Detailed info on below topics :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Detox Design Principles &lt;/li&gt;
&lt;li&gt;Espresso and EarlGrey - employed by Detox&lt;/li&gt;
&lt;li&gt;Detox vs Appium&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please visit my &lt;a href="https://medium.com/@avinashkannan/detox-gray-box-end-to-end-test-automation-framework-for-react-native-apps-129eb0f19cb0"&gt;Medium Blog on Detox&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>detox</category>
      <category>mobile</category>
      <category>testautomation</category>
    </item>
  </channel>
</rss>
