Rayrun
← Back to Discord Forum

Does isVisible() wait or not?

samhartmanncomposted in #help-playwright
Open in Discord
samhartmanncom
samhartmanncom

I'm a bit confused: The docs say that locator.isVisible() does not wait for the element to become visible and returns immediately. But the Usage example has an await at the beginning.

Does the method wait or not?

This thread is trying to answer question "Does the `locator.isVisible()` function wait for the element to become visible or does it return immediately?"

0 replies

Answer

Understanding locator.isVisible() in Playwright

In Playwright, locator.isVisible() checks if an element is visible right away. It doesn't wait for the element to become visible.

Here's how it works:

import { test } from '@playwright/test';

test('Check visibility', async ({ page }) => {
  await page.goto('https://ray.run');
  const locator = page.locator('.your-selector');
  const isVisible = await locator.isVisible();
  console.log(isVisible);
});

If the selector doesn't match any elements, Playwright considers it not visible. So, if there are no matching elements in the DOM, locator.isVisible() will return false.

This might be different from what you're used to with other frameworks. Some wait for an element to become visible before returning a result. But with Playwright, you need to handle waiting explicitly if needed.

If you want to wait for an element to become visible, you can use waitFor(). This method lets you specify conditions based on various states of an element.

import { test } from '@playwright/test';

test('Wait for visibility', async ({ page }) => {
  await page.goto('https://ray.run');
  const locator = page.locator('.your-selector');
  await locator.waitFor('visible');
  // Perform further actions or assertions
});

So, remember, locator.isVisible() in Playwright returns immediately. If you need to wait for visibility changes, use waitFor().

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].