Rayrun
← Back to Discord Forum

how to use if and else statements?

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?"

11 replies

how to use if and else statements?

if (page.locator(...).isVisible()) ... else ...

But it is a flaky solution. Your tests and test environment should be deterministic.

What controls this pop-up? Is it critical to test your scenarios?

If it's not, disable it or the code that triggers it.

hi @p01___ , thank you for your reply . these pop-up's comes up due to different scenarios and rules which are triggered for each member , which we cannot disable. so i need to figure out a way to close them when they appear or ignore that step when they do not appear ..

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.

maulanaalihanafiah
maulanaalihanafiah

hi @p01___ I want to ask if the use of if-else conditions and looping in playwright how is it? because I don't understand the use of these two conditions in playwright and have tried several times and failed.

Thanks

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

maulanaalihanafiah
maulanaalihanafiah
@p01___: 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")

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

maulanaalihanafiah
maulanaalihanafiah
@p01___: ok thanks @p01___

ok thanks @p01___

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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].