|
1 |
| -from selenium.common.exceptions import WebDriverException |
2 |
| -from selenium.webdriver.support.ui import WebDriverWait |
3 |
| -from selenium.webdriver.support import expected_conditions as EC |
4 |
| -from keywordgroup import KeywordGroup |
5 |
| - |
6 |
| - |
7 |
| -class _AlertKeywords(KeywordGroup): |
8 |
| - |
9 |
| - def __init__(self): |
10 |
| - self._cancel_on_next_confirmation = False |
11 |
| - |
12 |
| - # Public |
13 |
| - |
14 |
| - def input_text_into_prompt(self, text): |
15 |
| - """Types the given `text` into alert box. """ |
16 |
| - alert = None |
17 |
| - try: |
18 |
| - alert = self._wait_alert() |
19 |
| - alert.send_keys(text) |
20 |
| - except WebDriverException: |
21 |
| - raise RuntimeError('There were no alerts') |
22 |
| - |
23 |
| - def alert_should_be_present(self, text=''): |
24 |
| - """Verifies an alert is present and dismisses it. |
25 |
| -
|
26 |
| - If `text` is a non-empty string, then it is also verified that the |
27 |
| - message of the alert equals to `text`. |
28 |
| -
|
29 |
| - Will fail if no alert is present. Note that following keywords |
30 |
| - will fail unless the alert is dismissed by this |
31 |
| - keyword or another like `Get Alert Message`. |
32 |
| - """ |
33 |
| - alert_text = self.get_alert_message() |
34 |
| - if text and alert_text != text: |
35 |
| - raise AssertionError("Alert text should have been " |
36 |
| - "'%s' but was '%s'" |
37 |
| - % (text, alert_text)) |
38 |
| - |
39 |
| - def choose_cancel_on_next_confirmation(self): |
40 |
| - """Cancel will be selected the next time `Confirm Action` is used.""" |
41 |
| - self._cancel_on_next_confirmation = True |
42 |
| - |
43 |
| - def choose_ok_on_next_confirmation(self): |
44 |
| - """Undo the effect of using keywords `Choose Cancel On Next Confirmation`. Note |
45 |
| - that Selenium's overridden window.confirm() function will normally |
46 |
| - automatically return true, as if the user had manually clicked OK, so |
47 |
| - you shouldn't need to use this command unless for some reason you need |
48 |
| - to change your mind prior to the next confirmation. After any |
49 |
| - confirmation, Selenium will resume using the default behavior for |
50 |
| - future confirmations, automatically returning true (OK) unless/until |
51 |
| - you explicitly use `Choose Cancel On Next Confirmation` for each |
52 |
| - confirmation. |
53 |
| -
|
54 |
| - Note that every time a confirmation comes up, you must |
55 |
| - consume it by using a keywords such as `Get Alert Message`, or else |
56 |
| - the following selenium operations will fail. |
57 |
| - """ |
58 |
| - self._cancel_on_next_confirmation = False |
59 |
| - |
60 |
| - def confirm_action(self): |
61 |
| - """Dismisses currently shown confirmation dialog and returns it's message. |
62 |
| -
|
63 |
| - By default, this keyword chooses 'OK' option from the dialog. If |
64 |
| - 'Cancel' needs to be chosen, keyword `Choose Cancel On Next |
65 |
| - Confirmation` must be called before the action that causes the |
66 |
| - confirmation dialog to be shown. |
67 |
| -
|
68 |
| - Examples: |
69 |
| - | Click Button | Send | # Shows a confirmation dialog | |
70 |
| - | ${message}= | Confirm Action | # Chooses Ok | |
71 |
| - | Should Be Equal | ${message} | Are your sure? | |
72 |
| - | | | | |
73 |
| - | Choose Cancel On Next Confirmation | | | |
74 |
| - | Click Button | Send | # Shows a confirmation dialog | |
75 |
| - | Confirm Action | | # Chooses Cancel | |
76 |
| - """ |
77 |
| - text = self._close_alert(not self._cancel_on_next_confirmation) |
78 |
| - self._cancel_on_next_confirmation = False |
79 |
| - return text |
80 |
| - |
81 |
| - def get_alert_message(self, dismiss=True): |
82 |
| - """Returns the text of current JavaScript alert. |
83 |
| -
|
84 |
| - By default the current JavaScript alert will be dismissed. |
85 |
| - This keyword will fail if no alert is present. Note that |
86 |
| - following keywords will fail unless the alert is |
87 |
| - dismissed by this keyword or another like `Get Alert Message`. |
88 |
| - """ |
89 |
| - if dismiss: |
90 |
| - return self._close_alert() |
91 |
| - else: |
92 |
| - return self._read_alert() |
93 |
| - |
94 |
| - def dismiss_alert(self, accept=True): |
95 |
| - """ Returns true if alert was confirmed, false if it was dismissed |
96 |
| -
|
97 |
| - This keyword will fail if no alert is present. Note that |
98 |
| - following keywords will fail unless the alert is |
99 |
| - dismissed by this keyword or another like `Get Alert Message`. |
100 |
| - """ |
101 |
| - return self._handle_alert(accept) |
102 |
| - |
103 |
| - # Private |
104 |
| - |
105 |
| - def _close_alert(self, confirm=True): |
106 |
| - try: |
107 |
| - text = self._read_alert() |
108 |
| - self._handle_alert(confirm) |
109 |
| - return text |
110 |
| - except WebDriverException: |
111 |
| - raise RuntimeError('There were no alerts') |
112 |
| - |
113 |
| - def _read_alert(self): |
114 |
| - alert = None |
115 |
| - try: |
116 |
| - alert = self._wait_alert() |
117 |
| - # collapse new lines chars |
118 |
| - return ' '.join(alert.text.splitlines()) |
119 |
| - except WebDriverException: |
120 |
| - raise RuntimeError('There were no alerts') |
121 |
| - |
122 |
| - def _handle_alert(self, confirm=True): |
123 |
| - try: |
124 |
| - alert = self._wait_alert() |
125 |
| - if not confirm: |
126 |
| - alert.dismiss() |
127 |
| - return False |
128 |
| - else: |
129 |
| - alert.accept() |
130 |
| - return True |
131 |
| - except WebDriverException: |
132 |
| - raise RuntimeError('There were no alerts') |
133 |
| - |
134 |
| - def _wait_alert(self): |
135 |
| - return WebDriverWait(self._current_browser(), 1).until( |
136 |
| - EC.alert_is_present()) |
| 1 | +from __future__ import absolute_import |
| 2 | +from selenium.common.exceptions import WebDriverException |
| 3 | +from selenium.webdriver.support.ui import WebDriverWait |
| 4 | +from selenium.webdriver.support import expected_conditions as EC |
| 5 | +from .keywordgroup import KeywordGroup |
| 6 | + |
| 7 | + |
| 8 | +class _AlertKeywords(KeywordGroup): |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + self._cancel_on_next_confirmation = False |
| 12 | + |
| 13 | + # Public |
| 14 | + |
| 15 | + def input_text_into_prompt(self, text): |
| 16 | + """Types the given `text` into alert box. """ |
| 17 | + alert = None |
| 18 | + try: |
| 19 | + alert = self._wait_alert() |
| 20 | + alert.send_keys(text) |
| 21 | + except WebDriverException: |
| 22 | + raise RuntimeError('There were no alerts') |
| 23 | + |
| 24 | + def alert_should_be_present(self, text=''): |
| 25 | + """Verifies an alert is present and dismisses it. |
| 26 | +
|
| 27 | + If `text` is a non-empty string, then it is also verified that the |
| 28 | + message of the alert equals to `text`. |
| 29 | +
|
| 30 | + Will fail if no alert is present. Note that following keywords |
| 31 | + will fail unless the alert is dismissed by this |
| 32 | + keyword or another like `Get Alert Message`. |
| 33 | + """ |
| 34 | + alert_text = self.get_alert_message() |
| 35 | + if text and alert_text != text: |
| 36 | + raise AssertionError("Alert text should have been " |
| 37 | + "'%s' but was '%s'" |
| 38 | + % (text, alert_text)) |
| 39 | + |
| 40 | + def choose_cancel_on_next_confirmation(self): |
| 41 | + """Cancel will be selected the next time `Confirm Action` is used.""" |
| 42 | + self._cancel_on_next_confirmation = True |
| 43 | + |
| 44 | + def choose_ok_on_next_confirmation(self): |
| 45 | + """Undo the effect of using keywords `Choose Cancel On Next Confirmation`. Note |
| 46 | + that Selenium's overridden window.confirm() function will normally |
| 47 | + automatically return true, as if the user had manually clicked OK, so |
| 48 | + you shouldn't need to use this command unless for some reason you need |
| 49 | + to change your mind prior to the next confirmation. After any |
| 50 | + confirmation, Selenium will resume using the default behavior for |
| 51 | + future confirmations, automatically returning true (OK) unless/until |
| 52 | + you explicitly use `Choose Cancel On Next Confirmation` for each |
| 53 | + confirmation. |
| 54 | +
|
| 55 | + Note that every time a confirmation comes up, you must |
| 56 | + consume it by using a keywords such as `Get Alert Message`, or else |
| 57 | + the following selenium operations will fail. |
| 58 | + """ |
| 59 | + self._cancel_on_next_confirmation = False |
| 60 | + |
| 61 | + def confirm_action(self): |
| 62 | + """Dismisses currently shown confirmation dialog and returns it's message. |
| 63 | +
|
| 64 | + By default, this keyword chooses 'OK' option from the dialog. If |
| 65 | + 'Cancel' needs to be chosen, keyword `Choose Cancel On Next |
| 66 | + Confirmation` must be called before the action that causes the |
| 67 | + confirmation dialog to be shown. |
| 68 | +
|
| 69 | + Examples: |
| 70 | + | Click Button | Send | # Shows a confirmation dialog | |
| 71 | + | ${message}= | Confirm Action | # Chooses Ok | |
| 72 | + | Should Be Equal | ${message} | Are your sure? | |
| 73 | + | | | | |
| 74 | + | Choose Cancel On Next Confirmation | | | |
| 75 | + | Click Button | Send | # Shows a confirmation dialog | |
| 76 | + | Confirm Action | | # Chooses Cancel | |
| 77 | + """ |
| 78 | + text = self._close_alert(not self._cancel_on_next_confirmation) |
| 79 | + self._cancel_on_next_confirmation = False |
| 80 | + return text |
| 81 | + |
| 82 | + def get_alert_message(self, dismiss=True): |
| 83 | + """Returns the text of current JavaScript alert. |
| 84 | +
|
| 85 | + By default the current JavaScript alert will be dismissed. |
| 86 | + This keyword will fail if no alert is present. Note that |
| 87 | + following keywords will fail unless the alert is |
| 88 | + dismissed by this keyword or another like `Get Alert Message`. |
| 89 | + """ |
| 90 | + if dismiss: |
| 91 | + return self._close_alert() |
| 92 | + else: |
| 93 | + return self._read_alert() |
| 94 | + |
| 95 | + def dismiss_alert(self, accept=True): |
| 96 | + """ Returns true if alert was confirmed, false if it was dismissed |
| 97 | +
|
| 98 | + This keyword will fail if no alert is present. Note that |
| 99 | + following keywords will fail unless the alert is |
| 100 | + dismissed by this keyword or another like `Get Alert Message`. |
| 101 | + """ |
| 102 | + return self._handle_alert(accept) |
| 103 | + |
| 104 | + # Private |
| 105 | + |
| 106 | + def _close_alert(self, confirm=True): |
| 107 | + try: |
| 108 | + text = self._read_alert() |
| 109 | + self._handle_alert(confirm) |
| 110 | + return text |
| 111 | + except WebDriverException: |
| 112 | + raise RuntimeError('There were no alerts') |
| 113 | + |
| 114 | + def _read_alert(self): |
| 115 | + alert = None |
| 116 | + try: |
| 117 | + alert = self._wait_alert() |
| 118 | + # collapse new lines chars |
| 119 | + return ' '.join(alert.text.splitlines()) |
| 120 | + except WebDriverException: |
| 121 | + raise RuntimeError('There were no alerts') |
| 122 | + |
| 123 | + def _handle_alert(self, confirm=True): |
| 124 | + try: |
| 125 | + alert = self._wait_alert() |
| 126 | + if not confirm: |
| 127 | + alert.dismiss() |
| 128 | + return False |
| 129 | + else: |
| 130 | + alert.accept() |
| 131 | + return True |
| 132 | + except WebDriverException: |
| 133 | + raise RuntimeError('There were no alerts') |
| 134 | + |
| 135 | + def _wait_alert(self): |
| 136 | + return WebDriverWait(self._current_browser(), 1).until( |
| 137 | + EC.alert_is_present()) |
0 commit comments