def save_all_webdriver_or_ggr_logs(cls, test_name, test_passed, ggr=False): """Get all webdriver or GGR logs of each driver and write them to log files :param test_name: test that has generated these logs :param test_passed: True if the test has passed :param ggr: True if driver should be ggr or selenoid """ log_name = '{} [driver {}]' if len(cls.driver_wrappers) > 1 else '{}' driver_index = 1 for driver_wrapper in cls.driver_wrappers: if driver_wrapper.driver and ( driver_wrapper.config.getboolean_optional( 'Server', 'logs_enabled') or not test_passed): try: log_file_name = get_valid_filename( log_name.format(test_name, driver_index)) if ggr and driver_wrapper.server_type in [ 'ggr', 'selenoid' ]: Selenoid(driver_wrapper).download_session_log( log_file_name) elif not ggr and driver_wrapper.server_type not in [ 'ggr', 'selenoid' ]: driver_wrapper.utils.save_webdriver_logs(log_file_name) except Exception as exc: # Capture exceptions to avoid errors in teardown method due to session timeouts driver_wrapper.logger.warning( 'Error downloading webdriver logs: %s' % exc) driver_index += 1
def get_remote_node(self): """Return the remote node that it's executing the actual test session :returns: tuple with server type (local, grid, ggr, selenium) and remote node name """ logging.getLogger("requests").setLevel(logging.WARNING) remote_node = None server_type = 'local' if self.driver_wrapper.config.getboolean_optional('Server', 'enabled'): # Request session info from grid hub session_id = self.driver_wrapper.driver.session_id self.logger.debug("Trying to identify remote node") try: # Request session info from grid hub and extract remote node url = '{}/grid/api/testsession?session={}'.format( self.get_server_url(), session_id) proxy_id = requests.get(url).json()['proxyId'] remote_node = urlparse(proxy_id).hostname if urlparse( proxy_id).hostname else proxy_id server_type = 'grid' self.logger.debug("Test running in remote node %s", remote_node) except (ValueError, KeyError): try: # Request session info from GGR and extract remote node from toolium.selenoid import Selenoid remote_node = Selenoid( self.driver_wrapper).get_selenoid_info()['Name'] server_type = 'ggr' self.logger.debug("Test running in a GGR remote node %s", remote_node) except Exception: try: # The remote node is a Selenoid node url = '{}/status'.format(self.get_server_url()) requests.get(url).json()['total'] remote_node = self.driver_wrapper.config.get( 'Server', 'host') server_type = 'selenoid' self.logger.debug("Test running in a Selenoid node %s", remote_node) except Exception: # The remote node is not a grid node or the session has been closed remote_node = self.driver_wrapper.config.get( 'Server', 'host') server_type = 'selenium' self.logger.debug("Test running in a Selenium node %s", remote_node) return server_type, remote_node
def download_remote_video(self, server_type, video_name): """Download the video recorded in the remote node and save it in videos folder :param server_type: server type (grid, ggr, selenoid) :param video_name: video name """ video_name = get_valid_filename(video_name) if server_type == 'grid': # Download video from Grid Extras try: video_url = self._get_remote_video_url(self.driver_wrapper.remote_node, self.driver_wrapper.session_id) except requests.exceptions.ConnectionError: self.logger.warning("Remote server seems not to have video capabilities") return if not video_url: self.logger.warning("Test video not found in node '%s'", self.driver_wrapper.remote_node) return self._download_video(video_url, video_name) elif server_type in ['ggr', 'selenoid']: Selenoid(self.driver_wrapper).download_session_video(video_name)
def download_videos(cls, name, test_passed=True, maintain_default=False): """Download saved videos if video is enabled or if test fails :param name: destination file name :param test_passed: True if the test has passed :param maintain_default: True if the default driver should not be closed """ # Exclude first wrapper if the driver must be reused driver_wrappers = cls.driver_wrappers[ 1:] if maintain_default else cls.driver_wrappers video_name = '{}_driver{}' if len(driver_wrappers) > 1 else '{}' video_name = video_name if test_passed else 'error_{}'.format( video_name) driver_index = 1 for driver_wrapper in driver_wrappers: if not driver_wrapper.driver: continue try: # Download video if necessary (error case or enabled video) if (not test_passed or driver_wrapper.config.getboolean_optional('Server', 'video_enabled', False)) \ and driver_wrapper.remote_node_video_enabled: if driver_wrapper.server_type in ['ggr', 'selenoid']: name = get_valid_filename( video_name.format(name, driver_index)) Selenoid(driver_wrapper).download_session_video(name) elif driver_wrapper.server_type == 'grid': # Download video from Grid Extras driver_wrapper.utils.download_remote_video( driver_wrapper.remote_node, driver_wrapper.session_id, video_name.format(name, driver_index)) except Exception as exc: # Capture exceptions to avoid errors in teardown method due to session timeouts driver_wrapper.logger.warn('Error downloading videos: %s' % exc) driver_index += 1