Ejemplo n.º 1
0
def get_slots(driver, prefs, slot_route):
    if slot_route.waypoints[-1].dest not in remove_qs(driver.current_url):
        try:
            handle_redirect(driver, slot_route.args.ignore_oos)
        except UnhandledRedirect:
            log.warning('Unhandled redirect')
            slot_route.navigate(driver)
    log.info('Checking for available slots')
    preferred_slots = []
    slot_container = wait_for_element(driver, config.Locators.SLOT_CONTAINER)
    slots = [
        SlotElement(element)
        for element in slot_container.find_elements(*config.Locators.SLOT)
    ]
    if slots:
        log.info('Found {} slots: \n{}'.format(
            len(slots), '\n'.join([s.full_name for s in slots])))
    if slots and prefs:
        log.info('Comparing available slots to prefs')
        for cmp in prefs:
            if cmp.startswith('any'):
                _pref = [
                    s for s in slots
                    if cmp.replace('any', '') in clean_slotname(s)
                ]
            else:
                _pref = [s for s in slots if clean_slotname(s).startswith(cmp)]
            preferred_slots.extend(_pref)
        if preferred_slots:
            log.info('Found {} preferred slots: {}'.format(
                len(preferred_slots),
                '\n' + '\n'.join([p.full_name for p in preferred_slots])))
        return preferred_slots
    else:
        return slots
Ejemplo n.º 2
0
 def is_logged_in(self):
     if self.current_url == self.site_config.BASE_URL:
         try:
             text = wait_for_element(self.driver, self.Locators.LOGIN).text
             return self.Patterns.NOT_LOGGED_IN not in text
         except Exception:
             return False
     elif self.Patterns.AUTH_URL in self.current_url:
         return False
     else:
         # Lazily assume true if we are anywhere but BASE_URL / AUTH pattern
         return True
Ejemplo n.º 3
0
 def navigate_waypoint(self, driver, waypoint, timeout, valid_dest):
     log.info('Navigating ' + str(waypoint))
     elem = wait_for_element(driver, waypoint.locator, timeout=timeout)
     jitter(.4)
     click_when_enabled(driver, elem)
     try:
         WebDriverWait(driver, timeout).until(EC.staleness_of(elem))
     except TimeoutException:
         pass
     current = remove_qs(driver.current_url)
     if current == BASE_URL + waypoint.dest:
         log.info("Navigated to '{}'".format(waypoint.dest))
     elif valid_dest and any(d in current for d in valid_dest):
         log.info("Navigated to valid dest '{}'".format(current))
     else:
         raise NavigationException("Navigation to '{}' failed".format(
             waypoint.dest))
Ejemplo n.º 4
0
def handle_throttle(browser, timeout_mins=10):
    alert('Throttled', 'Sosumi')
    # Dump source until we're sure we have correct locator for continue button
    dump_source(browser.driver)
    try:
        click_when_enabled(
            browser.driver,
            wait_for_element(browser.driver,
                             browser.Locators.THROTTLE_CONTINUE),
            timeout=60)
    except Exception as e:
        log.error(e)
    t = datetime.now()
    while browser.Patterns.THROTTLE_URL in browser.current_url:
        if int((datetime.now() - t).total_seconds()) > timeout_mins * 60:
            raise UnhandledRedirect(
                'Throttled and timed out waiting for user input')
        sleep(1)
Ejemplo n.º 5
0
 def navigate_waypoint(self, waypoint, timeout, valid_dest):
     if callable(waypoint.callable):
         log.info('Executing {}() before navigation'.format(
             waypoint.callable.__name__))
         waypoint.callable(browser=self)
     log.info('Navigating ' + str(waypoint))
     elem = wait_for_element(self.driver, waypoint.locator, timeout=timeout)
     jitter(.4)
     click_when_enabled(self.driver, elem)
     try:
         WebDriverWait(self.driver, timeout).until(EC.staleness_of(elem))
     except TimeoutException:
         pass
     if waypoint.check_current(self.current_url):
         log.info("Navigated to '{}'".format(
             waypoint.check_current(self.current_url)))
     elif valid_dest and any(d in self.current_url for d in valid_dest):
         log.info("Navigated to valid dest '{}'".format(self.current_url))
     else:
         raise NavigationException("Navigation to '{}' failed".format(
             waypoint.dest))
Ejemplo n.º 6
0
def handle_oos(browser, timeout_mins=10):
    try:
        browser.save_removed_items()
    except Exception:
        log.error('Could not save removed items')
    if browser.args.ignore_oos:
        log.warning('Attempting to proceed through OOS alert')
        click_when_enabled(
            browser.driver,
            wait_for_element(browser.driver, browser.Locators.OOS_CONTINUE))
    else:
        t = datetime.now()
        alert(
            "An item is out of stock. Press continue if you'd like to proceed",
            'Sosumi')
        while browser.Patterns.OOS_URL in browser.current_url:
            if int((datetime.now() - t).total_seconds()) > timeout_mins * 60:
                raise ItemOutOfStock(
                    'Encountered OOS alert and timed out waiting for user '
                    'input\n Use `ignore-oos` to bypass these alerts')
            sleep(1)
Ejemplo n.º 7
0
def handle_redirect(driver,
                    ignore_oos,
                    valid_dest=None,
                    timeout=None,
                    route=None):
    current = remove_qs(driver.current_url)
    log.warning("Redirected to: '{}'".format(current))

    if Patterns.AUTH_URL in current:
        wait_for_auth(driver)
    elif Patterns.OOS_URL in current:
        try:
            save_removed_items(driver)
        except Exception:
            log.error('Could not save removed items')
        if ignore_oos:
            log.warning('Attempting to proceed through OOS alert')
            click_when_enabled(driver,
                               wait_for_element(driver, Locators.OOS_CONTINUE))
        else:
            raise ItemOutOfStock(
                'Encountered OOS Alert. Use `ignore-oos` to bypass')
    elif route and current == route.route_start and route.waypoints_reached:
        raise RouteRedirect()
    elif valid_dest and timeout:
        log.warning(
            'Handling unknown redirect (timeout in {}s)'.format(timeout))
        try:
            WebDriverWait(driver,
                          timeout).until(EC.url_matches('|'.join(valid_dest)))
        except TimeoutException:
            raise UnhandledRedirect(
                "Timed out waiting for redirect to a valid dest\n"
                "Current URL: '{}'".format(driver.current_url))
    else:
        raise UnhandledRedirect()