def _validate_url(self, url): """ Ensure that url string given is either a valid Insights URL or a valid Insights query string. :param url: the url to validate :type url: str """ try: # try full URL match re.match(f'^{BASE_URL}' + r'/analytics/explore_embed\?insights_page=[a-zA-Z0-9_]+(=){0,2}$', url) \ .group(0) except AttributeError: try: # try saved report URL match re.match(f'^{BASE_URL}' + r'/analytics/reports/[0-9]+$', url) \ .group(0) except AttributeError: try: # try query parameter match re.match(r'^[a-zA-Z0-9_]+(=){0,2}$', url) \ .group(0) except AttributeError: try: re.match( f'^{BASE_URL}' + r'/analytics/reports/new\?looker_explore_name=[a-zA-Z0-9]+&qid=[a-zA-Z0-9]+', url).group(0) except AttributeError: raise InvalidURLError()
def validate_url_school(self): """Ensure that the current URL leads to a valid school's login page""" if self._browser.element_exists_by_xpath('//span[text()=\'Please ' 'select your school to ' 'sign in.\']'): raise InvalidURLError( "The school specified in the URL is not valid")
def _switch_to_looker_iframe(self): """Bring the Looker iframe into focus on the insights page""" exists_once_iframe_in_focus = "//i[@class='lk-icon-gear']" if not self._browser.element_exists_by_xpath( exists_once_iframe_in_focus): if self._browser.element_exists_by_xpath( "//div[@style='display: none']"): raise InvalidURLError("Insights query string is malformed") iframe_xpath = "//iframe[@id='explore-iframe']" self._browser.wait_until_element_exists_by_xpath(iframe_xpath) self._browser.switch_to_frame_by_xpath(iframe_xpath) if self._browser.element_exists_by_xpath( "//div[@class='error-http-status-code']"): raise InvalidURLError("Insights query string is malformed") self._browser.wait_until_element_exists_by_xpath( exists_once_iframe_in_focus)
def _validate_url(self, url): """ Ensure that the given URL is a valid login URL :param url: the url to validate :type url: str """ try: re.match(r'^https://[a-zA-Z]+\.joinhandshake\.com(/login)?$', url) \ .group(0) except AttributeError: raise InvalidURLError()
def __enter__(self) -> HandshakeBrowser: """ Open a web browser and log into Handshake, beginning the session :returns: a logged-in HandshakeBrowser :rtype: HandshakeBrowser """ self._browser.get(self._login_url) if self._school_is_invalid(): raise InvalidURLError('Invalid school in login URL') # try to log in via the new style of login page if the old one fails login_page = LoginPage(self._login_url, self._browser) login_page.login(self._email, self._password) self._browser.update_constants() return self._browser
def __init__(self, url_string: str, browser: HandshakeBrowser): """ :param url_string: either a full insights URL or just the alphanumeric query string specifying the exact report :type url_string: str :param browser: a logged-in HandshakeBrowser :type browser: HandshakeBrowser """ super().__init__(url_string, browser) if self._browser.element_exists_by_xpath( "//span[text()='Select some dimensions or measures.' and @aria-hidden='false']" ): raise InvalidURLError( 'Insights URL has no dimensions or measures selected') self.modal = _DownloadModal(self._browser)
def _validate_url_str(url: str): """ Determine whether or not a given Handshake URL is valid :param login_url: the login url to test :type login_url: str """ try: re.match(r'^https://[a-zA-Z]+\.joinhandshake\.com', url) \ .group(0) except AttributeError: raise InvalidURLError( 'URL must be of the form ' '"https://app.joinhandshake.com[/...]" or ' '"https://[school name].joinhandshake.com[/...]"')
def _wait_until_page_is_loaded(self): """Wait until the page has finished loading.""" if self._browser.element_exists_by_xpath( '//div[text()="Invalid report link"]'): raise InvalidURLError('Insights URL is invalid') self._switch_to_looker_iframe()
def _validate_page_exists(self): """Determine whether the current page exists or gave a 404 error.""" if self.element_exists_by_xpath("//p[contains(text(), 'You may want " "to head back to the homepage.')]"): raise InvalidURLError(self._browser.current_url)