【发布时间】:2014-08-06 11:04:05
【问题描述】:
我正在使用 Ruby,并且正在使用 Watir 的最新 gem 版本。我打算使用无头浏览器,例如 PhantomJS。执行时如何将参数传递给 Phantom JS 浏览器。
我的目的是让 Phantom JS 不需要加载图片。
【问题讨论】:
我正在使用 Ruby,并且正在使用 Watir 的最新 gem 版本。我打算使用无头浏览器,例如 PhantomJS。执行时如何将参数传递给 Phantom JS 浏览器。
我的目的是让 Phantom JS 不需要加载图片。
【问题讨论】:
如PhantomJS command line page 所述,有一个选项可以控制图像的加载:
--load-images=[true|false] 加载所有内联图片(默认为true)。也接受:[yes|no]。
在 Watir::Browser 的初始化过程中,可以在 :args 键中以数组的形式指定这些设置。例如:
args = %w{--load-images=false}
browser = Watir::Browser.new(:phantomjs, :args => args)
一个工作示例:
require 'watir-webdriver'
# Capture screenshot with --load-images=true
browser = Watir::Browser.new(:phantomjs)
browser.goto 'https://www.google.com'
browser.screenshot.save('phantomjs_with_images.png')
# Capture screenshot with --load-images=false
args = %w{--load-images=false}
browser = Watir::Browser.new(:phantomjs, :args => args)
browser.goto 'https://www.google.com'
browser.screenshot.save('phantomjs_without_images.png')
这给出了加载图像的屏幕截图:
还有没有加载图片的截图:
请注意,页面的 Google 图像未在第二个屏幕截图中加载(正如预期的那样,因为 load-images 设置为 false)。
【讨论】:
phantomjs 作为带有 args 的测试时,它可以工作,但在 watir/ruby 中它只是没有任何区别。几个小时以来一直在尝试和谷歌搜索,现在找不到这是如何完成的......