__init__.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. """
  15. Python package `playwright` is a Python library to automate Chromium,
  16. Firefox and WebKit with a single API. Playwright is built to enable cross-browser
  17. web automation that is ever-green, capable, reliable and fast.
  18. """
  19. from typing import Any, Optional, Union, overload
  20. import playwright._impl._api_structures
  21. import playwright._impl._errors
  22. import playwright.async_api._generated
  23. from playwright._impl._assertions import (
  24. APIResponseAssertions as APIResponseAssertionsImpl,
  25. )
  26. from playwright._impl._assertions import LocatorAssertions as LocatorAssertionsImpl
  27. from playwright._impl._assertions import PageAssertions as PageAssertionsImpl
  28. from playwright.async_api._context_manager import PlaywrightContextManager
  29. from playwright.async_api._generated import (
  30. APIRequest,
  31. APIRequestContext,
  32. APIResponse,
  33. APIResponseAssertions,
  34. Browser,
  35. BrowserContext,
  36. BrowserType,
  37. CDPSession,
  38. ConsoleMessage,
  39. Dialog,
  40. Download,
  41. ElementHandle,
  42. FileChooser,
  43. Frame,
  44. FrameLocator,
  45. JSHandle,
  46. Keyboard,
  47. Locator,
  48. LocatorAssertions,
  49. Mouse,
  50. Page,
  51. PageAssertions,
  52. Playwright,
  53. Request,
  54. Response,
  55. Route,
  56. Selectors,
  57. Touchscreen,
  58. Video,
  59. WebError,
  60. WebSocket,
  61. WebSocketRoute,
  62. Worker,
  63. )
  64. ChromiumBrowserContext = BrowserContext
  65. Cookie = playwright._impl._api_structures.Cookie
  66. FilePayload = playwright._impl._api_structures.FilePayload
  67. FloatRect = playwright._impl._api_structures.FloatRect
  68. Geolocation = playwright._impl._api_structures.Geolocation
  69. HttpCredentials = playwright._impl._api_structures.HttpCredentials
  70. PdfMargins = playwright._impl._api_structures.PdfMargins
  71. Position = playwright._impl._api_structures.Position
  72. ProxySettings = playwright._impl._api_structures.ProxySettings
  73. ResourceTiming = playwright._impl._api_structures.ResourceTiming
  74. SourceLocation = playwright._impl._api_structures.SourceLocation
  75. StorageState = playwright._impl._api_structures.StorageState
  76. StorageStateCookie = playwright._impl._api_structures.StorageStateCookie
  77. ViewportSize = playwright._impl._api_structures.ViewportSize
  78. Error = playwright._impl._errors.Error
  79. TimeoutError = playwright._impl._errors.TimeoutError
  80. def async_playwright() -> PlaywrightContextManager:
  81. return PlaywrightContextManager()
  82. class Expect:
  83. _unset: Any = object()
  84. def __init__(self) -> None:
  85. self._timeout: Optional[float] = None
  86. def set_options(self, timeout: Optional[float] = _unset) -> None:
  87. """
  88. This method sets global `expect()` options.
  89. Args:
  90. timeout (float): Timeout value in milliseconds. Default to 5000 milliseconds.
  91. Returns:
  92. None
  93. """
  94. if timeout is not self._unset:
  95. self._timeout = timeout
  96. @overload
  97. def __call__(
  98. self, actual: Page, message: Optional[str] = None
  99. ) -> PageAssertions: ...
  100. @overload
  101. def __call__(
  102. self, actual: Locator, message: Optional[str] = None
  103. ) -> LocatorAssertions: ...
  104. @overload
  105. def __call__(
  106. self, actual: APIResponse, message: Optional[str] = None
  107. ) -> APIResponseAssertions: ...
  108. def __call__(
  109. self, actual: Union[Page, Locator, APIResponse], message: Optional[str] = None
  110. ) -> Union[PageAssertions, LocatorAssertions, APIResponseAssertions]:
  111. if isinstance(actual, Page):
  112. return PageAssertions(
  113. PageAssertionsImpl(actual._impl_obj, self._timeout, message=message)
  114. )
  115. elif isinstance(actual, Locator):
  116. return LocatorAssertions(
  117. LocatorAssertionsImpl(actual._impl_obj, self._timeout, message=message)
  118. )
  119. elif isinstance(actual, APIResponse):
  120. return APIResponseAssertions(
  121. APIResponseAssertionsImpl(
  122. actual._impl_obj, self._timeout, message=message
  123. )
  124. )
  125. raise ValueError(f"Unsupported type: {type(actual)}")
  126. expect = Expect()
  127. __all__ = [
  128. "expect",
  129. "async_playwright",
  130. "APIRequest",
  131. "APIRequestContext",
  132. "APIResponse",
  133. "Browser",
  134. "BrowserContext",
  135. "BrowserType",
  136. "CDPSession",
  137. "ChromiumBrowserContext",
  138. "ConsoleMessage",
  139. "Cookie",
  140. "Dialog",
  141. "Download",
  142. "ElementHandle",
  143. "Error",
  144. "FileChooser",
  145. "FilePayload",
  146. "FloatRect",
  147. "Frame",
  148. "FrameLocator",
  149. "Geolocation",
  150. "HttpCredentials",
  151. "JSHandle",
  152. "Keyboard",
  153. "Locator",
  154. "Mouse",
  155. "Page",
  156. "PdfMargins",
  157. "Position",
  158. "Playwright",
  159. "ProxySettings",
  160. "Request",
  161. "ResourceTiming",
  162. "Response",
  163. "Route",
  164. "Selectors",
  165. "SourceLocation",
  166. "StorageState",
  167. "StorageStateCookie",
  168. "TimeoutError",
  169. "Touchscreen",
  170. "Video",
  171. "ViewportSize",
  172. "WebError",
  173. "WebSocket",
  174. "WebSocketRoute",
  175. "Worker",
  176. ]