_dialog.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 typing import TYPE_CHECKING, Dict, Optional
  15. from playwright._impl._connection import ChannelOwner, from_nullable_channel
  16. from playwright._impl._errors import is_target_closed_error
  17. from playwright._impl._helper import locals_to_params
  18. if TYPE_CHECKING: # pragma: no cover
  19. from playwright._impl._page import Page
  20. class Dialog(ChannelOwner):
  21. def __init__(
  22. self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
  23. ) -> None:
  24. super().__init__(parent, type, guid, initializer)
  25. self._page: Optional["Page"] = from_nullable_channel(initializer.get("page"))
  26. def __repr__(self) -> str:
  27. return f"<Dialog type={self.type} message={self.message} default_value={self.default_value}>"
  28. @property
  29. def type(self) -> str:
  30. return self._initializer["type"]
  31. @property
  32. def message(self) -> str:
  33. return self._initializer["message"]
  34. @property
  35. def default_value(self) -> str:
  36. return self._initializer["defaultValue"]
  37. @property
  38. def page(self) -> Optional["Page"]:
  39. return self._page
  40. async def accept(self, promptText: str = None) -> None:
  41. await self._channel.send("accept", None, locals_to_params(locals()))
  42. async def dismiss(self) -> None:
  43. try:
  44. await self._channel.send(
  45. "dismiss",
  46. None,
  47. )
  48. except Exception as e:
  49. if is_target_closed_error(e):
  50. return
  51. raise