How to Verify that an Element is Not Visible in WebDriverIO in Mocha JavaScript
Image by Yvett - hkhazo.biz.id

How to Verify that an Element is Not Visible in WebDriverIO in Mocha JavaScript

Posted on

If you’re reading this, chances are you’re stuck trying to figure out how to verify that an element is not visible in WebDriverIO using Mocha JavaScript. Don’t worry, we’ve all been there! In this article, we’ll walk you through the steps to achieve this, and by the end of it, you’ll be a pro at verifying invisible elements.

What is WebDriverIO?

Before we dive into the solution, let’s quickly cover what WebDriverIO is. WebDriverIO is a browser automation framework that allows you to write tests for your web application using JavaScript. It’s built on top of the WebDriver protocol and provides a user-friendly API for interacting with web elements. WebDriverIO is widely used in conjunction with Mocha, a popular JavaScript testing framework.

Why Verify Invisible Elements?

Verifying that an element is not visible might seem trivial, but it’s an essential part of ensuring your web application behaves as expected. Think about it: what if a critical error message is supposed to be hidden from the user, but your test doesn’t account for it? You might end up with false positives or, worse, missed bugs.

By verifying invisible elements, you can:

  • Ensure that sensitive information is not exposed to users
  • Validate that loading indicators or spinners are correctly hidden
  • Test that errors or warnings are properly displayed or hidden
  • Improve overall test reliability and confidence

The Problem: Invisible Elements in WebDriverIO

In WebDriverIO, verifying that an element is not visible can be tricky. Unlike other test automation frameworks, WebDriverIO doesn’t provide a built-in method for checking element visibility. This means you’ll need to get creative with your testing approach.

The Solution: Using `isVisible` and `waitForVisible`

The secret to verifying invisible elements in WebDriverIO lies in using the `isVisible` and `waitForVisible` methods. These methods allow you to check whether an element is visible or not, and even wait for an element to become visible or invisible.

Method 1: Using `isVisible`

The `isVisible` method returns a boolean indicating whether the element is visible or not. You can use this method to verify that an element is not visible:

const elem = await browser.$('selector');
expect(elem.isVisible()).to.be.false;

In this example, we use the `$` method to retrieve an element with the specified selector. Then, we call the `isVisible` method on the element and assert that it returns `false`, indicating that the element is not visible.

Method 2: Using `waitForVisible` with a Timeout

An alternative approach is to use the `waitForVisible` method with a timeout. This method waits for the element to become visible within the specified timeout period. If the element doesn’t become visible within the timeout, the method will throw an error:

const elem = await browser.$('selector');
try {
  await elem.waitForVisible({ timeout: 1000 });
  throw new Error('Element should not be visible');
} catch (err) {
  // Element is not visible, test passes
}

In this example, we try to wait for the element to become visible within a 1-second timeout. If the element becomes visible, we throw an error to indicate that the test has failed. If the timeout expires without the element becoming visible, the test passes, indicating that the element is indeed not visible.

Method 3: Using `waitForNotVisible`

WebDriverIO also provides a `waitForNotVisible` method, which is specifically designed for waiting for an element to become invisible:

const elem = await browser.$('selector');
await elem.waitForNotVisible({ timeout: 1000 });
// Element is not visible, test passes

In this example, we use the `waitForNotVisible` method to wait for the element to become invisible within a 1-second timeout. If the element becomes invisible within the timeout, the test passes.

Best Practices for Verifying Invisible Elements

To ensure reliable tests, follow these best practices when verifying invisible elements in WebDriverIO:

  1. Use the correct selector**: Make sure you’re using the correct selector to retrieve the element. A poorly chosen selector can lead to false positives or false negatives.
  2. Wait for the element to stabilize**: Elements can take time to render or become visible/invisible. Use `waitForVisible` or `waitForNotVisible` to wait for the element to stabilize before verifying its visibility.
  3. Set a reasonable timeout**: Choose a timeout that allows for the element to become visible or invisible within a reasonable timeframe. A too-short timeout can lead to test failures, while a too-long timeout can slow down your test suite.
  4. Verify the element’s existence**: Before verifying visibility, ensure that the element exists in the DOM using `browser.$(‘selector’).isExisting()`.

Conclusion

Verifying that an element is not visible in WebDriverIO using Mocha JavaScript can be a challenge, but with the right approach, you can ensure that your tests are reliable and accurate. By using `isVisible`, `waitForVisible`, and `waitForNotVisible` methods, you can confidently verify invisible elements and improve the overall quality of your test suite. Remember to follow best practices, and you’ll be well on your way to becoming a WebDriverIO master!

Method Description
`isVisible` Returns a boolean indicating whether the element is visible or not.
`waitForVisible` Waits for the element to become visible within a specified timeout period.
`waitForNotVisible` Waits for the element to become invisible within a specified timeout period.

Now that you’ve mastered verifying invisible elements, go ahead and tackle those pesky tests! If you have any questions or need further clarification, feel free to ask in the comments below.

Frequently Asked Question

Get ready to uncover the secrets of verifying the invisible elements in WebDriverIO with Mocha JavaScript!

How do I verify that an element is not visible using WebDriverIO in Mocha JavaScript?

You can use the `isVisible` command and assert its return value to be `false`. For example: `expect(await $(‘selector’).isVisible()).to.be.false;`. This will verify that the element is not visible.

What if the element is not present in the DOM at all?

In that case, you can use the `isExisting` command to check if the element exists in the DOM. If it doesn’t, `isExisting` will return `false`. For example: `expect(await $(‘selector’).isExisting()).to.be.false;`.

How can I verify that an element is not visible due to CSS styles?

You can use the `getCSSProperty` command to retrieve the value of a specific CSS property, such as `display` or `visibility`. For example: `expect(await $(‘selector’).getCSSProperty(‘display’)).to.equal(‘none’);`.

What if I need to verify that an element is not visible due to it being hidden by another element?

In this case, you can use the `getBoundingBox` command to get the element’s bounding box, and then check if it’s fully or partially obscured by another element. For example: `const boundingBox = await $(‘selector’).getBoundingBox(); expect(boundingBox.width * boundingBox.height).to.equal(0);`.

How can I make my code more readable and maintainable when verifying element visibility?

You can create a custom helper function or a page object method to encapsulate the verification logic. For example: `async isElementNotVisible(selector) { return !(await $(selector).isVisible()); }`. This will make your test code more concise and easier to understand.

Leave a Reply

Your email address will not be published. Required fields are marked *