1. Introduction
The life cycle of some page elements is as fleeting as a shooting star. We don't know if this element has ever appeared in the page, in order to capture this beautiful moment and make it eternal. In order to capture this beautiful moment and make it eternal, we will determine whether the element is shown to have appeared or not.
You can determine the state of an element before manipulating it. Judging the state of an element operation can also be used for assertions.
2. Commonly used elemental judgment methods
2.1 Judgment methods for page object invocation
The judgment method called by the page object, passing a selector location parameter.
- page.is_checked(selector: str) # checkbox or radio Whether or not to check
- page.is_disabled(selector: str) # whether the element is clickable or editable
- page.is_editable(selector: str) # whether the element is editable or not
- page.is_enabled(selector: str) # whether or not it's operable
- page.is_hidden(selector: str) # whether or not it is hidden
- page.is_visible(selector: str) # whether or not it is visible
2.2 Judgment methods for locator object invocation
Judgment methods for locator object invocations
- locator.is_checked()
- locator.is_disabled()
- locator.is_editable()
- locator.is_enabled()
- locator.is_hidden()
- locator.is_visible()
2.3 Methods for determining element handles
Element Handle Determination Methods
- element_handle.is_checked()
- element_handle.is_disabled()
- element_handle.is_editable()
- element_handle.is_enabled()
- element_handle.is_hidden()
- element_handle.is_visible()
Element handle (element_handle) is returned by page.query_selector () method call ElementHandle , this is generally not commonly used. It is not recommended.
3.Project practice
In automated testing, there is a scenario where we often need to make a judgment. For example, some operations, we do, will trigger some reminders, some are correct reminders, some are red font display error message. How do we capture these fields in our automation, and if we do test automation to determine it. Here we need to use the methods we learned today. Macro here to use the home page login example, to determine whether the field "Please fill in the verification code" appears.
3.1 Test cases (ideas)
1. Visit the homepage of Duoyuan
2. Locate the login button on the home page and click on the
3. The login box pops up to locate the SMS login button, and then click the
4. Locate the cell phone number input box, and then enter the cell phone number
5. Locate the agreement and click
6. Locate the Login button in the Login box and click the
7. Locate the "Please fill in the verification code", and then judgment.
The method explained in the previous article is still the traditional way of judging, so today we look at the new way of judging.
Judgment Methods for Object Calls
4.1 Code design
4.2 Reference code
# coding=utf-8🔥 # 1. First set the encoding, utf-8 can support Chinese and English, such as the above, generally placed in the first line # 2. Notes: including record creation time, creator, project name. ''' Created on 2024-05-09 @author: Beijing, capital of People's *-* businessman Public No.: Beijing Hongge (WeChat search: Beijing Hongge, follow Hongge, unlock more testing dry goods in advance!) Project: "The latest out of the box" series of first peek - Python + Playwright automated testing - 60 - determine whether the element is displayed - the next chapter ''' # 3. Import module from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: browser = (headless=False) context = browser.new_context() page = context.new_page() ("/") # Locate the Login button on the home page and click Login ("//*[@id='u1']/a[1]").click() page.wait_for_timeout(300) # The login box pops up to locate the SMS login button, and then click the ("#TANGRAM__PSP_11__changeSmsCodeItem").click() # Locate the cell phone number input box and enter the cell phone number ("#TANGRAM__PSP_11__smsPhone").type("13734294156") # Locate the consent agreement click ("#TANGRAM__PSP_11__smsIsAgree").click() # Locate the Login button in the Login box and click ("#TANGRAM__PSP_11__smsSubmit").click() # Determines whether the element appears to be displayed (display prints True, no display prints False) print(page.is_visible("//*[@id='TANGRAM__PSP_11__smsError' and text()='Please fill in the verification code']")) page.wait_for_timeout(1000) print("browser will be close"); () () () with sync_playwright() as playwright: run(playwright)
4.3 Running the code
1. Run the code, right click Run 'Test', you can see the console output, as shown below:
2. Run the code after the browser action on the computer side. As shown in the figure below:
post-location judgement element
5.1 Code Design
5.2 Reference code
# coding=utf-8🔥 # 1. First set the encoding, utf-8 can support Chinese and English, such as the above, generally placed in the first line # 2. Notes: including record creation time, creator, project name. ''' Created on 2024-05-09 @author: Beijing, capital of People's *-* businessman Public No.: Beijing Hongge (WeChat search: Beijing Hongge, follow Hongge, unlock more testing dry goods in advance!) Project: "The latest out of the box" series of first peek - Python + Playwright automated testing - 60 - determine whether the element is displayed - the next chapter ''' # 3. Import module from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: browser = (headless=False) context = browser.new_context() page = context.new_page() ("/") # Locate the Login button on the home page and click Login ("//*[@id='u1']/a[1]").click() page.wait_for_timeout(300) # The login box pops up to locate the SMS login button, and then click the ("#TANGRAM__PSP_11__changeSmsCodeItem").click() # Locate the cell phone number input box and enter the cell phone number ("#TANGRAM__PSP_11__smsPhone").type("13734294156") # Locate the consent agreement click ("#TANGRAM__PSP_11__smsIsAgree").click() # Locate the Login button in the Login box and click ("#TANGRAM__PSP_11__smsSubmit").click() # Determines whether the element appears to be displayed (display prints True, no display prints False) print(("//*[@id='TANGRAM__PSP_11__smsError' and text()='Please fill in the verification code']").is_visible()) page.wait_for_timeout(1000) print("browser will be close"); () () () with sync_playwright() as playwright: run(playwright)
5.3 Running the code
1. Run the code, right click Run 'Test', you can see the console output, as shown below:
2. Run the code after the browser action on the computer side. As shown in the figure below:
6. Summary
Well, it's getting late today, about determining whether an element is displayed or not is introduced here, thank you for your patience!!!!