How to use test_should_wait_for_enabled_button method in Playwright Python

Best Python code snippet using playwright-python

test_element_handle_wait_for_element_state.py

Source: test_element_handle_wait_for_element_state.py Github

copy

Full Screen

...53 div = page.query_selector("div")54 page.evaluate("setTimeout(() => div.remove(), 250)")55 div.wait_for_element_state("hidden")56 assert div.is_hidden()57def test_should_wait_for_enabled_button(page, server):58 page.set_content("<button id=button disabled><span>Target</​span></​button>")59 span = page.query_selector("text=Target")60 page.evaluate("setTimeout(() => button.disabled = false, 250)")61 assert span.is_enabled() is False62 span.wait_for_element_state("enabled")63 assert span.is_enabled()64def test_should_throw_waiting_for_enabled_when_detached(page):65 page.set_content("<button id=button disabled>Target</​button>")66 button = page.query_selector("button")67 page.evaluate("setTimeout(() => button.remove(), 250)")68 with pytest.raises(Error) as exc_info:69 button.wait_for_element_state("enabled")70 assert "Element is not attached to the DOM" in exc_info.value.message71def test_should_wait_for_disabled_button(page):...

Full Screen

Full Screen

StackOverFlow community discussions

Questions
Discussion

Why are the values yielded by a pytest fixture and a function called directly different?

playwright-python advanced setup

Scrapy playwright scrolldown and wait to load the html

How to pass a variable out of a lambda

Using playwright for Python, how do I wait for two different selectors/handles at the same time and take the first successful match?

Trouble in Clicking on Log in Google Button of Pop Up Menu Playwright Python

How to find partial text using Playwright

What are the differences between Python Playwright sync vs. async APIs?

Problem with selecting a specific web element with Playwright in Python

Can Playwright be used to launch a browser instance

For the 1st form:

def get_playwright():
    with sync_playwright() as playwright:
        yield playwright

print(get_playwright())  # <generator object get_playwright at 0x108aac580>

That is expected because get_playwright is a generator, which returns a generator iterator, which you have to call next(...) on to get each yielded value from the iterator.

Consider a simpler, non-playwright example:

In [14]: def generate_nums():
    ...:     for num in range(10):
    ...:         yield num
    ...: 

In [15]: nums = generate_nums()

In [16]: nums
Out[16]: <generator object generate_nums at 0x11115e6d0>

In [17]: next(nums)
Out[17]: 0

In [18]: next(nums)
Out[18]: 1

In [19]: next(nums)
Out[19]: 2

For more examples, see Understanding generators in Python.

Since your get_playwright returns an iterator, you need to call next() once to get the actual object:

from playwright.sync_api import sync_playwright


def get_playwright():
    with sync_playwright() as playwright:
        yield playwright

playwright_generator = get_playwright()
print(playwright_generator)  # <generator object get_playwright at 0x104031580>

playwright = next(playwright_generator)
print(playwright)  # <playwright._impl._playwright.Playwright object at 0x1041aabb0>

For the 2nd form:

@pytest.fixture()
def get_playwright():
    with sync_playwright() as playwright:
        yield playwright

def test(get_playwright):
    print(get_playwright)

It should be the same case, but it's just that pytest automatically calls next() on the fixture value if it's a generator. I could not find documentation for this behavior from the pytest docs, but it was mentioned by one of the pytest author's/maintainer's in a different answer:

Here's roughly the execution here

  • pytest notices your fixture is used for the test function
  • pytest calls the fixture function
    • since it is a generator, it returns immediately without executing code
  • pytest notices it is a generator, calls next(...) on it
    • this causes the code to execute until the yield and then "pausing". you can think of it kind of as a co-routine ...
  • pytest then executes your test function

...which is probably why the value passed to your test function is already the next-ed value, the playwright object.

https://stackoverflow.com/questions/72027987/why-are-the-values-yielded-by-a-pytest-fixture-and-a-function-called-directly-di

Blogs

Check out the latest blogs from LambdaTest on this topic:

How To Choose The Best JavaScript Unit Testing Frameworks

JavaScript is one of the most widely used programming languages. This popularity invites a lot of JavaScript development and testing frameworks to ease the process of working with it. As a result, numerous JavaScript testing frameworks can be used to perform unit testing.

Selenium 4.0 and The Future: Manoj Kumar [Testμ 2022]

We were eager to listen to Manoj Kumar, VP Developer Relations, LambdaTest, speak on the importance of Selenium 4.0 and how bright the future is. This was the agenda of the speech:

Top Cross Browser Testing Trends [2022]

With the rapid evolution in technology and a massive increase of businesses going online after the Covid-19 outbreak, web applications have become more important for organizations. For any organization to grow, the web application interface must be smooth, user-friendly, and cross browser compatible with various Internet browsers.

11 Best Automated UI Testing Tools In 2022

The web development industry is growing, and many Best Automated UI Testing Tools are available to test your web-based project to ensure it is bug-free and easily accessible for every user. These tools help you test your web project and make it fully compatible with user-end requirements and needs.

Open e2e Test Initiative with Open MCT: John Hill [Testμ 2022]

Open MCT is a next-generation mission control framework for data visualization on desktop and mobile devices. It was created at NASA’s Ames Research Center, and NASA uses it to analyze spacecraft mission data.

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful