Skip to content

Commit

Permalink
Hot fix open new playwright only if server has been stop (#120)
Browse files Browse the repository at this point in the history
* Add fix library context

* Force set current context when call open browser

Co-authored-by: atthaboon.s <[email protected]>
  • Loading branch information
atthaboon and atthaboon authored Jun 25, 2021
1 parent 0f5c86d commit eec585b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion PuppeteerLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
7 changes: 5 additions & 2 deletions PuppeteerLibrary/keywords/browsermanagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion PuppeteerLibrary/playwright/playwright_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down

0 comments on commit eec585b

Please sign in to comment.