← Back to Discord Forum

Switch browser tabs during test run


I run a test with one browser, one context, two tabs. The test runs in both tabs fine: first in tab 1, then tab 2, then finishes in tab 1 again. However, I see only the first created tab during test run, which is tab 1.

Is there any way in playwright to switch to tab where the test is actually running?

This thread is trying to answer question "Is there any way in Playwright to switch to the tab where the test is actually running?"

12 replies

This is what I do with POM. Hopefully this is what you're asking for:

async yourMethod() { ... const [tab2] = await Promise.all([ this.context.waitForEvent('page'); // your action to go to 2nd tab ]); return tab2; }

You don't have to switch tab just declare your page 1, 2 separately and call them, playwright will auto detect and run your code exactly

@bububu8615: i probably just misunderstood what he's asking

i probably just misunderstood what he's asking

I drive Playwright using Python / pytest, not JS / TS, hence the Promise.all is not available for me.

Also, I do know that the two tabs are fully functional during testing, tests are passing, fine. However, I do not see what happens in one of the tab, but I would like to, hence my question.


The trace viewer will see what's happening in all tabs. You shouldn't need to see it yourself. If you need to you can switch to the tab yourself.

Considering the execution speed of the Playwright test, by the time I would switch tab, the test is already over.


so what's the problem?


you're wanting to be able to watch the test?


Does the trace viewer capture all tabs?

Its likely a timing issue of when the screenshots occur for: Action, Before, and After are captured. Do you have an expect of some type defined for the second tab?


Try page2.bring_to_front() if you just want to see what's happening in the headed or trace viewer.


Oh I knew there was something like that, I had just forgotten what it was! https://playwright.dev/docs/api/class-page#page-bring-to-front

Related Discord Threads

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 luc@ray.run.