填写对话框表格。 Python剧作家

问题描述 投票:0回答:1

我有Python剧作家的剧本。我试图在对话框中填写提示字段,但脚本只需单击按钮,打开提示对话框并立即接受它,而无需使用

prompt_text
变量填写输入字段。我尝试处理这个问题https://practice-automation.com/popups/

方法提示


def prompt(self, prompt_text) -> None:
    self.page.once("dialog", lambda dialog: dialog.accept(prompt_text))
    self.popup_locator.prompt_trigger_btn.click()
    time.sleep(1)
    if prompt_text == "":
        expect(self.popup_locator.prompt_result_p).to_be_visible()
        expect(self.popup_locator.prompt_result_p).to_have_text("Fine, be that way...")
    else:
        expect(self.popup_locator.prompt_result_p).to_be_visible()
        expect(self.popup_locator.prompt_result_p).to_have_text(f"Nice to meet you, {prompt_text}!")
    self.popup_locator.prompt_trigger_btn.click()
    self.page.once("dialog", lambda dialog: dialog.dismiss())
    expect(self.popup_locator.prompt_result_p).to_have_text("Fine, be that way...")

包含测试的文件:

def test_close(set_up):
    popup_obj = Popup_page(set_up)
    popup_obj.goto()
    popup_obj.prompt("Dupa")
    popup_obj.prompt("")

我也有一个带有定位器的文件,但按钮打开对话框有效。它不会填写提示中的输入字段。

python pytest playwright playwright-python
1个回答
0
投票

您没有提供完整的可运行代码,因此很难知道哪里不好。

我从您的代码中编写了下面的示例测试代码,您可以尝试反映到您的代码中吗? 该代码有单击按钮之前和之后的屏幕截图,因此您可以看到它是如何工作的。

import datetime

from playwright.sync_api import Page, expect


def get_screenshot_file_path(type: str):
    current = datetime.datetime.now()
    return "screenshots/" + current.strftime("%Y%m%d-%H%M%S-%f") + "-" + type + ".png"


def prompt(page: Page, prompt_text) -> None:
    page.once("dialog", lambda dialog: dialog.accept(prompt_text))
    # popup_locator.prompt_trigger_btn.click()
    prompt_trigger_locator = page.get_by_role("button", name="Prompt Popup")
    page.screenshot(path=get_screenshot_file_path("before-first-prompt-click"))
    prompt_trigger_locator.click()
    page.screenshot(path=get_screenshot_file_path("after-first-prompt-click"))

    # time.sleep(1)
    page.wait_for_timeout(1_000)

    prompt_result_locator = page.locator("#promptResult")
    expect(prompt_result_locator).to_be_visible()

    if prompt_text == "":
        expect(prompt_result_locator).to_have_text("Fine, be that way...")
    else:
        expect(prompt_result_locator).to_have_text(f"Nice to meet you, {prompt_text}!")

    page.once("dialog", lambda dialog: dialog.dismiss())
    # popup_locator.prompt_trigger_btn.click()
    page.screenshot(path=get_screenshot_file_path("before-second-prompt-click"))
    prompt_trigger_locator.click()
    page.screenshot(path=get_screenshot_file_path("after-second-prompt-click"))

    expect(page.locator("#promptResult")).to_have_text("Fine, be that way...")


def test_close(page: Page):
    page.goto("https://practice-automation.com/popups/")
    prompt(page, "Dupa")
    prompt(page, "")
© www.soinside.com 2019 - 2024. All rights reserved.