Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I reuse a WebDriverWait object?

I have a page object which interacts with few elements on the DOM. If I create a WebDriverWait object on my page object initialization as an instance member, can I use it for all of the waits?

Or if I wanna wait for two separate element, it's better to have two WebDriverWaits?

I'm experiencing some weird TimeoutExceptions and I wonder it might be that. Like:

class MyPage(object):
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)

    def get_search_box(self):
        return self.wait.until(EC.presence_of_element_located('srch'))

    def get_search_btn(self):
        return self.wait.until(EC.presence_of_element_located('btn'))

versus:

    def get_search_btn(self):
        wait = WebDriverWait(self.driver, 10)
        return wait.until(EC.presence_of_element_located('btn'))
like image 521
Sam R. Avatar asked Aug 01 '15 23:08

Sam R.


1 Answers

I guess what you need is a function which you can reuse as required. Take a look at the following function for example:

def wait_for_element_to_be_visible(self, *locator):
    """Wait for an element to become visible"""
    self.selenium.implicitly_wait(0)
    try:
        WebDriverWait(self.selenium, self.timeout).until(
            lambda s: self._selenium_root.find_element(*locator).is_displayed())
    except TimeoutException:
        Assert.fail(TimeoutException)
    finally:
        self.selenium.implicitly_wait(self.testsetup.default_implicit_wait)
like image 65
Amit Verma Avatar answered Sep 29 '22 22:09

Amit Verma