|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 |
|
| 18 | +from typing import Dict, Optional |
| 19 | + |
18 | 20 | from selenium.webdriver.common.bidi.common import command_builder |
19 | 21 |
|
20 | 22 |
|
| 23 | +class UserPromptHandlerType: |
| 24 | + """Represents the behavior of the user prompt handler.""" |
| 25 | + |
| 26 | + ACCEPT = "accept" |
| 27 | + DISMISS = "dismiss" |
| 28 | + IGNORE = "ignore" |
| 29 | + |
| 30 | + VALID_TYPES = {ACCEPT, DISMISS, IGNORE} |
| 31 | + |
| 32 | + |
| 33 | +class UserPromptHandler: |
| 34 | + """Represents the configuration of the user prompt handler.""" |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + alert: Optional[str] = None, |
| 39 | + before_unload: Optional[str] = None, |
| 40 | + confirm: Optional[str] = None, |
| 41 | + default: Optional[str] = None, |
| 42 | + file: Optional[str] = None, |
| 43 | + prompt: Optional[str] = None, |
| 44 | + ): |
| 45 | + """Initialize UserPromptHandler. |
| 46 | +
|
| 47 | + Parameters: |
| 48 | + ----------- |
| 49 | + alert: Handler type for alert prompts |
| 50 | + before_unload: Handler type for beforeUnload prompts |
| 51 | + confirm: Handler type for confirm prompts |
| 52 | + default: Default handler type for all prompts |
| 53 | + file: Handler type for file picker prompts |
| 54 | + prompt: Handler type for prompt dialogs |
| 55 | +
|
| 56 | + Raises: |
| 57 | + ------ |
| 58 | + ValueError: If any handler type is not valid |
| 59 | + """ |
| 60 | + for field_name, value in [ |
| 61 | + ("alert", alert), |
| 62 | + ("before_unload", before_unload), |
| 63 | + ("confirm", confirm), |
| 64 | + ("default", default), |
| 65 | + ("file", file), |
| 66 | + ("prompt", prompt), |
| 67 | + ]: |
| 68 | + if value is not None and value not in UserPromptHandlerType.VALID_TYPES: |
| 69 | + raise ValueError( |
| 70 | + f"Invalid {field_name} handler type: {value}. Must be one of {UserPromptHandlerType.VALID_TYPES}" |
| 71 | + ) |
| 72 | + |
| 73 | + self.alert = alert |
| 74 | + self.before_unload = before_unload |
| 75 | + self.confirm = confirm |
| 76 | + self.default = default |
| 77 | + self.file = file |
| 78 | + self.prompt = prompt |
| 79 | + |
| 80 | + def to_dict(self) -> Dict[str, str]: |
| 81 | + """Convert the UserPromptHandler to a dictionary for BiDi protocol. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + ------- |
| 85 | + Dict[str, str]: Dictionary representation suitable for BiDi protocol |
| 86 | + """ |
| 87 | + result = {} |
| 88 | + if self.alert is not None: |
| 89 | + result["alert"] = self.alert |
| 90 | + if self.before_unload is not None: |
| 91 | + result["beforeUnload"] = self.before_unload |
| 92 | + if self.confirm is not None: |
| 93 | + result["confirm"] = self.confirm |
| 94 | + if self.default is not None: |
| 95 | + result["default"] = self.default |
| 96 | + if self.file is not None: |
| 97 | + result["file"] = self.file |
| 98 | + if self.prompt is not None: |
| 99 | + result["prompt"] = self.prompt |
| 100 | + return result |
| 101 | + |
| 102 | + |
21 | 103 | class Session: |
22 | 104 | def __init__(self, conn): |
23 | 105 | self.conn = conn |
|
0 commit comments