Location>code7788 >text

A First Look at the "New Out of the Box" Series - Python+Playwright Automated Testing - 59 - Determining Whether an Element is Displayed or Not - Part 1

Popularity:94 ℃/2024-07-31 09:24:54

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 invocations

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.

3.2 Code design

Follow Macro's code idea in the Selenium tutorial:

3.3 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: Python+Playwright Automation Testing - 59 - Determining if an Element is Displayed
'''

# 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()
    error_message = ("//*[@id='TANGRAM__PSP_11__smsError' and text()='Please fill in the verification code']")
    if error_message.is_visible():
        print("Hiro! Elemental Presence")
    else:
        print("Hiro! The elements don't exist.")
    page.wait_for_timeout(1000)
    print("browser will be close");
    ()
    ()
    ()
with sync_playwright() as playwright:
    run(playwright)

3.4 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:

5. Methodology II

The second method, that is, first get this string with a String variable to save, and then compare the two strings. In fact, this method has been used before, only the macro brother did not point out, just like the previous article toast element, directly positioned in the variable stored, and then print out the text, is not it ah partners or children.

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: Python+Playwright Automation Testing - 59 - Determining if an Element is Displayed
'''

# 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()
    error_message = ("//*[@id='TANGRAM__PSP_11__smsError' and text()='Please fill in the verification code']").inner_text()
    if error_message=="Please fill in the verification code":
        print("Hiro! Elemental Presence")
    else:
        print("Hiro! The elements don't exist.")
    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!!!!