From eec585bbc46673e0997712b2296f958790e3cb5a Mon Sep 17 00:00:00 2001 From: "Atthaboon Sanurt (P'Art)" Date: Fri, 25 Jun 2021 19:29:39 +0700 Subject: [PATCH] Hot fix open new playwright only if server has been stop (#120) * Add fix library context * Force set current context when call open browser Co-authored-by: atthaboon.s --- PuppeteerLibrary/__init__.py | 4 +++- PuppeteerLibrary/keywords/browsermanagement.py | 7 +++++-- PuppeteerLibrary/playwright/playwright_context.py | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/PuppeteerLibrary/__init__.py b/PuppeteerLibrary/__init__.py index cf9d47f..167f69c 100644 --- a/PuppeteerLibrary/__init__.py +++ b/PuppeteerLibrary/__init__.py @@ -158,7 +158,9 @@ async def set_current_library_context(self, context_name) -> iLibraryContext: @not_keyword def get_library_context_by_name(self, alias: str) -> iLibraryContext: - return self.library_contexts[alias] + if alias in self.library_contexts: + return self.library_contexts[alias] + return None @not_keyword def get_all_library_context(self) -> List[iLibraryContext]: diff --git a/PuppeteerLibrary/keywords/browsermanagement.py b/PuppeteerLibrary/keywords/browsermanagement.py index 1b1b90d..1bc9871 100644 --- a/PuppeteerLibrary/keywords/browsermanagement.py +++ b/PuppeteerLibrary/keywords/browsermanagement.py @@ -45,7 +45,10 @@ def open_browser(self, url, browser="chrome", alias=None, options={}): options = {} self.info(url) - library_context = self.ctx.create_library_context(alias, browser) + library_context = self.ctx.get_library_context_by_name(alias) + if library_context is None: + library_context = self.ctx.create_library_context(alias, browser) + self.loop.run_until_complete(self.ctx.set_current_library_context(alias)) self.loop.run_until_complete(library_context.start_server(options)) self.loop.run_until_complete(library_context.create_new_page(options)) self.loop.run_until_complete(self.get_async_keyword_group().go_to(url)) @@ -75,7 +78,7 @@ def close_all_browser(self): @keyword def close_puppeteer(self): - library_contexts_dict = self.ctx.get_all_library_context_dict() + library_contexts_dict = self.ctx.get_all_library_context_dict() for key in list(library_contexts_dict.keys()): self.loop.run_until_complete(library_contexts_dict[key].stop_server()) self.ctx.remove_library_context(key) diff --git a/PuppeteerLibrary/playwright/playwright_context.py b/PuppeteerLibrary/playwright/playwright_context.py index e886cbf..dff06fb 100644 --- a/PuppeteerLibrary/playwright/playwright_context.py +++ b/PuppeteerLibrary/playwright/playwright_context.py @@ -59,7 +59,10 @@ async def start_server(self, options: dict={}): elif key in ['slowMo']: merged_options[key] = str2int(merged_options[key]) - self.playwright = await async_playwright().start() + # Only start new Playwright server. If server didn't start before + if self.playwright is None: + self.playwright = await async_playwright().start() + if self.browser_type == "pwchrome": self.browser = await self.playwright.chromium.launch( headless=merged_options['headless'])