we have a scenario where -
When we try to Automation our application, we have multiple popup’s coming all over through the application, so we are not sure if the pop up comes up or not, it depends on the different scenarios to the pop up to show or not.
so in this scenario how to Handle these situations where if the popup shows click on ‘close’ or if the pop up doesn’t show continue with the test.
for ex – if popup is visible click close else – click on menu
Can you please help with the this ‘if & else’ scenario?
This thread is trying to answer question "How to handle unpredictable popups in an application during automation testing using if and else statements?"
Well, these popups don't appear by magic, they are triggered by some javascript, and are either window.alert() and co or just plain HTML elements, so you can prevent these from happening.
But ideally, these popups and the logic behind them should behind a feature flag disabled for your test users.
Hi, for the popups the OP was facing, using await page.addInitScript(...)
to inject some JS to prevent or disable the creation of the popups in the page, on the browser side, would work.
I'm not sure what kind of if-else conditions you have in mind. If you're still stuck, you could start a thread and post an example of your tests/script, even if it is in pseudo code.
Best
Sorry, one example is like this:
from playwright.sync_api import Page, expect
def test_example(page: Page): page.goto("https://www.saucedemo.com/") page.locator("[data-test="username"]").fill("standard_user") page.locator("[data-test="password"]").fill("secret_sauce") page.locator("[data-test="login-button"]").click()
if (expect(page.locator("//span[@class='title']")).to_contain_text("Products")):
print("Login successful!")
else:
print("Login Failed")
The expect will throw on failure, so you won't see the "Login Failed"
In that case, you might want to get the return value of the expect and try/catch it or use a soft assertion https://playwright.dev/docs/test-assertions#soft-assertions
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].