首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载位置Selenium-webdriver

下载位置Selenium-webdriver
EN

Stack Overflow用户
提问于 2016-05-23 13:08:11
回答 3查看 3.1K关注 0票数 8

我在用黄瓜和露比。当通过Selenium-Webdriver在Chrome中运行测试时,我希望将下载位置更改为测试文件夹,而不是用户下载文件夹。

我当前的铬驱动程序设置如下:

代码语言:javascript
复制
Capybara.default_driver = :selenium
 Capybara.register_driver :selenium do |app|
    Capybara::Selenium::Driver.new(app, :browser => :chrome,
      desired_capabilities: {
      'chromeOptions' => {
      'args' => %w{ window-size=1920,1080 }

     }
    }
   )
  end

我需要添加哪些内容才能更改下载位置?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-23 13:28:04

可以使用download.default_directory首选项设置下载目录:

代码语言:javascript
复制
require 'capybara'
require 'selenium-webdriver'

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app,
    :browser => :chrome,
    :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
      'chromeOptions' => {
        'args' => [ "--window-size=1920,1080" ],
        'prefs' => {
          'download.default_directory' => File.expand_path("C:\\Download"),
          'download.prompt_for_download' => false,
          'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
        }
      }
    )
  )
end

session = Capybara::Session.new(:chrome)
票数 16
EN

Stack Overflow用户

发布于 2020-06-29 05:32:14

我最近遇到了这个问题,由于我的设置不同,所以无法得到先前的答案。我有以下设置:

test_helper.rb

代码语言:javascript
复制
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/reporters'
MiniTest::Reporters.use!

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

application_system_test_case.rb

代码语言:javascript
复制
require 'test_helper'
require 'capybara/rails'
require 'capybara/poltergeist'
require 'fileutils'
require 'selenium-webdriver'

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  #driven_by :poltergeist, options: { js_errors: false } #uncomment if you want to run headless
  self.use_instantiated_fixtures = true
  @downloads = File.expand_path(Rails.root + 'tmp/downloads')
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: {prefs:{
      'download.default_directory' => @downloads,
      'download.prompt_for_download' => false,
      'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
  } }
end

一个典型的测试: admin_page_test.rb

代码语言:javascript
复制
require 'application_system_test_case'
include ApplicationHelper

class AdminPageTest < ApplicationSystemTestCase

  setup do
    visit new_user_session_url
    fill_in 'Email', with: 'whatever@whatever.com'
    fill_in 'Password', with: 'password'
    click_on 'commit'
    assert_selector 'h1', text: 'Admin Status Board'
  end

我到处寻找在课堂上传递选项的正确方法,并最终无意中发现了它,阅读了Capybara模块,并尝试了错误。我想我在不同的地方看过近百篇文章,但没有一篇是有效的。希望这能帮助那些偶然发现它的人。

票数 0
EN

Stack Overflow用户

发布于 2020-09-11 15:49:59

由于我的Chrome驱动程序是用选项而不是功能设置的,所以我最终选择了这样的路线:

代码语言:javascript
复制
Capybara.register_driver :selenium_chrome_headless do |app|
  Capybara::Selenium::Driver.load_selenium
  browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
    opts.args << "--headless"
  end

  browser_options.add_preference(:download, { prompt_for_download: false, default_directory: DOWNLOAD_PATH })
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end

受到这个要旨的启发

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37391879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档