def test_create_existing_folder(): folder = os.path.join('output', str(uuid.uuid4())) os.makedirs(folder) makedirs_safe(folder) assert os.path.isdir(folder) os.rmdir(folder)
def retrieve_remote_downloaded_file(context, filename, destination_filename=None): """ Retrieves a file downloaded in a remote node and saves it in the output folder :param context: behave context :param filename: downloaded file :param destination_filename: local destination name :returns: destination file path """ destination_filepath = None if context.download_directory is not None and context.driver_wrapper.config.getboolean_optional( 'Server', 'enabled'): url = _get_download_directory_url(context) file_url = '%s/%s' % (url, filename) destination_folder = os.path.join(DriverWrappersPool.output_directory, DOWNLOADS_FOLDER, context.download_directory) makedirs_safe(destination_folder) destination_filename = destination_filename if destination_filename else filename destination_filepath = os.path.join(destination_folder, destination_filename) context.logger.info('Retrieving downloaded file from "%s" to "%s"', file_url, destination_filepath) urlretrieve(file_url.encode('utf-8').decode(), destination_filepath) return destination_filepath
def get_download_directory_base(context): """ Get base folder to download files :param context: behave context :returns: base folder """ if context.download_directory is None: base = None elif context.driver_wrapper.config.getboolean_optional( 'Server', 'enabled'): try: platform = context.driver_wrapper.config.get('Driver', 'type').split('-')[3] except IndexError: platform = 'linux' if platform.lower().startswith('win'): # Windows node base = 'C:\\tmp\\%s\\' % DOWNLOADS_FOLDER else: # Linux or Mac node base = '/tmp/%s/' % DOWNLOADS_FOLDER else: # Local folder destination_folder = os.path.join(DriverWrappersPool.output_directory, DOWNLOADS_FOLDER, '') makedirs_safe(destination_folder) base = str(destination_folder) return base
def __download_file(self, url, path_file, timeout): """ download a file from the server using a request with retries policy :param url: server url to request :param path_file: path and file where to download :param timeout: threshold until the video file is downloaded :return boolean """ status_code = 0 init_time = time.time() self.driver_wrapper.logger.info('Downloading file from Selenoid node: %s' % url) # retries policy while status_code != STATUS_OK and time.time() - init_time < float(timeout): body = requests.get(url) status_code = body.status_code if status_code != STATUS_OK: time.sleep(1) took = time.time() - init_time # time used to download the file # create the folders and store the file downloaded if status_code == STATUS_OK: path, name = os.path.split(path_file) makedirs_safe(path) try: fp = open(path_file, 'wb') fp.write(body.content) fp.close() self.driver_wrapper.logger.info('File has been downloaded successfully to "%s" and took %d ' 'seconds' % (path_file, took)) return True except IOError as e: self.driver_wrapper.logger.warn('Error writing downloaded file in "%s":\n %s' % (path_file, e)) else: self.driver_wrapper.logger.warn('File "%s" does not exist in the server after %s seconds' % (url, timeout)) return False
def __init__(self, driver_wrapper=None, force=False): self.driver_wrapper = driver_wrapper if driver_wrapper else DriverWrappersPool.get_default_wrapper( ) self.force = force if not self.driver_wrapper.config.getboolean_optional( 'VisualTests', 'enabled') and not self.force: return if 'PerceptualEngine' not in globals(): raise Exception( 'The visual tests are enabled, but needle is not installed') self.utils = self.driver_wrapper.utils self.logger = logging.getLogger(__name__) self.output_directory = DriverWrappersPool.visual_output_directory # Update baseline with real platformVersion value if '{platformVersion}' in self.driver_wrapper.baseline_name: platform_version = self.driver_wrapper.driver.desired_capabilities[ 'platformVersion'] baseline_name = self.driver_wrapper.baseline_name.replace( '{platformVersion}', platform_version) self.driver_wrapper.baseline_name = baseline_name self.driver_wrapper.visual_baseline_directory = os.path.join( DriverWrappersPool.visual_baseline_directory, get_valid_filename(baseline_name)) self.baseline_directory = self.driver_wrapper.visual_baseline_directory self.engine = self._get_engine() self.save_baseline = self.driver_wrapper.config.getboolean_optional( 'VisualTests', 'save') # Create folders makedirs_safe(self.baseline_directory) makedirs_safe(self.output_directory) # Copy js, css and html template to output directory dst_template_path = os.path.join(self.output_directory, self.report_name) if not os.path.exists(dst_template_path): resources_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'resources') orig_template_path = os.path.join(resources_path, self.template_name) orig_javascript_path = os.path.join(resources_path, self.javascript_name) dst_javascript_path = os.path.join(self.output_directory, self.javascript_name) orig_css_path = os.path.join(resources_path, self.css_name) dst_css_path = os.path.join(self.output_directory, self.css_name) shutil.copyfile(orig_template_path, dst_template_path) shutil.copyfile(orig_javascript_path, dst_javascript_path) shutil.copyfile(orig_css_path, dst_css_path) self._add_summary_to_report()
def _download_video(self, video_url, video_name): """Download a video from the remote node :param video_url: video url :param video_name: video name """ filename = '{0:0=2d}_{1}'.format(DriverWrappersPool.videos_number, video_name) filename = '{}.mp4'.format(get_valid_filename(filename)) filepath = os.path.join(DriverWrappersPool.videos_directory, filename) makedirs_safe(DriverWrappersPool.videos_directory) response = requests.get(video_url) open(filepath, 'wb').write(response.content) self.logger.info("Video saved in '%s'", filepath) DriverWrappersPool.videos_number += 1
def capture_screenshot(self, name): """Capture screenshot and save it in screenshots folder :param name: screenshot name suffix :returns: screenshot path """ filename = '{0:0=2d}_{1}'.format(DriverWrappersPool.screenshots_number, name) filename = '{}.png'.format(get_valid_filename(filename)) filepath = os.path.join(DriverWrappersPool.screenshots_directory, filename) makedirs_safe(DriverWrappersPool.screenshots_directory) if self.driver_wrapper.driver.get_screenshot_as_file(filepath): self.logger.info('Screenshot saved in %s', filepath) DriverWrappersPool.screenshots_number += 1 return filepath return None
def configure_common_directories(cls, tc_config_files): """Configure common config and output folders for all tests :param tc_config_files: test case specific config files """ if cls.config_directory is None: # Get config directory from properties config_directory = cls.get_configured_value( 'Config_directory', tc_config_files.config_directory, 'conf') prop_filenames = cls.get_configured_value( 'Config_prop_filenames', tc_config_files.config_properties_filenames, 'properties.cfg') cls.config_directory = cls._find_parent_directory( config_directory, prop_filenames.split(';')[0]) # Get output directory from properties and create it cls.output_directory = cls.get_configured_value( 'Output_directory', tc_config_files.output_directory, 'output') if not os.path.isabs(cls.output_directory): # If output directory is relative, we use the same path as config directory cls.output_directory = os.path.join( os.path.dirname(cls.config_directory), cls.output_directory) makedirs_safe(cls.output_directory) # Get visual baseline directory from properties default_baseline = os.path.join(cls.output_directory, 'visualtests', 'baseline') cls.visual_baseline_directory = cls.get_configured_value( 'Visual_baseline_directory', tc_config_files.visual_baseline_directory, default_baseline) if not os.path.isabs(cls.visual_baseline_directory): # If baseline directory is relative, we use the same path as config directory cls.visual_baseline_directory = os.path.join( os.path.dirname(cls.config_directory), cls.visual_baseline_directory)
def run_makedirs(folder, exceptions): try: makedirs_safe(folder) except Exception as exc: exceptions.put(exc)