Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of python playwright automatic waiting and assertion

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "python playwright automatic waiting and assertion example analysis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "python playwright automatic waiting and assertion example analysis"!

Automatic waiting and element execution method

A series of methods for manipulating elements can be elicited as long as the test folder function page is called:

Import pytestfrom playwright.sync_api import Pagedef test_example (page: Page): page.goto ("https://www.baidu.com") page.hover ('/ * [@ id=" U1 "] / * [text () =" Settings "]') page.click ('/ * [@ id=" s-user-setting-menu "] / / * [text () =" search Settings "]') page.set_checked ('/ / * [ Text () = "all languages"]' False) assert page.title () = = "Baidu You know "page.close () if _ _ name__ ='_ _ main__': pytest.main (["-v "," test_example.py "])

Before playwright executes on the element, it performs a series of operability checks to ensure that these actions work as expected. It automatically waits for all relevant checks to pass before performing the requested operation. If the required check does not pass within the given range, timeout is thrown, and the operation fails and the TimeoutError is displayed.

The execution method is as follows:

Double-click page.dblclick () to get the element focus # get the element and focus on it. If there is no matching element, the method waits for the matching element to appear in the DOM. Page.focus ('# su') Mouse hover # is the mouse over the button This method is aimed at which floating box operation page.hover ('/ / * [@ id= "U1"] / * [text () = "Settings"]') Click the page.click ('/ / * [@ id= "s-user-setting-menu"] / / * [text () = "search Settings"]') setting check box to cancel or select page.click ('/ * [@ id= "s-user-setting-menu"] / / * [text) () = "search Settings"]') deselect the check box and take # to make sure the element is a check box or radio box. If the element is unchecked, this method immediately returns the page.uncheck ('/ * [text () = "simplified Chinese only"]') input parameter # this method is a focus element, and the event is triggered after the input enters the value. You can also pass an empty string to clear the input field. Page.fill ("# kw", "Qin Shi Mingyue") gets the element attribute value # returns the element attribute value page.get_attribute ('# kw' 'name') get internal text page.inner_text (' / / * [@ id= "s-hotsearch-wrapper"] / / * [@ data-index= "2"] / / * [@ class= "title-content-title"]') get internal HTMLpage.inner_html ('/ / * [@ id= "s-hotsearch-wrapper"] / / * [@ data-index= "2"]') get text content page.text_content ('/ / * [@ id= "s) -hotsearch-wrapper "] / * [@ data-index=" 2 "]') Screenshot # baidu.png is stored in the cases folder under the current folder page.screenshot (path='./cases/baidu.png') to fill in the text and trigger the keyboard event # to send a keydown for each character in the text, Keypress/input and keyup events. Page.type ("# kw", "hello") enter keyboard operation # to get button elements Enter the keyboard operation page.press ('# su', 'Enter') to set the select drop-down option # single selection page.select_option (\ "select#colors\",\ "blue\") # single selection page.select_option (\ "select#colors\", label=\ "blue\") matching the label # multiple selection page.select_option (\ "select#colors\", value= [\ "red\") \ "green\",\ "blue\"]) scheduling event # type can be passed: "click", "dragstart" page.dispatch_event ('# su', 'click') checkpoint (assertion) text content assertion # get text content Assert content = page.text_content ('[target= "_ blank"]: first-child') assert content = = "news" internal text assertion # get internal text, assert text = page.inner_text ('[target= "_ blank"]: first-child') assert text = = "news" attribute assertion # get attribute value Assert attribute = page.get_attribute ('# su', 'value') assert attribute = = "Baidu check box" assert page.hover (' / * [@ id= "U1"] / * [text () = "Settings"]') page.click ('/ * [@ id= "s-user-setting-menu"] / / * [text () = "search Settings"]') # check box status Assert checked = page.is_checked ('/ / * [text () = "all languages"]') assert checkedjs expression assert # JS expression, assert js_content = page.locator ('[data-index= "4"] > a > [class= "title-content-title"]'). Text_content () assert js_content = = "Changjin Lake Super Battle Wolf becomes the Box Office Champion of Chinese Film History" Internal HTML assertion # Internal HTML Assert html = page.inner_html ('[class= "hot-title"]') assert "Baidu Hot search" in html element visible assertion # element visibility, assert visible = page.is_visible ('# su') assert visible startup status assertion # enabled status (element exists clickable) Make the assertion enabled = page.is_enabled ('# su') assert enabled directly compare the assertion assert page.title () = = "Baidu, you will know"

Playwright also provides custom assertions, which I haven't practiced yet. If you are interested, you can continue to study them:

# assert the local stored value user_id = page.evaluate ("() = > window.localStorage.getItem ('user_id')") assert user_id# asserts the value of the input element value = page.locator (' # search'). Input_value () assert value = = 'query'# assertion calculation style font_size = page.locator (' div'). Evaluate ('el = > window.getComputedStyle (el) .fontSize') assert font_size = '16px'# assertion List length length = page.locator ('li.selected') .count () assert length = = 3 to this point I believe that you have a deeper understanding of "python playwright automatic waiting and assertion example analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report