~brendan-donegan/+junk/python3-selenium

« back to all changes in this revision

Viewing changes to py/selenium/webdriver/support/wait.py

  • Committer: Brendan Donegan
  • Date: 2015-11-24 23:05:38 UTC
  • Revision ID: brendan.donegan@canonical.com-20151124230538-76iylks5vdjkoc75
Python3 selenium

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Licensed to the Software Freedom Conservancy (SFC) under one
 
2
# or more contributor license agreements.  See the NOTICE file
 
3
# distributed with this work for additional information
 
4
# regarding copyright ownership.  The SFC licenses this file
 
5
# to you under the Apache License, Version 2.0 (the
 
6
# "License"); you may not use this file except in compliance
 
7
# with the License.  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,
 
12
# software distributed under the License is distributed on an
 
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
14
# KIND, either express or implied.  See the License for the
 
15
# specific language governing permissions and limitations
 
16
# under the License.
 
17
 
 
18
import time
 
19
from selenium.common.exceptions import NoSuchElementException
 
20
from selenium.common.exceptions import TimeoutException
 
21
 
 
22
POLL_FREQUENCY = 0.5  # How long to sleep inbetween calls to the method
 
23
IGNORED_EXCEPTIONS = (NoSuchElementException,)  # exceptions ignored during calls to the method
 
24
 
 
25
 
 
26
class WebDriverWait(object):
 
27
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
 
28
        """Constructor, takes a WebDriver instance and timeout in seconds.
 
29
 
 
30
           :Args:
 
31
            - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
 
32
            - timeout - Number of seconds before timing out
 
33
            - poll_frequency - sleep interval between calls
 
34
              By default, it is 0.5 second.
 
35
            - ignored_exceptions - iterable structure of exception classes ignored during calls.
 
36
              By default, it contains NoSuchElementException only.
 
37
 
 
38
           Example:
 
39
            from selenium.webdriver.support.ui import WebDriverWait \n
 
40
            element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
 
41
            is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
 
42
                        until_not(lambda x: x.find_element_by_id("someId").is_displayed())
 
43
        """
 
44
        self._driver = driver
 
45
        self._timeout = timeout
 
46
        self._poll = poll_frequency
 
47
        # avoid the divide by zero
 
48
        if self._poll == 0:
 
49
            self._poll = POLL_FREQUENCY
 
50
        exceptions = list(IGNORED_EXCEPTIONS)
 
51
        if ignored_exceptions is not None:
 
52
            try:
 
53
                exceptions.extend(iter(ignored_exceptions))
 
54
            except TypeError:  # ignored_exceptions is not iterable
 
55
                exceptions.append(ignored_exceptions)
 
56
        self._ignored_exceptions = tuple(exceptions)
 
57
 
 
58
    def __repr__(self):
 
59
        return '<{0.__module__}.{0.__name__} (session="{1}")>'.format(
 
60
            type(self), self._driver.session_id)
 
61
 
 
62
    def until(self, method, message=''):
 
63
        """Calls the method provided with the driver as an argument until the \
 
64
        return value is not False."""
 
65
        screen = None
 
66
        stacktrace = None
 
67
 
 
68
        end_time = time.time() + self._timeout
 
69
        while True:
 
70
            try:
 
71
                value = method(self._driver)
 
72
                if value:
 
73
                    return value
 
74
            except self._ignored_exceptions as exc:
 
75
                screen = getattr(exc, 'screen', None)
 
76
                stacktrace = getattr(exc, 'stacktrace', None)
 
77
            time.sleep(self._poll)
 
78
            if time.time() > end_time:
 
79
                break
 
80
        raise TimeoutException(message, screen, stacktrace)
 
81
 
 
82
    def until_not(self, method, message=''):
 
83
        """Calls the method provided with the driver as an argument until the \
 
84
        return value is False."""
 
85
        end_time = time.time() + self._timeout
 
86
        while True:
 
87
            try:
 
88
                value = method(self._driver)
 
89
                if not value:
 
90
                    return value
 
91
            except self._ignored_exceptions:
 
92
                return True
 
93
            time.sleep(self._poll)
 
94
            if time.time() > end_time:
 
95
                break
 
96
        raise TimeoutException(message)