使用Watir-Webdriver / Selenium在Firefox中指定自定义用户数据目录

问题描述 投票:3回答:4

我可以使用Chrome驱动程序执行以下操作:

b = Watir::Browser.new :chrome, :switches => ['--user-data-dir=C:/some_folder/'] # same philosophy for selenium, just a bit of a different syntax.

这将创建一个新的user data directory,其中将存储所有的cookie,书签,缓存等。基本上,创建一个新的配置文件。如果这样的文件夹不存在,它将创建它。如果确实存在,它将从中加载cookie /所有相关文件。

有没有办法使用Firefox驱动程序做同样的事情?我一直在研究创建Firefox配置文件的方法,我发现的只有这篇文章:Creating a new Firefox Profile不能解决我的问题,因为我希望自动执行,就像上面的Chrome驱动程序一样。此外,您似乎可以创建一个新的配置文件:

profile = Selenium::WebDriver::Firefox::Profile.new

但我还没有找到一种方法来保存具有我指定名称的配置文件。

ruby firefox selenium selenium-webdriver watir-webdriver
4个回答
2
投票

基于Selenium Issue 1954Issue 7374,Firefox驱动程序目前没有此功能。鉴于某些项目成员反对这一想法,很难判断它是否会实施。

目前,我认为您必须对Selenium-WebDiver版本进行补丁以添加此功能。我得到了与@ shri046的答案相同的结论,即修改layout_on_disk方法。

在您需要Selenium-WebDriver之后,为Selenium :: WebDriver :: FirefoxProfile添加以下猴子补丁。这个补丁中的逻辑是:

  • 如果未指定配置文件目录,请使用正常/现有行为,这意味着复制现有配置文件并在退出时将其删除。
  • 如果指定了配置文件目录: 如果该目录存在,请将该目录用于配置文件。假设该目录是有效的配置文件(即我没有添加检查以确保它是有效的)。 如果该目录不存在,将创建现有配置文件的副本(即正常行为)。然后,此配置文件将移动到指定的目录。退出时不会删除配置文件,因此可以在以后重新使用。

补丁:

require 'watir-webdriver'
require 'selenium-webdriver'

module Selenium
  module WebDriver
    module Firefox
      class Profile
        class << self
          attr_accessor :webdriver_profile_directory
        end

        def layout_on_disk
          # When a directory is specified, ensure it is not deleted at exit
          if Profile.webdriver_profile_directory
            FileReaper.reap = false
          end

          # Use the specified directory if it already exists (ie assuming an existing profile)
          if Profile.webdriver_profile_directory && Dir.exists?(Profile.webdriver_profile_directory)
            return Profile.webdriver_profile_directory
          end

          # Create the profile directory as usual when it does not exist
          profile_dir = @model ? create_tmp_copy(@model) : Dir.mktmpdir("webdriver-profile")
          FileReaper << profile_dir

          install_extensions(profile_dir)
          delete_lock_files(profile_dir)
          delete_extensions_cache(profile_dir)
          update_user_prefs_in(profile_dir)

          # If a directory is specified, move the created profile to that directory
          if Profile.webdriver_profile_directory
            FileUtils.cp_r(profile_dir, Profile.webdriver_profile_directory)
            profile_dir = Profile.webdriver_profile_directory
          end

          profile_dir
        end
      end # Profile
    end # Firefox
  end # WebDriver
end # Selenium

要指定要使用的配置文件位置,请执行以下操作:

Selenium::WebDriver::Firefox::Profile.webdriver_profile_directory = 'C:/temp/test-profile'
browser = Watir::Browser.new :firefox

此补丁可能有局限性和未经测试的区域。例如,它可能无法处理创建多个Firefox实例。但是,对于单个实例,它至少似乎与重新加载创建的书签(这是我的测试的限制)一起工作。


0
投票

看看source code for Firefox profile,似乎有一种方法可以让你将新创建的配置文件写入磁盘

  def layout_on_disk
    profile_dir = @model ? create_tmp_copy(@model) : Dir.mktmpdir("webdriver-profile")
    FileReaper << profile_dir

    install_extensions(profile_dir)
    delete_lock_files(profile_dir)
    delete_extensions_cache(profile_dir)
    update_user_prefs_in(profile_dir)

    profile_dir
  end

现在没有太多关于Ruby实现的文档,但是这种方法的Java等价物有这些细节

 /**
  * Call this to cause the current profile to be written to disk. The profile directory is
  * returned. Note that this profile directory is a temporary one and will be deleted when the JVM
  * exists (at the latest)
  * 
  * This method should be called immediately before starting to use the profile and should only be
  * called once per instance of the {@link org.openqa.selenium.firefox.FirefoxDriver}.
  * 
  * @return The directory containing the profile.
  */

在这里完成所有这些工作是对必须完成的工作的概述

  1. webdriver.reap_profile system property设为false。这将阻止清理创建的临时Firefox配置文件。
  2. 创建一个新的Firefox配置文件,调用layoutOnDisk()方法将配置文件写入磁盘。
  3. 从上述步骤返回的文件路径将存储在所有测试的公共变量中。
  4. 使用新创建的配置文件启动WebDriver。

我没有试图实现这个并测试它,所以这可能不是最准确或可行的解决方案 - 如果它确实有效。这取决于您的用例,为什么您希望在所有测试中重复使用相同的配置文件。希望这可以帮助。


0
投票

这是为firefox 60+和新的marionette/geckodriver

在摆弄了很多东西后,这就是我使用自定义配置文件的方法:

$ xvfb-run firefox -CreateProfile test
options = Selenium::WebDriver::Firefox::Options.new # no profile specified here
options.add_argument "--profile"
options.add_argument "/home/jenkins/.mozilla/firefox/f0jecsmr.test"
@browser = Watir::Browser.new :firefox, options: options, driver_opts: {marionette_port: 2828}, **capabilities

它接近于documentation建议如何处理python。虽然(或者我没有找到相关文档),但是阅读了源代码来找出这些driver_opts

HTH有人。


-2
投票

您可以使用以下命令创建Firefox配置文件

profile = Selenium::WebDriver::Firefox::Profile.new
b = Watir::Browser.new :firefox, :profile => profile`
© www.soinside.com 2019 - 2024. All rights reserved.