banner



How To Open New Window In Python

Larn how to take action against new browser windows using Microsoft's automation library.

Photo by Adrien Siami on Unsplash

New windows and popups accept historically been a hurting point for test automation engineers. While Selenium can handle windows, the results can be finicky due to browser focus. Some tools such as Cypress intentionally practise not let for new window actions to accept place.

Microsoft's Playwright was congenital with the express purpose of handling web interactions gracefully. Information technology comes as no surprise and then, that windows and popups are piece of cake to manage using Playwright.

This tutorial will detail how to capture new windows using Playwright and how to make assertions against them. We will use DemoQA's Browser Windows folio as a base.

Getting Started

You lot will need to install the post-obit packages using Pip or the package managing director of your choosing:

  • Playwright
  • Pytest
  • Pytest-Playwright

The pytest-playwright library is maintained past the creators of Playwright. It comes with a agglomeration of useful fixtures and methods for engineering convenience.

Edifice a Test

The DemoQA Browser Windows page features 3 buttons:

  • New tab
  • New window
  • New window with a message

For this tutorial, nosotros volition focus on the new window button. Clicking the button opens a new window with text on the folio. There are no other interactions to consider.

Our user journeying will be to navigate to the browser windows page, select the new window button, then verify that a window with text loads.

          def test_new_browser_window(page):
"""Test that a new window may be opened.
:param page: A Playwright browser page
"""
page.goto("https://world wide web.demoqa.com/browser-windows")
folio.wait_for_selector("#windowButton")

In the above code, we navigate to the browser windows page, and so wait for the window button to be visible. Before getting too far, we should take a moment to better understand how window interactions occur within Playwright.

Context Needed

Playwright opens new windows in what is termed a browser context. Each Playwright session starts with a hierarchy of browser -> context -> folio OR browser -> folio, depending on whether a context is used or not.

One unique browser (Chromium, Firefox, or Webkit) may exist at whatsoever given fourth dimension. However, a browser may incorporate multiple contexts and multiple pages within each context.

Opening a new window in Playwright adds a folio to an existing browser context. This means that all new windows within a specific context inherit items like storage state and cookies.

Opening Windows

Using the following lawmaking, we tin capture a window opening on a specific consequence, such every bit clicking on a page chemical element or navigating to a specific URL.

In our test case, clicking the #windowButton element opens a new window in the browser context.

          with page.context.expect_page() as window:
page.click("#windowButton")
new_window = window.value

If you have chosen non to employ the pytest-playwright library, the lawmaking will look marginally different:

          with context.expect_page() as window:
page.click("#windowButton")
new_window = window.value

The expect_page method returns an EventContextManager which is invoked using the with statement. The variable window is an EventInfo class returned from the context manager. We can access the class using the value property which returns a Playwright Folio object.

Binding new_window to the result of window.value allows us to have activeness against the new page, such as creating an Element Handle.

          heading = new_window.wait_for_selector("#sampleHeading")        

Nosotros tin use a Playwright assertion to run a simple visibility check against the new element.

          visible = heading.is_visible()        

Origins Unknown

Should a window open without a clear origination, we can handle it with the following:

          def handle_page(folio):
page.wait_for_load_state()
impress(page.url)
folio.on("folio", handle_page)

The handle_page function volition run for every new page opened within the browser context and print the URL for the new window.

Examination Hardening

The new window has a text node reading "This is a sample page". We can make an exclamation that the inner text of the node matches an expected text string. Our test should so resemble the following when complete:

          def test_new_browser_window(page):
"""Test that a new window may be opened.
:param page: A Playwright browser page
"""
window_text = "This is a sample page"
page.goto("https://www.demoqa.com/browser-windows")
page.wait_for_selector("#windowButton")
with page.context.expect_page() equally window:
page.click("#windowButton")
new_window = window.value
heading = new_window.wait_for_selector(
"#sampleHeading"
)
visible = heading.is_visible()
text = heading.inner_text()
assert visible and window_text in text

Nosotros can now run our exam using headless or headful commands.

  • pytest
  • pytest --headful

Project Directory

Your directory should resemble the following upon completion of this tutorial:

          tests
|__ test_windows.py
.gitignore
README.doctor
requirements.txt

Summary

Treatment windows in Playwright is effortless. Using the expect_page method, we can accept activity against a new window or simply wait until information technology has loaded. In one case present, we can employ Playwright'due south library of assertions to ensure our user journey has been successful.

Jonathan Thompson is a Senior Quality Engineer specializing in test automation. He currently resides in Raleigh, NC with his wife and a Goldendoodle named Winston. You lot tin can connect with him on LinkedIn, or follow him on either Twitter (@jacks_elsewhere) or Github (ThompsonJonM).

Source: https://python.plainenglish.io/handling-new-windows-with-python-and-playwright-c223a1e846d9

Posted by: carterseethe.blogspot.com

0 Response to "How To Open New Window In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel