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
17import os
18
19from robot.api import logger
20from robot.libraries.BuiltIn import BuiltIn, RobotNotRunningError
21
22from SeleniumLibrary.utils import is_noney, timestr_to_secs
23
24from .context import ContextAware
25from .robotlibcore import PY2
26
27
28class LibraryComponent(ContextAware):
29
30    def info(self, msg, html=False):
31        logger.info(msg, html)
32
33    def debug(self, msg, html=False):
34        logger.debug(msg, html)
35
36    def log(self, msg, level='INFO', html=False):
37        if not is_noney(level):
38            logger.write(msg, level.upper(), html)
39
40    def warn(self, msg, html=False):
41        logger.warn(msg, html)
42
43    def log_source(self, loglevel='INFO'):
44        self.ctx.log_source(loglevel)
45
46    def assert_page_contains(self, locator, tag=None, message=None,
47                             loglevel='TRACE'):
48        if not self.find_element(locator, tag, required=False):
49            self.log_source(loglevel)
50            if is_noney(message):
51                message = ("Page should have contained %s '%s' but did not."
52                           % (tag or 'element', locator))
53            raise AssertionError(message)
54        logger.info("Current page contains %s '%s'."
55                    % (tag or 'element', locator))
56
57    def assert_page_not_contains(self, locator, tag=None, message=None,
58                                 loglevel='TRACE'):
59        if self.find_element(locator, tag, required=False):
60            self.log_source(loglevel)
61            if is_noney(message):
62                message = ("Page should not have contained %s '%s'."
63                           % (tag or 'element', locator))
64            raise AssertionError(message)
65        logger.info("Current page does not contain %s '%s'."
66                    % (tag or 'element', locator))
67
68    def get_timeout(self, timeout=None):
69        if is_noney(timeout):
70            return self.ctx.timeout
71        return timestr_to_secs(timeout)
72
73    @property
74    def log_dir(self):
75        try:
76            logfile = BuiltIn().get_variable_value('${LOG FILE}')
77            if logfile == 'NONE':
78                return BuiltIn().get_variable_value('${OUTPUTDIR}')
79            return os.path.dirname(logfile)
80        except RobotNotRunningError:
81            return os.getcwdu() if PY2 else os.getcwd()
82