1# Copyright 2008-2011 Nokia Networks
2# Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3# Copyright 2016-     Robot Framework Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#    http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from SeleniumLibrary.base import LibraryComponent, keyword
18
19
20class FrameKeywords(LibraryComponent):
21
22    @keyword
23    def select_frame(self, locator):
24        """Sets frame identified by ``locator`` as the current frame.
25
26        See the `Locating elements` section for details about the locator
27        syntax.
28
29        Works both with frames and iframes. Use `Unselect Frame` to cancel
30        the frame selection and return to the main frame.
31
32        Example:
33        | `Select Frame`   | top-frame | # Select frame with id or name 'top-frame'   |
34        | `Click Link`     | example   | # Click link 'example' in the selected frame |
35        | `Unselect Frame` |           | # Back to main frame.                        |
36        | `Select Frame`   | //iframe[@name='xxx'] | # Select frame using xpath       |
37        """
38        self.info("Selecting frame '%s'." % locator)
39        element = self.find_element(locator)
40        self.driver.switch_to.frame(element)
41
42    @keyword
43    def unselect_frame(self):
44        """Sets the main frame as the current frame.
45
46        In practice cancels the previous `Select Frame` call.
47        """
48        self.driver.switch_to.default_content()
49
50    @keyword
51    def current_frame_should_contain(self, text, loglevel='TRACE'):
52        """Verifies that current frame contains ``text``.
53
54        See `Page Should Contain` for explanation about the ``loglevel``
55        argument.
56
57        Prior to SeleniumLibrary 3.0 this keyword was named
58        `Current Frame Contains`.
59        """
60        if not self.is_text_present(text):
61            self.log_source(loglevel)
62            raise AssertionError("Frame should have contained text '%s' "
63                                 "but did not." % text)
64        self.info("Current frame contains text '%s'." % text)
65
66    @keyword
67    def current_frame_contains(self, text, loglevel='TRACE'):
68        """*DEPRECATED in SeleniumLibrary 3.2.* Use `Current Frame Should Contain` instead."""
69        self.current_frame_should_contain(text, loglevel)
70
71    @keyword
72    def current_frame_should_not_contain(self, text, loglevel='TRACE'):
73        """Verifies that current frame does not contains ``text``.
74
75        See `Page Should Contain` for explanation about the ``loglevel``
76        argument.
77        """
78        if self.is_text_present(text):
79            self.log_source(loglevel)
80            raise AssertionError("Frame should not have contained text '%s' "
81                                 "but it did." % text)
82        self.info("Current frame did not contain text '%s'." % text)
83
84    @keyword
85    def frame_should_contain(self, locator, text, loglevel='TRACE'):
86        """Verifies that frame identified by ``locator`` contains ``text``.
87
88        See the `Locating elements` section for details about the locator
89        syntax.
90
91        See `Page Should Contain` for explanation about the ``loglevel``
92        argument.
93        """
94        if not self._frame_contains(locator, text):
95            self.log_source(loglevel)
96            raise AssertionError("Frame '%s' should have contained text '%s' "
97                                 "but did not." % (locator, text))
98        self.info("Frame '%s' contains text '%s'." % (locator, text))
99
100    def _frame_contains(self, locator, text):
101        element = self.find_element(locator)
102        self.driver.switch_to.frame(element)
103        self.info("Searching for text from frame '%s'." % locator)
104        found = self.is_text_present(text)
105        self.driver.switch_to.default_content()
106        return found
107