0

I am using playwright and Java. On test level I have

List<BrowserContext> browserContexts = browser.contexts();
browserContexts.get(1).close();

In debug mode I have two browsers context with a difference at the url name. Now I want to prepare a method which returns int for particular url which I need to used in this test. So this method I can put in

browserContexts.get(nameOfThisMethod).close();

Url name which I want to close has "/home" in name. I don't know how to write this method which will include steps to identify url which contains /home. Can someone help?

goney225
  • 91
  • 1
  • 1
  • 10

1 Answers1

1

You need to go into browser contexts, extract list of pages (joining into one list) and find a page, which containing url.

For example, this code do something like this

List<Page> result = new ArrayList<>();
browser.contexts().stream()
                    .map(BrowserContext::pages)
                    .forEach(result::addAll);
result.stream()
      .filter(p -> p.url().contains("/home"))
      .findFirst()
      .ifPresent(Page::close);

Example how to close page and context

public Optional<Page> containsUrl(List<Page> pages, String urlPart) {
        return pages.stream().filter(p -> p.url().contains(urlPart)).findFirst();
}

...

browser.contexts().stream().filter(browserContext -> containsUrl(browserContext.pages(), "/home").isPresent())
                        .findAny().ifPresent(BrowserContext::close);

or less nice solution

//in test
withoutContext(browser, "/home");

// somewhere in TestRunner or BaseTest
public void withoutContext(Browser browser, String urlPart) {
 for (BrowserContext context: browser.contexts()) {
  for(Page contextPage: context.pages()) {
    if (contextPage.url().contains(urlPart)) {
        context.close();
        break;
    }
  }
 }
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Yaroslavm
  • 1,762
  • 2
  • 7
  • 15
  • I implemented this. Page with /home is now closed but the same issue when I tried to close context 'Error: Target page, context or browser has been closed – goney225 Jul 24 '23 at 11:47
  • @goney225 Added second block of code to close context (closing context should also close all related pages to context). Please, check – Yaroslavm Jul 24 '23 at 12:17
  • Unfortunately still first browser is closed too. I want to close only this browser context which has "/home" in url. I see that this url is closed but first too and error target page, context or browser has been closed is displayed – goney225 Jul 25 '23 at 07:02
  • @goney225 are you sure that first url doesn't contain /home? Cause I tested this code with example.com and dou.ua by condition .ua and it closed second context and hadn't closed first. – Yaroslavm Jul 25 '23 at 07:43
  • Can you provide me your resource? Or you can debug expression and print urls in console – Yaroslavm Jul 25 '23 at 07:45
  • @goney225 added third example of code (not so nice) that also works for me. Also, you can easily debug it if you need – Yaroslavm Jul 25 '23 at 07:57
  • After debugging this part of code with stream is working because browser with /home in url is closed. I was wondering now why remaining part of code is not executed. I have one line code with method on click on button. It looks like this exist browser lost focus and returned "target page, context or browser has been closed" – goney225 Jul 25 '23 at 09:11
  • @goney225 be sure that you execute button.click() in context that is alive. for example, try to get button from `Page page = browser.contexts()[0].pages()[0]`. `page.waitForSelector` ... – Yaroslavm Jul 25 '23 at 09:29
  • I used this and error about target page, context or browser has been closed `Page page = browser.contexts().get(0).pages().get(0);` – goney225 Jul 25 '23 at 10:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254675/discussion-between-goney225-and-yaroslavm). – goney225 Jul 26 '23 at 10:34