Monkey Testing with Selenium and WebdriverIO

Learn how to perform Monkey Testing with Selenium and WebdriverIO using this guide.

Guide Banner Image
Home Guide Monkey Testing with Selenium and WebdriverIO

Monkey Testing with Selenium and WebdriverIO

Monkey testing as a technique is beneficial for identifying edge cases that structured test cases may miss.

Overview

What is Monkey Testing?

Monkey testing involves interacting with an app in unpredictable ways, like random clicks, keystrokes, and gestures, to simulate real-world user behavior and expose hidden issues.

Importance of Monkey Testing in Selenium

Monkey testing complements structured Selenium test cases by introducing randomness, helping teams find bugs that may not surface during planned testing.

How to Perform Monkey Testing with Selenium

  1. Use tools like Gremlins.js or custom scripts to trigger random events
  2. Integrate monkey testing logic with WebDriverIO or Selenium WebDriver
  3. Run tests on real browsers and devices for accurate results
  4. Monitor app behavior and capture logs or screenshots on failures
  5. Include monkey tests in CI/CD pipelines for early detection

This article explains how to perform monkey testing using Selenium and WebDriverIO, the tools required, challenges to expect, and best practices to follow.

What is Monkey Testing?

Monkey testing is a software testing technique in which testers performs random action on the application without having predefined test cases and verifies if the application behaves normally. The main purpose of Monkey Testing is to find the bugs and errors in the software application using the experimental approach.

Benefits of Monkey Testing

Monkey testing is functional when you want to check your application’s behavior under random and unexpected user actions. It adds another layer of confidence, especially after testing core functionality.

Key benefits include:

  • Catches bugs missed in regular tests: Random inputs can trigger edge cases that scripted tests often miss.
  • Checks system stability: It helps see if the application crashes or slows down under unpredictable use.
  • Quick to start: You don’t need detailed test cases or planning. It works with minimal setup.
  • Works well with other test methods: You can use it alongside manual and automated tests to increase coverage.
  • Useful for UI testing: It exposes issues in how the UI responds to repeated clicks, typing, or navigation.
  • Supports automation: You can automate monkey testing using tools like WebDriverIO with plugins or libraries.

Why Use Monkey Testing in Selenium Projects

Monkey testing helps uncover edge cases that structured test scripts may miss. It helps identify crashes or unhandled errors when users interact randomly with the UI.

Selenium-based projects can complement regular test automation.

Use monkey testing in Selenium to:

  • Check how the app handles unexpected or random inputs
  • Discover crashes and bugs that don’t follow a user flow
  • Improve application resilience under unpredictable usage
  • Test app stability over time with continuous random input

How to Perform Monkey Testing with Selenium in Different Languages

Selenium supports multiple programming languages, allowing monkey testing to be implemented in the team’s existing stack.

Here’s how it generally works:

  • Java: Use loops and random generators to simulate clicks, keystrokes, and navigations on web elements
  • Python: Combine Selenium with libraries like random to select elements and simulate random input
  • JavaScript (Node.js): Use tools like Gremlins.js in browser environments, or build logic using WebDriver methods
  • C#: Generate random actions using .NET libraries and feed them into Selenium scripts

Each language requires handling element detection, exception handling, and logging to track random events and results.

Monkey Testing using WebdriverIO

WebdriverIO is an open-source test automation framework managed by the OpenJS foundation. It supports writing the tests in Javascript and Typescript programming languages. WebdriverIO uses the Selenium WebDriver under the hood.

Here is how to perform monkey testing using WebdriverIO

Pre-Requisite:

Setup WebdriverIO

Write a WebdriverIO script to perform Monkey Testing

Here is an example of monkey testing

Scenario:

  1. Generate a random number between 1 to 5
  2. Based on the random number, perform one of the actions
describe('Monkey Testing Demo', () => {

  it('Interact with different elements randomly', async () => {



    await browser.maximizeWindow()

    

    // Open the target url

    await browser.url('https://www.browserstack.com');



    // Accept cookies

    await browser.$('#accept-cookie-notification').click();//Accept Cookies



    // Perform monkey testing by generating random events on the page

    for (let i = 0; i < 100; i++) {

      // Generate a random number between 1 and 5

      const randomNumber = Math.floor(Math.random() * 5) + 1;



      // Based on the random number, perform a random action on the page

      switch (randomNumber) {

        case 1:

          // Click on a random link on the page

          const links = await browser.$$('a');

          const randomLinkIndex = Math.floor(Math.random() * links.length);

          try {

            await links[randomLinkIndex].click();

          }

          catch(err) {

            console.log(err)

          }

          break;

        case 2:

          // Enter a random string into a text field

          const inputs = await browser.$$('input[type="text"]');

          if (inputs.length > 0) {

            const randomInputIndex = Math.floor(Math.random() * inputs.length);

            try {

              await inputs[randomInputIndex].setValue(`Random string ${i}`);

            }

            catch(err) {

              console.log(err)

            }

          }

          break;

        case 3:

          // Click on a random button on the page

          const buttons = await browser.$$('button');

          if (buttons.length > 0) {

            const randomButtonIndex = Math.floor(Math.random() * buttons.length);

            try {

              await buttons[randomButtonIndex].click();

            }

            catch(err) {

              console.log(err)

            }

          }

          break;

        case 4:

          // Select a random option from a dropdown menu

          const selects = await browser.$$('select');

          if (selects.length > 0) {

            const randomSelectIndex = Math.floor(Math.random() * selects.length);

            const options = await selects[randomSelectIndex].$$('option');

            const randomOptionIndex = Math.floor(Math.random() * options.length);

            try {

              await options[randomOptionIndex].click();

            }

            catch(err) {

              console.log(err)

            }

          }

          break;

        case 5:

          // Click on a random element on the page

          const elements = await browser.$$('*');

          if (elements.length > 0) {

            const randomElementIndex = Math.floor(Math.random() * elements.length);

            try {

              await elements[randomElementIndex].click();

            }

            catch(err) {

              console.log(err)

            }

          }

          break;

        default:

          // Do nothing

          break;

      }

    }

  })

});

Understanding the code

The above code performs random actions on the webpage. First, it generates a random number between 1 to 5 and based on the random number it enters one of the five switch use cases.

We have designed 5 switch cases:

  1. Click the random link on page
  2. Enter the random string on page
  3. Click on the random button on page
  4. Select a random option on the drop-down menu
  5. Click on the random element on the page

every time when you execute the test. The behavior of the test will be different, and based on the random number, the code gets executed.

Here is one use case. The random number generated is 1. now the test enters Case 1 and gets the links available in the webpage using the code

const links = await browser.$$('a');

Next, the random number will be generated again based on the number of links available in the webpage

const randomLinkIndex = Math.floor(Math.random() * links.length);

Finally, it performs the click on one of the random links. Execute the Monkey test using WebdriverIO. You can use the below command to execute the tests

npx wdio run ./wdio.conf.js

Wait for the execution to complete. The command line results can be seen below.

The script randomly selected links from the Products section in the footer; some were repeated, while some were skipped, but the system handled it without issues, and all tests passed.

However, running this action repeatedly may lead to unexpected behavior depending on the app. Monkey testing is useful for uncovering random bugs but shouldn’t be the sole testing method, as it lacks full coverage.

While automated monkey testing reduces manual effort, it’s less effective. Combining it with manual testing across devices and platforms is a better approach.

Types of Monkey Testing

Monkey testing is divided into three types, based on how random inputs are generated and how much context the test has:

  1. Dumb Monkey Testing: The tool interacts with the app randomly, without knowledge of UI structure or flow. It’s useful for stress testing, but can be inefficient.
  2. Smart Monkey Testing: The tool understands the app’s UI elements and behavior. It targets input fields, buttons, and flows logically, improving coverage.
  3. Brilliant Monkey Testing: This type uses domain knowledge. It behaves like a real user and covers meaningful user journeys. It’s the most efficient but also the most complex to build.

Tools and Libraries for Monkey Testing in Selenium

Monkey testing can be written from scratch, but several tools help simulate random user behavior more efficiently.

  • Gremlins.js: JavaScript library that runs in-browser to simulate user events like clicks, scrolls, and input
  • Randoma11y: Injects random actions and accessibility checks into web pages (used with Gremlins.js)
  • Faker: Generates fake data to feed into form fields and inputs (available in most languages)
  • Custom logic: Developers often write scripts to pick random elements and trigger actions using standard Selenium methods

BrowserStack Automate Banner

Automate Monkey Testing With WebDriverIO on an Online Selenium Grid

Running monkey tests locally has limitations, such as limited browsers, OS, and scalability. Using BrowserStack Automate, you can run monkey tests powered by WebDriverIO at scale on real devices and browsers.

Benefits of using BrowserStack:

  • Run tests on 3500+ real browsers and devices without local setup
  • Access real user conditions to identify environment-specific issues
  • Use parallel testing to reduce feedback time
  • Capture detailed logs, screenshots, and videos for easier debugging
  • Integrate into CI pipelines with minimal configuration

This approach increases test coverage, reduces setup effort, and helps teams catch random behavior issues before production.

Common Challenges of Monkey Testing in Selenium Projects

Monkey testing is simple to star, but comes with some practical issues.

  • Reproducibility: Random actions make it difficult to reproduce bugs
  • False positives: Unexpected crashes might result from invalid input not relevant to real usage
  • Performance: Long test runs or memory issues may occur when running unbounded random actions
  • Element targeting: Scripts may fail if the logic cannot handle dynamic or hidden elements
  • Debugging failures: Without proper logging, it can be hard to trace what caused an error

Best Practices for Effective Monkey Testing with Selenium

To make monkey testing useful in real projects, follow a few key guidelines:

  • Set boundaries: Limit random actions to areas where it makes sense (e.g., logged-in state, specific page)
  • Log everything: Capture actions, time, and DOM state before and after each event
  • Use hybrid tests: Combine monkey testing with some structured flows for better coverage
  • Avoid destructive actions: Skip elements that could delete data or log users out
  • Run tests on real browsers: Use cloud platforms like BrowserStack to replicate real environments

These practices help reduce noise and focus on catching real issues that impact users.

Integrating Monkey Testing into CI/CD Pipelines

Monkey testing can be part of your build process to detect regressions early.

How to add it:

  1. Include monkey test scripts in your test suite.
  2. Run them in parallel with structured tests using CI tools like Jenkins, GitHub Actions, or CircleCI.
  3. Use BrowserStack Automate to run tests on real browsers without maintaining infrastructure.
  4. Set time limits or seed values to control randomness.
  5. Store logs and videos as build artifacts for debugging.

This allows teams to catch random behavior issues early in development and ship more stable software.

Talk to an Expert

Conclusion

Monkey testing is a valuable technique for uncovering unpredictable bugs that structured tests may overlook.

Combined with Selenium and the right tools, it helps improve application stability under real-world usage.

By integrating monkey testing into your CI/CD workflows and running tests on real devices using platforms like BrowserStack, teams can catch hidden issues early and release more reliable software.

Tags
Automated UI Testing Automation Testing Website Testing