Detox 101: How to write stable end-to-end tests for your React Native application

Rate this content
Bookmark
The video details how to write stable end-to-end tests for React Native applications using Detox. It explains that Detox is a cross-platform framework specifically designed for React Native, providing synchronization with the application to avoid test failures due to ongoing processes or animations. The video covers how to configure Detox for both iOS and Android, emphasizing the importance of starting tests from a clean state and using unique test IDs for elements. It also demonstrates actions like tapping, scrolling, and swiping, and how to handle platform-specific code. The video discusses strategies to avoid flakiness in tests, such as using retries and running tests in parallel threads. Debugging tools such as detailed logs, screenshots, and video recordings of test sessions are highlighted as essential features of Detox. The video encourages contributions to the Detox open-source project and provides detailed steps for setting up and writing tests for a React Native application using Detox.

From Author:

Compared to unit testing, end-to-end testing aims to interact with your application just like a real user. And as we all know it can be pretty challenging. Especially when we talk about Mobile applications.


Tests rely on many conditions and are considered to be slow and flaky. On the other hand - end-to-end tests can give the greatest confidence that your app is working. And if done right - can become an amazing tool for boosting developer velocity.


Detox is a gray-box end-to-end testing framework for mobile apps. Developed by Wix to solve the problem of slowness and flakiness and used by React Native itself as its E2E testing tool.


Join me on this workshop to learn how to make your mobile end-to-end tests with Detox rock.


Prerequisites

- iOS/Android: MacOS Catalina or newer

- Android only: Linux

- Install before the workshop

This workshop has been presented at React Summit 2022, check out the latest edition of this React Conference.

FAQ

Contributors can get involved with the Detox project by fixing bugs, developing features, or enhancing the framework's functionality. Interested developers should look for issues labeled 'looking for contributors' in the Detox repository and follow the contribution guide provided by the project.

Detox provides several debugging tools, including detailed log levels, taking screenshots, and recording videos of test sessions. These features help identify the causes of test failures and verify the application's behavior during testing.

Yes, Detox supports cross-platform testing, allowing the same tests to be run on both iOS and Android platforms. It includes support for handling platform-specific elements and gestures, making it versatile for testing mobile applications across different devices.

Detox addresses test flakiness by synchronizing with the app, waiting for it to go idle before proceeding with tests. This approach ensures that tests only run when the app is stable, reducing the likelihood of failures due to timing issues.

Detox is an open-source end-to-end testing framework developed by Wix to address issues like slowness and flakiness in test automation, specifically for mobile applications. It is designed for React Native and provides stable testing by synchronizing with the application.

To avoid flakiness in Detox tests, it is recommended to use unique identifiers for elements, ensure tests are independent, start tests from a clean state, and use proper synchronization. Additionally, running tests in parallel and using the retry option as a last resort can also help manage flakiness.

Yevheniia Hlovatska
Yevheniia Hlovatska
117 min
01 Jul, 2022

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to Detox and Test Automation

Short description:

Welcome to our Detox 101 workshop. Today, we'll learn how to write stable, end-to-end tests for your React Native application using Detox. Detox is a gray box testing tool developed by Wix to solve the problem of slowness and flakiness in test automation. It is different from other frameworks and popular in the React Native community. Detox allows you to access memory, read application state, and sync with the main thread of your application.

So, hello everyone, and welcome to our Detox 101 workshop. I'm super happy to see all of you here. Today, we'll actually try to make it hands-on. We'll try to practice a lot, and then learn the tool. And we will actually learn how to write stable, end-to-end tests for your React Native application.

But first, let me introduce myself. Hi, my name is Yevgenia. You can call me Jane. I'm from Ukraine, and currently in Ukraine also. I'm a QA manager in Wix, and my main focus during the last few years was actually on developing strategies, best practices, education, and test support for test automation in both web and mobile in Wix. And for web we have our internal solution, and for mobile, as you might already know, we have our open-source end-to-end testing framework for mobile applications, which is called DtoX. And we will be talking about it today. Also, I'm an authorized instructor in ICA Agile, and I'm a founder of QA School in Ukraine, where we educate QAs how to do good test automation. I'm also a public speaker. I'm usually talking on QA conferences, but you guys invited me to do the challenge and do a workshop on a DevConference, so it's my first time. Wish me luck. And I'm super actually excited to do it. And I'm a crazy Corgi lover, especially loving this little guy. His name is Ruby, and he is somewhere around us today. He was actually named after a programming language, so he's one of the participants. But he's not the topic today. Today we're here to talk about detox.

And since we're talking about testing tool, before starting to switch to the tool itself, it's also worth to say that we have today an amazing detox team with us. Full house. Guys, please say hi. Great. So, we have Amit, Asaf, Joseph, and Jonathan. Thanks a lot for joining. Guys, we'll be actually here to help you during the workshop. In case you're having any issues or questions or anything, you're always welcome to post them to the Discord channel created for this workshop. It's called RSH Detox 101 and it's dedicated to our workshop. So in case you're facing any issues during the workshop, they will be there to help you. And also I've posted a message there called For Q&A. If you have questions that you want to get an answer for like, what is your next feature I reported some bug when you're going to fix it, or like how can I contribute, we will save time in the end for the Q&A. So just put it into the thread. And again, don't hesitate to ask for help in case. Okay. And let's just stay muted. I will go through the flow of the workshop and guys will be there for you in Discord to help.

Before actually starting to talk about again, end-to-end testing and tools, it's worth to remind about the primary concept in that's automation, you might have already saw it before. It's a concept of basic automation pyramid. So what it actually says is that we should have multiple testing levels in order to cover our application in the most cost effective way. We should have as many unit tests as possible, because they are testing the code itself, the exact functions. They're the cheapest, the fastest, so we can have as much as possible. Then we should have a smaller amount of integration tests. And it can be regular integration, it can be component tests, and some other intermediate levels. We should have less of them to connect those units together. And then we have end-to-end tests for anything else that is like remaining. And end-to-end tests, we kind of think that it should be the smallest amount of those, because they are actually connecting the dots acting like a real user by just literally pressing buttons on your application, visually checking something and such.

In this case, when I usually talk to devs, because I work a lot with development groups in Wix. I hear that they use uni tests a lot, I hear that they like integration tests, but I almost never hear that they love to write end-to-end tests. And usually, end-to-end tests considered to be problematic. And the reason for that is usually flakiness. So sometimes, since end-to-end tests are related, they depend on many, many circumstances. So there they depend on the, even the connection, the other modules that you're testing, internet third parties, everything. Because they're literally doing it like a real user. So it means that you end up with having your tests flaky, and even because it was not like your problem. And the good thing is that Detox was actually created with this problem in mind.

So please meet Detox. Detox was developed by Wix to actually solve the problem of slowness and flakiness in test automation and used by React Native as it's own end-to-end testing tool. So to explain why, like, and how Detox is different from other frameworks and why it's like popular in the React Native community. The first thing is that Detox is gray box and not black box. You might have heard the terms like, black box is when you are testing something like a real user, you have no idea what is going on under the hood and white box is when you know everything and you kind of know how each function works, so it's usually close to unit testing and such. But there is also an intermediate concept in between, which is called gray box. So gray box is kind of when you know something about how your system works under the hood and you can work according to it. In terms of test automation, when you look at the black box, black box is kind of when you know only about the UI hierarchy of your app. So basically you know what are the components that you have over here and you know that you can interact with them. This is it. You don't have anything else. But when we are talking about the gray box concept, you can access memory, you can read application state and you can actually sync with the main thread of your application.

2. Introduction to Detox

Short description:

Detox is a cross-platform framework for writing stable, end-to-end tests for React Native applications. It synchronizes with your application, waits for it to go idle, and prevents tests from failing due to ongoing processes or animations. Detox has first-class React Native support and does not rely on WebDriver. It allows you to write unified tests that work for both iOS and Android, while still supporting platform-specific code. Today, we will be using a demo project, a basic movies app, to learn and practice writing tests with Detox.

So what does it mean in reality? The way detox works, it synchronizes with your application and working by this super simple graph. So it waits for up to go idle and like continuously every like millisecond checking, if app is idle and in case it is not, it will still wait until it will go idle. And only once it is, it will proceed. And basically it will prevent you from failing on many, many cases. You are entering some screen, you had to lower the running, and like especially happens a lot on web, you just press the button which was not present and your test failed because your app was still like doing some requests or like still loading some animation or something and app they can test failed because app didn't actually play. This framework didn't actually wait for app to go idle and deduct does it. So it saves a lot of tests from failing and makes it a lot more stable. Another thing is that D-tox is cross-platform meaning you can write the same test, it will work for both iOS and Android, which is super great. You can also have some platform specific code like using some platform specific gestures or some specific like components that only like iOS have or Android, but still you can use something unified and still have one test that will run for both Android and iOS, which we will actually try out today. D-tox does not rely on WebDriver, it was built for mobile applications from scratch and it has the first class React Native support. And today we will be actually playing with it a bit. We will use the React Native app to do everything with the D-tox, and this is actually D-tox in action. I will be showing you the writing tests today, and we will be in, like, the detailed agenda will be in a few minutes, but we will try many different actions learn some tips and best practices, and here you can just see the way it works. So we will work with the demo project that I've prepared for this workshop. It is a super basic movies app. I hope you love movies, because I do. And this app was prepared for the workshop. It is super easy, and it has some components added, so we will play with it. And I really hope that you had a chance to clone it already. Install everything, and, like, made it work. In case you didn't, please do right now. You have the link in the Discord Channel in the top, so just find it and make sure you follow the prerequisites, so we can... I highly encourage you to do it hands-on with me.