Our goal in this tutorial is to understand how to leverage automated testing tools for hybrid apps. Hybrid apps, as you might know, are applications that can run on multiple platforms like Android, iOS, and the web. They are built using HTML, CSS, and JavaScript and wrapped in a native application using platforms like Cordova.
By the end of this tutorial, you will:
- Understand what automated testing is and why it's crucial for hybrid apps
- Get to know about a few popular automated testing tools
- Learn how to use these tools to write and execute tests
Automated testing is a process that validates if the software or an application is working correctly. It does this by running predefined test cases automatically. The main advantage is that it can quickly test large parts of your codebase, freeing up time for developers to focus on writing code.
For hybrid apps, we can use tools like Appium, Detox, and Jest for automated testing. We'll focus on Appium in this tutorial, which is an open-source tool used for automating mobile, web, and hybrid applications on iOS mobile, Android mobile, and Windows desktop platforms.
First, we need to install Appium. You can do so by running the following command in your terminal:
npm install -g appium
After installing Appium, we can write our tests. Appium supports writing tests in multiple programming languages like Java, Ruby, Python, PHP, JavaScript, etc. For this tutorial, we'll write our tests in JavaScript.
// Import wd (WebDriver), which is a library to automate browsers and mobile devices
let wd = require("wd");
// Set up desired capabilities
let desiredCaps = {
platformName: "Android",
deviceName: "Android Emulator",
app: "/path/to/your/.apk/file"
};
// Initialize the driver
let driver = wd.promiseChainRemote("localhost", 4723);
driver
.init(desiredCaps)
.then(function () {
return driver.elementByAccessibilityId("TestApp");
})
.then(function (el) {
return el.click();
})
.then(function () {
return driver.quit();
})
.done();
In the above code, we first import wd
, set up the desired capabilities (like platform name, device name, and the path to your .apk file), and initialize the driver. We then find the element with the accessibility id "TestApp", click on it, and quit the driver.
Let's say we want to test a login functionality of our app. Here's how you can do it:
driver
.init(desiredCaps)
.then(function () {
return driver.elementById("username");
})
.then(function (el) {
return el.type("testuser");
})
.then(function () {
return driver.elementById("password");
})
.then(function (el) {
return el.type("testpassword");
})
.then(function () {
return driver.elementById("login");
})
.then(function (el) {
return el.click();
})
.then(function () {
return driver.quit();
})
.done();
In the above code, we find the username and password fields, type in our test username and password, click on the login button, and quit.
In this tutorial, we've learned about automated testing and how we can use tools like Appium to automate our tests. We've also learned how to write tests and execute them.
Write a test to check if a button with the id "testButton" is present in your app.
Write a test to check if clicking on a button with the id "testButton" takes you to a new screen with the title "Test Screen".
Automated testing is a powerful tool for any developer, and we've only scratched the surface. To learn more, you can explore other automated testing tools, different types of testing like unit testing, integration testing, etc., and how to use them in your projects.