Filling out dialog form. Python playwright

34 views Asked by At

I have script in python playwright. I'm trying to fill in a prompt field in a dialog but the script just clicks in the button, opens prompt dialog and immediately accepts it without filling out the input field with the prompt_text variable. I tried to handle this https://practice-automation.com/popups/.

Method prompt


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...")

File with tests:

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

I have a file with locators as well, but the button opening dialog works. It doesn't fill out input field in the prompt.

1

There are 1 answers

0
congacon On

You did not provide full runnable code, so it is hard to know where is bad.

I write a sample test code below from your code, could you try to reflect into your code? The code has screenshot before and after click button, so you can see how it works.

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, "")