Path: blob/trunk/rb/lib/selenium/webdriver/common/profile_helper.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21#22# @api private23#24# Common methods for Chrome::Profile and Firefox::Profile25# Includers must implement #layout_on_disk26#2728module ProfileHelper29def self.included(base)30base.extend ClassMethods31end3233def self.decoded(json)34JSON.parse(json).fetch('zip')35end3637def encoded38Zipper.zip(layout_on_disk)39end4041def as_json(*)42{'zip' => encoded}43end4445def to_json(*)46JSON.generate as_json47end4849private5051def create_tmp_copy(directory)52tmp_directory = Dir.mktmpdir('webdriver-rb-profilecopy')5354# TODO: must be a better way..55FileUtils.rm_rf tmp_directory56FileUtils.mkdir_p File.dirname(tmp_directory), mode: 0o70057FileUtils.cp_r directory, tmp_directory5859tmp_directory60end6162def verify_model(model)63return unless model6465raise Errno::ENOENT, model unless File.exist?(model)66raise Errno::ENOTDIR, model unless File.directory?(model)6768model69end7071module ClassMethods72def from_json(json)73data = decoded(json)7475Tempfile.create do |zip_path|76File.open(zip_path, 'wb') { |zip_file| zip_file << Base64.decode64(data) }7778new Zipper.unzip(zip_path)79end80end81end # ClassMethods82end # ProfileHelper83end # WebDriver84end # Selenium858687