_browser.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. # Copyright (c) Microsoft Corporation.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from pathlib import Path
  15. from types import SimpleNamespace
  16. from typing import (
  17. TYPE_CHECKING,
  18. Dict,
  19. List,
  20. Optional,
  21. Pattern,
  22. Sequence,
  23. Set,
  24. Union,
  25. cast,
  26. )
  27. from playwright._impl._api_structures import (
  28. ClientCertificate,
  29. Geolocation,
  30. HttpCredentials,
  31. ProxySettings,
  32. StorageState,
  33. ViewportSize,
  34. )
  35. from playwright._impl._artifact import Artifact
  36. from playwright._impl._browser_context import BrowserContext
  37. from playwright._impl._cdp_session import CDPSession
  38. from playwright._impl._connection import ChannelOwner, from_channel
  39. from playwright._impl._errors import is_target_closed_error
  40. from playwright._impl._helper import (
  41. ColorScheme,
  42. Contrast,
  43. ForcedColors,
  44. HarContentPolicy,
  45. HarMode,
  46. ReducedMotion,
  47. ServiceWorkersPolicy,
  48. locals_to_params,
  49. make_dirs_for_file,
  50. )
  51. from playwright._impl._page import Page
  52. if TYPE_CHECKING: # pragma: no cover
  53. from playwright._impl._browser_type import BrowserType
  54. class Browser(ChannelOwner):
  55. Events = SimpleNamespace(
  56. Disconnected="disconnected",
  57. )
  58. def __init__(
  59. self, parent: "BrowserType", type: str, guid: str, initializer: Dict
  60. ) -> None:
  61. super().__init__(parent, type, guid, initializer)
  62. self._browser_type: Optional["BrowserType"] = None
  63. self._is_connected = True
  64. self._should_close_connection_on_close = False
  65. self._cr_tracing_path: Optional[str] = None
  66. self._contexts: Set[BrowserContext] = set()
  67. self._traces_dir: Optional[str] = None
  68. self._channel.on(
  69. "context",
  70. lambda params: self._did_create_context(
  71. cast(BrowserContext, from_channel(params["context"]))
  72. ),
  73. )
  74. self._channel.on("close", lambda _: self._on_close())
  75. self._close_reason: Optional[str] = None
  76. def __repr__(self) -> str:
  77. return f"<Browser type={self._browser_type} version={self.version}>"
  78. def _connect_to_browser_type(
  79. self,
  80. browser_type: "BrowserType",
  81. traces_dir: Optional[str] = None,
  82. ) -> None:
  83. # Note: when using connect(), `browserType` is different from `this.parent`.
  84. # This is why browser type is not wired up in the constructor, and instead this separate method is called later on.
  85. self._browser_type = browser_type
  86. self._traces_dir = traces_dir
  87. for context in self._contexts:
  88. self._setup_browser_context(context)
  89. def _did_create_context(self, context: BrowserContext) -> None:
  90. context._browser = self
  91. self._contexts.add(context)
  92. # Note: when connecting to a browser, initial contexts arrive before `_browserType` is set,
  93. # and will be configured later in `ConnectToBrowserType`.
  94. if self._browser_type:
  95. self._setup_browser_context(context)
  96. def _setup_browser_context(self, context: BrowserContext) -> None:
  97. context._tracing._traces_dir = self._traces_dir
  98. assert self._browser_type is not None
  99. self._browser_type._playwright.selectors._contexts_for_selectors.add(context)
  100. def _on_close(self) -> None:
  101. self._is_connected = False
  102. self.emit(Browser.Events.Disconnected, self)
  103. @property
  104. def contexts(self) -> List[BrowserContext]:
  105. return list(self._contexts)
  106. @property
  107. def browser_type(self) -> "BrowserType":
  108. assert self._browser_type is not None
  109. return self._browser_type
  110. def is_connected(self) -> bool:
  111. return self._is_connected
  112. async def new_context(
  113. self,
  114. viewport: ViewportSize = None,
  115. screen: ViewportSize = None,
  116. noViewport: bool = None,
  117. ignoreHTTPSErrors: bool = None,
  118. javaScriptEnabled: bool = None,
  119. bypassCSP: bool = None,
  120. userAgent: str = None,
  121. locale: str = None,
  122. timezoneId: str = None,
  123. geolocation: Geolocation = None,
  124. permissions: Sequence[str] = None,
  125. extraHTTPHeaders: Dict[str, str] = None,
  126. offline: bool = None,
  127. httpCredentials: HttpCredentials = None,
  128. deviceScaleFactor: float = None,
  129. isMobile: bool = None,
  130. hasTouch: bool = None,
  131. colorScheme: ColorScheme = None,
  132. reducedMotion: ReducedMotion = None,
  133. forcedColors: ForcedColors = None,
  134. contrast: Contrast = None,
  135. acceptDownloads: bool = None,
  136. defaultBrowserType: str = None,
  137. proxy: ProxySettings = None,
  138. recordHarPath: Union[Path, str] = None,
  139. recordHarOmitContent: bool = None,
  140. recordVideoDir: Union[Path, str] = None,
  141. recordVideoSize: ViewportSize = None,
  142. storageState: Union[StorageState, str, Path] = None,
  143. baseURL: str = None,
  144. strictSelectors: bool = None,
  145. serviceWorkers: ServiceWorkersPolicy = None,
  146. recordHarUrlFilter: Union[Pattern[str], str] = None,
  147. recordHarMode: HarMode = None,
  148. recordHarContent: HarContentPolicy = None,
  149. clientCertificates: List[ClientCertificate] = None,
  150. ) -> BrowserContext:
  151. params = locals_to_params(locals())
  152. assert self._browser_type is not None
  153. await self._browser_type._prepare_browser_context_params(params)
  154. channel = await self._channel.send("newContext", None, params)
  155. context = cast(BrowserContext, from_channel(channel))
  156. await context._initialize_har_from_options(
  157. record_har_content=recordHarContent,
  158. record_har_mode=recordHarMode,
  159. record_har_omit_content=recordHarOmitContent,
  160. record_har_path=recordHarPath,
  161. record_har_url_filter=recordHarUrlFilter,
  162. )
  163. return context
  164. async def new_page(
  165. self,
  166. viewport: ViewportSize = None,
  167. screen: ViewportSize = None,
  168. noViewport: bool = None,
  169. ignoreHTTPSErrors: bool = None,
  170. javaScriptEnabled: bool = None,
  171. bypassCSP: bool = None,
  172. userAgent: str = None,
  173. locale: str = None,
  174. timezoneId: str = None,
  175. geolocation: Geolocation = None,
  176. permissions: Sequence[str] = None,
  177. extraHTTPHeaders: Dict[str, str] = None,
  178. offline: bool = None,
  179. httpCredentials: HttpCredentials = None,
  180. deviceScaleFactor: float = None,
  181. isMobile: bool = None,
  182. hasTouch: bool = None,
  183. colorScheme: ColorScheme = None,
  184. forcedColors: ForcedColors = None,
  185. contrast: Contrast = None,
  186. reducedMotion: ReducedMotion = None,
  187. acceptDownloads: bool = None,
  188. defaultBrowserType: str = None,
  189. proxy: ProxySettings = None,
  190. recordHarPath: Union[Path, str] = None,
  191. recordHarOmitContent: bool = None,
  192. recordVideoDir: Union[Path, str] = None,
  193. recordVideoSize: ViewportSize = None,
  194. storageState: Union[StorageState, str, Path] = None,
  195. baseURL: str = None,
  196. strictSelectors: bool = None,
  197. serviceWorkers: ServiceWorkersPolicy = None,
  198. recordHarUrlFilter: Union[Pattern[str], str] = None,
  199. recordHarMode: HarMode = None,
  200. recordHarContent: HarContentPolicy = None,
  201. clientCertificates: List[ClientCertificate] = None,
  202. ) -> Page:
  203. params = locals_to_params(locals())
  204. async def inner() -> Page:
  205. context = await self.new_context(**params)
  206. page = await context.new_page()
  207. page._owned_context = context
  208. context._owner_page = page
  209. return page
  210. return await self._connection.wrap_api_call(inner, title="Create page")
  211. async def close(self, reason: str = None) -> None:
  212. self._close_reason = reason
  213. try:
  214. if self._should_close_connection_on_close:
  215. await self._connection.stop_async()
  216. else:
  217. await self._channel.send("close", None, {"reason": reason})
  218. except Exception as e:
  219. if not is_target_closed_error(e):
  220. raise e
  221. @property
  222. def version(self) -> str:
  223. return self._initializer["version"]
  224. async def new_browser_cdp_session(self) -> CDPSession:
  225. return from_channel(await self._channel.send("newBrowserCDPSession", None))
  226. async def start_tracing(
  227. self,
  228. page: Page = None,
  229. path: Union[str, Path] = None,
  230. screenshots: bool = None,
  231. categories: Sequence[str] = None,
  232. ) -> None:
  233. params = locals_to_params(locals())
  234. if page:
  235. params["page"] = page._channel
  236. if path:
  237. self._cr_tracing_path = str(path)
  238. params["path"] = str(path)
  239. await self._channel.send("startTracing", None, params)
  240. async def stop_tracing(self) -> bytes:
  241. artifact = cast(
  242. Artifact, from_channel(await self._channel.send("stopTracing", None))
  243. )
  244. buffer = await artifact.read_info_buffer()
  245. await artifact.delete()
  246. if self._cr_tracing_path:
  247. make_dirs_for_file(self._cr_tracing_path)
  248. with open(self._cr_tracing_path, "wb") as f:
  249. f.write(buffer)
  250. self._cr_tracing_path = None
  251. return buffer