|
| 1 | +# |
| 2 | +# Copyright 2024 Splunk Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +from ..base_component import Selector |
| 17 | +from .base_control import BaseControl |
| 18 | +from selenium.common.exceptions import TimeoutException |
| 19 | + |
| 20 | + |
| 21 | +class File(BaseControl): |
| 22 | + """ |
| 23 | + Entity-Component: File |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, browser, container) -> None: |
| 27 | + """ |
| 28 | + :param browser: The selenium webdriver |
| 29 | + :param container: The locator of the container where the control is located in. |
| 30 | + """ |
| 31 | + super().__init__(browser, container) |
| 32 | + self.elements.update( |
| 33 | + { |
| 34 | + "input": Selector(select=container.select + " input"), |
| 35 | + "support_message": Selector( |
| 36 | + select=container.select |
| 37 | + + ' [data-test="file"] [data-test="file-supports"]' |
| 38 | + ), |
| 39 | + "error_text": Selector( |
| 40 | + select=container.select + ' [data-test="file"] [data-test="help"]' |
| 41 | + ), |
| 42 | + "selected": Selector( |
| 43 | + select=container.select + ' [data-test="item"] [data-test="label"]' |
| 44 | + ), |
| 45 | + "cancel_selected": Selector( |
| 46 | + select=container.select + ' [data-test="remove"]' |
| 47 | + ), |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + def set_value(self, value: str) -> None: |
| 52 | + """ |
| 53 | + set value of the File input |
| 54 | + """ |
| 55 | + self.wait_for("input") |
| 56 | + self.input.send_keys(value) |
| 57 | + |
| 58 | + def get_value(self) -> str: |
| 59 | + """ |
| 60 | + get the name of the selected file |
| 61 | + :return: Str The name of the selected file |
| 62 | + """ |
| 63 | + try: |
| 64 | + return self.selected.get_attribute("innerText").strip() |
| 65 | + except TimeoutException: |
| 66 | + pass |
| 67 | + return "" |
| 68 | + |
| 69 | + def get_support_message(self) -> str: |
| 70 | + """ |
| 71 | + get the file support message |
| 72 | + :return: Str file support message |
| 73 | + """ |
| 74 | + return self.support_message.get_attribute("innerText").strip() |
| 75 | + |
| 76 | + def get_error_text(self) -> str: |
| 77 | + """ |
| 78 | + get the file validation error text |
| 79 | + :return: Str error message of the file validation |
| 80 | + """ |
| 81 | + try: |
| 82 | + return self.error_text.get_attribute("innerText").strip() |
| 83 | + except TimeoutException: |
| 84 | + pass |
| 85 | + return "" |
| 86 | + |
| 87 | + def cancel_selected_value(self) -> bool: |
| 88 | + """ |
| 89 | + Cancels the currently selected value in the File component |
| 90 | + :return: Bool whether canceling the selected item was successful, else raises an error |
| 91 | + """ |
| 92 | + self.wait_to_be_clickable("cancel_selected") |
| 93 | + self.cancel_selected.click() |
| 94 | + return True |
0 commit comments