Location>code7788 >text

Getting Started with the "New Out of the Box" Series - Python+Playwright Automation Testing - 57 - Uploading Files - Extra

Popularity:226 ℃/2024-07-29 17:10:26

1. Introduction

The previous three articles basically introduce and explain the knowledge of file uploading almost, today is mainly to share macro in the actual operation of file uploading found in a problem: input control and non-input control upload API can be uploaded successfully. Without further ado, let's get straight to the point.

2.Project practice

Macro previously explained and shared in the Java + selenium series, will be divided into non-input controls to upload files, when in order to realize the file upload is a lot of trouble. Let's take a look at this test scenario.

2.1 Test scenarios

Open the Baidu home page, the left side of the search button has a camera icon, click on it to select the image search, we simulate the file automation upload operation through the process of uploading images locally, after the upload is successful, Baidu Sense will identify whether it is a Baidu search image, if so, it will display information about the image, if not, it will identify the failure, prompting re-upload. Preparation conditions, you search for a picture in Baidu image, save it to your desktop, for example, Hong found a picture about selenium, and then save it in the test folder on the desktop, the name of the Beijing Hong's pinyin initials:. First of all macro according to our conventional thinking: non-input control to upload files.

3. Non-input control to upload files

3.1 Code design

3.2 Reference code

# coding=utf-8🔥

# 1. First set the encoding, utf-8 can support Chinese and English, as above, generally placed in the first line

# 2. Comments: including record creation time, creator, project name.
'''
Created on 2024-04-26
@author: Beijing, capital of People's *-* businessman
Public No.: Beijing Hong (WeChat search: Beijing Hong, pay attention to Hong, in advance to unlock more testing dry goods!)
Project: 《latest out of the oven》Series Starter-Python+Playwrightautomated test-57- Upload File - extra article (in a newspaper)
'''

# 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 Select File Camera button and click the
    ("//*/span[@class='soutu-btn']").click()
    page.wait_for_timeout(1000)
    with page.expect_file_chooser() as fc_info:
         ("//*[@id='form']/div/div[2]/div[2]/input").click() # Click on the upload attachment button
    file_chooser = fc_info.value
    file_chooser.set_files('E:/Desktop/test/') # upload files
    # In order to see the uploaded image clearly, Macro increases the wait time
    page.wait_for_timeout(10000)
    print("browser will be close");
    ()
    ()
    ()
with sync_playwright() as playwright: run(playwright)
    run(playwright)

3.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:

Next, we will press the input control to upload the file, see if it can be successful, immediately is to witness the miracle time.

Controls for uploading files

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. Comments: including record creation time, creator, project name.
'''
Created on 2024-04-26
@author: Beijing, capital of People's *-* businessman
Public No.: Beijing Hong (WeChat search: Beijing Hong, follow Hong, unlock more testing dry goods in advance!)
Project: 《latest out of the oven》Series Starter-Python+Playwrightautomated test-57- Upload File - extra article (in a newspaper)
'''

# 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 select file button
    ("//*/span[@class='soutu-btn']").click()
    ("//*[@id='form']/div/div[2]/div[2]/input").set_input_files('E:/Desktop/test/')
    page.wait_for_timeout(3000)
    print("browser will be close")
    ()
    ()
    ()
with sync_playwright() as playwright: run(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:

5. Summary analysis

The reason why the input control API uploads successfully and the non-input control API uploads successfully is that the macro just happens to locate the input control, and this input control pops up the file selection dialog event when clicked, and all of the two methods work.

5.1 Reasons for successful upload of input control APIs

1. After a long time of summarizing and analyzing, and then finally a chance, suddenly found that the above two methods can be uploaded for the following reason: Hong just will be positioned in the input control on the so applicable to the input control to upload the file of the API can be successfully uploaded pictures. As shown in the figure below:

5.2 Reasons for successful upload of input control APIs

From the reasons above, we know that Macro locates on the input control how the non-input control method also applies, and uploads the picture successfully. Because this input is the previous "Select File" button, after clicking it, the non-input control listens to the window popup event, and then uploaded successfully. If you don't believe me, Hong will add this input control with the text "Upload Picture", and we can see that it just covers the previous "Select File" text. We can see just before the "Select File" text override. As you can see in the picture below:

6. Verification

There may be partners or children's shoes do not believe or still do not understand, Hong here again to verify. How to verify?

Step 1: First Macro locates the input control and then clicks on it to see if it will pop up the file selection dialog box event. This is very simple.

6.1 Code Design

6.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-04-26
@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 - Python+Playwright Automation Testing - 57 - Uploading Files - Extra
'''

# 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 Select File Camera button and click
    ("//*/span[@class='soutu-btn']").click()
    page.wait_for_timeout(1000)
    ("//*[@id='form']/div/div[2]/div[2]/input").click()  # Click the Upload Attachment button
    # In order to see the uploaded pictures clearly, Hiro increased the waiting time
    page.wait_for_timeout(10000)
    print("browser will be close");
    ()
    ()
    ()
with sync_playwright() as playwright:
    run(playwright)

6.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:

Step 2: If we locate "Select File" and click on it, it won't pop up the file selection dialog event, filechooser can't listen to the pop up file selection dialog event, and it will report an error.

6.4 Code Design

6.5 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-04-26
@author: Beijing, capital of People's *-* businessman
Public No.: Beijing Hong (WeChat search: Beijing Hong, pay attention to Hong, unlock more testing dry goods in advance!)
Project: The Latest Out of the Box Series - Python+Playwright Automation Testing - 57 - Uploading Files - Extra
'''

# 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 Select File Camera button and click
    ("//*/span[@class='soutu-btn']").click()
    page.wait_for_timeout(1000)
    # Positioning Select file
    context = page.get_by_text("Select File")
    # Determine if it is located and print the located text.
    print(context.inner_text())
    ()
    # In order to see the uploaded pictures clearly, Hiro increased the waiting time
    page.wait_for_timeout(1000)
    print("browser will be close");
    ()
    ()
    ()
with sync_playwright() as playwright:
    run(playwright)

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

From the above we can see that try to click many times do not work, and finally reported an error, if you locate this "select file" application non-input API, and finally filechooser can not listen to the pop-up file selection dialog box event, it will report an error. Macro here will not do a demonstration, interested parties can practice on their own.

7. Summary

This article is mainly to introduce the macro in the file upload practice process encountered a small hiccup, as well as why there will be the reason for this small hiccup. It's actually quite simple to figure out. Well, it's not early today, Macro will explain and share here, thank you for your patience to read, I hope to help you. The next article introduces the file download.