Blogs

Get useful information on apps testing and development

Automating Mobile App Testing with Appium and Nightwatch.js

Mobile app testing is a critical aspect of the app development process. It ensures that your application functions correctly across various devices, operating systems, and screen sizes. Manual testing can be time-consuming and error-prone, making automation a valuable solution. Automating mobile app testing is essential for ensuring the reliability and quality of your mobile applications. Appium and Nightwatch.js provide a powerful combination for achieving this goal. With the ability to write tests in JavaScript, interact with mobile elements, and perform assertions, you can thoroughly test your mobile apps across various platforms and devices. Before we deep dive into how to achieve automation using Appium & Nightwatch.js Let’s get the basics right.

 

What is Appium?

 

Appium is an open-source automation tool for testing mobile applications on different platforms, including iOS and Android. It allows you to write tests using your preferred programming language (Java, Python, JavaScript, etc.) and provides a unified API to interact with both native and hybrid mobile apps. Appium also supports testing on simulators/emulators as well as physical devices, making it a versatile choice for mobile app testing.

 

What is Nightwatch.js?

 

Nightwatch.js is an automated testing framework for web and mobile applications. It is built on top of Node.js and provides an easy-to-use and flexible approach to writing end-to-end tests. Nightwatch.js allows you to write tests using JavaScript and offers a wide range of commands and assertions to interact with web and mobile elements. It can be seamlessly integrated with Appium to extend your test coverage to mobile apps.

 

Setting up the Environment

 

Before diving into mobile app testing with Appium and Nightwatch.js, you need to set up your development environment. Here’s a step-by-step guide:

 

  1. Install Node.js: Both Appium and Nightwatch.js rely on Node.js. Install it by downloading the latest version from the official website or using a package manager like npm (Node Package Manager).
  1. Install Appium: Use npm to install Appium globally on your system:
npm install -g appium
  1. Install Appium Server: Install the Appium server to handle device communication. You can do this by running:
appium server
  1. Install Appium Client Library: Depending on your preferred programming language, install the Appium client library. For JavaScript, you can install it using npm:
npm install appium
  1. Install Nightwatch.js: Install Nightwatch.js globally using npm:
npm install -g nightwatch
  1. Configure Nightwatch.js: Create a Nightwatch.js configuration file (nightwatch.json) in your project directory. Configure it to use the Appium WebDriver for mobile testing. Here’s a basic example:
{   “src_folders”: [“tests”],   “webdriver”: {     “start_process”: true,     “server_path”: “node_modules/.bin/appium”,     “port”: 4723,     “host”: “127.0.0.1”,     “default_path_prefix”: “/wd/hub”,     “test_settings”: {       “default”: {         “desiredCapabilities”: {           “platformName”: “Android”,           “deviceName”: “emulator-5554”,           “app”: “path/to/your/app.apk”         }       }     }   } }
  1. Write Your Tests: Create test scripts using JavaScript in the specified test folder (e.g., “tests” in the above configuration).

 

 

Writing Your First Test

Now that your environment is set up, let’s write a simple test using Nightwatch.js and Appium. This test will open a mobile app, interact with it, and make assertions.

module.exports = {   ‘Test Mobile App’: function (browser) {     browser       .url(‘appium://localhost:4723/wd/hub’)       .waitForElementVisible(‘[@text=”Login”]’, 5000)       .click(‘[@text=”Login”]’)       .waitForElementVisible(‘[@resource-id=”username”]’, 5000)       .setValue(‘[@resource-id=”username”]’, ‘your_username’)       .setValue(‘[@resource-id=”password”]’, ‘your_password’)       .click(‘[@text=”Submit”]’)       .waitForElementVisible(‘[@text=”Welcome, User!”]’, 5000)       .assert.containsText(‘[@text=”Welcome, User!”]’, ‘Welcome, User!’)       .end();   }, };

In this example:

  • We navigate to the Appium server running on localhost and port 4723.
  • We locate and interact with elements using selectors like ‘[@text=”Login”]’ and ‘[@resource-id=”username”]’.
  • We set values for username and password fields.
  • We click the submit button and then assert that the “Welcome, User!” text is present.

Running Your Tests

To execute your tests, use the following command in your project’s root directory:

nightwatch

Nightwatch.js will read your configuration, launch the Appium server, and execute your tests on the specified mobile device or emulator.

Benefits of Using Appium with Nightwatch.js

Using Appium in combination with Nightwatch.js provides a powerful and versatile solution for automating mobile app testing. This pairing allows you to write maintainable, cross-platform tests with ease, leverage the extensive capabilities of JavaScript, and integrate seamlessly into your CI/CD pipeline. Here are a few advantages of using this power packed testing automation combination to ensure quality and reliability of your mobile applications while streamlining your testing process.

  1. Cross-Platform Support: Appium is a cross-platform automation tool, supporting both iOS and Android. When combined with Nightwatch.js, you can write tests that work seamlessly on multiple platforms, reducing the need for separate testing scripts for each platform.
  1. Unified Testing Framework: Nightwatch.js provides a unified testing framework for both web and mobile applications. This means you can use a single testing framework to automate tests for your web and mobile apps, reducing the learning curve and maintenance effort.
  1. Wide Range of Commands: Nightwatch.js offers a comprehensive set of built-in commands and assertions specifically designed for automating web and mobile interactions. This makes it easier to simulate user interactions, perform validations, and handle various scenarios in your tests.
  1. Ease of Use: Both Appium and Nightwatch.js are known for their simplicity and ease of use. Nightwatch.js’s declarative syntax makes it particularly accessible to developers and testers, allowing them to write tests quickly and effectively.
  1. JavaScript-Based: JavaScript is one of the most popular programming languages, and many developers are already familiar with it. By using Nightwatch.js, you can leverage JavaScript’s extensive ecosystem and libraries to enhance your test automation efforts.
  1. Parallel Test Execution: Nightwatch.js supports parallel test execution, allowing you to run tests concurrently on multiple devices or emulators. This significantly reduces test execution time and helps identify issues more quickly.
  1. Integration with Continuous Integration (CI) Systems: Nightwatch.js can be easily integrated with popular CI/CD systems like Jenkins, Travis CI, CircleCI, and others. This enables you to automate the testing process as part of your development pipeline, ensuring that tests are run automatically with every code change.
  1. Community and Support: Both Appium and Nightwatch.js have active communities and extensive documentation. You can find a wealth of resources, tutorials, and plugins to extend their functionality and address specific testing needs.
  2. Device Farm Compatibility: Appium supports testing on various cloud-based device farms, such as AWS Device Farm and Sauce Labs. Integrating Appium with Nightwatch.js allows you to leverage these services for efficient and scalable testing across a wide range of devices.
  1. Open Source and Cost-Effective: Appium and Nightwatch.js are open-source tools, meaning they are freely available for use. This can help reduce the overall cost of your test automation efforts compared to using proprietary testing solutions.
  1. Flexibility in Test Scripting: Appium allows you to write test scripts in multiple programming languages, while Nightwatch.js uses JavaScript. This flexibility allows you to choose the language that best suits your team’s skills and preferences.

Conclusion

Leveraging the dynamic duo of Appium and Nightwatch.js for mobile app automation testing presents a powerful solution that is both versatile and robust. This combination offers cross-platform support, unified testing capabilities, a rich set of commands and assertions, ease of use, and scalability. It streamlines testing processes, supports parallel execution, integrates seamlessly with CI/CD pipelines, and encourages best practices such as the Page Object Model. Additionally, it provides comprehensive device and platform coverage, synchronization mechanisms, and robust reporting features. By embracing Appium and Nightwatch.js, organizations can ensure the quality and reliability of their mobile applications while optimizing their development workflow and reducing testing overhead.

Automate with Appium

Automate your mobile app testing with Appium. Run your test scripts on multiple real devices and accelerate your testing.

R Dinakar

Dinakar is a Content Strategist at Pcloudy. He is an ardent technology explorer who loves sharing ideas in the tech domain. In his free time, you will find him engrossed in books on health & wellness, watching tech news, venturing into new places, or playing the guitar. He loves the sight of the oceans and the sound of waves on a bright sunny day.

Recent Posts