Capybara is working fine except I can't see it.
Both these configurations run "headless" and I can't see the browser.
Expect to see Chrome running:
Capybara.javascript_driver = :selenium_chrome
Expect headless behavior:
Capybara.javascript_driver = :selenium_chrome_headless
I'm trying to debug and I'd like to pause execution (pry) and work with Chrome in its current state. How do I make Chrome NOT headless?
Edit:
We're using RSpec.
This is the the only occurrence of current_driver in the codebase:
# spec/support/capybara_support.rb
def resize_window(width, height)
case Capybara.current_driver
when :selenium, :chrome, :selenium_chrome, :selenium_chrome_headless, :webkit
Capybara.current_session.current_window.resize_to(width, height)
else
raise NotImplementedError,
"resize_window is not supported by #{Capybara.current_driver}"
end
end
Related
I am having the issue that phantomjs doesn't render the page correctly when I am running the tests. Please see the attached picture .
Feature Helper Code:
require 'rails_helper'
require 'capybara-screenshot/rspec'
require 'capybara/dsl'
require 'capybara/poltergeist'
Dir[Rails.root.join('spec/features/support/**/*.rb')].each { |f| require f }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {
js_errors: false,
phantomjs_options: ['--ignore-ssl-errors=yes', '--ssl-protocol=any'],
debug: false,
:timeout => 120,
window_size: [1600, 1200],
# timeout: 5.minutes,
extensions: [
Rails.root.join('spec/features/support/phantomjs/disable_animations.js')
]
})
end
RSpec.configure do |config|
config.include Auth
config.include General
end
Capybara.configure do |config|
config.default_selector = :css
config.javascript_driver = :poltergeist
# config.javascript_driver = :poltergeist_debug
config.server_host = 'foo.xxx.localhost'
Capybara.server_port = 3001
config.default_max_wait_time = 10
end
The latest release versions of PhantomJS (2.1.x) are basically equivalent to a 6-7 year old version of Safari. As such they don't support a lot of modern CSS/JS. Therefore, the most likely reason for incorrect rendering is use of unsupported features in your page. If it's unsupported JS causing your issue then enabling js_errors and/or debug in your driver registration could possibly show that. If it's the use of unsupported CSS (CSS Grid for instance) then no error will be shown and the rendering will just be wrong.
If you need 100% correct rendering and aren't going to modify your app to support old browsers then you probably want to change from using Poltergeist/PhantomJS to the selenium driver with Chrome (in headless mode if wanted).
In order to work with and test a new responsive frontend for my site, I'm trying to use Rails' new system tests (specs) with javascript and Chrome headless. I cannot figure out a way to set the screen size of the browser in the spec, though.
Here's my setup in spec/rails_helper.rb
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless, screen_size: [1900, 800]
end
I then create the screenshot with:
page.driver.save_screenshot(the_uri)
But the size of the rendered screenshot still is the default, which is much smaller. Ideally, I'd like to see the entire rendered page, but at this point, I'd be happy with just using the dimensions I've provided.
Ideas on what I'm missing here?
You simply need to redefine the driver which passes the headless and screen size arguments.
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
[
"headless",
"window-size=1280x1280",
"disable-gpu" # https://developers.google.com/web/updates/2017/04/headless-chrome
].each { |arg| options.add_argument(arg) }
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
RSpec.configure do |config|
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless
end
end
Had same issue.
Changed this line
driven_by :selenium_chrome_headless, screen_size: [1400, 1400]
to this
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
to fix the issue.
I just ran into this today and I think the answer is... you can't :(
From the docs:
driven_by has a required argument for the driver name. The keyword arguments are :using for the browser and :screen_size to change the size of the browser screen. These two options are not applicable for headless drivers and will be silently ignored if passed.
Huge bummer. Testing responsive websites is going to be nearly impossible as certain elements show and hide depending on screen width.
Previously, I had Capybara 2.5, and Poltergeist page.save_screenshot worked just fine. But, recently, I've just upgraded my Capybara to 2.15.4, and that caused Poltergeist page.save_screenshot not to work anymore. The file is not created.
Here is my feature spec:
feature 'create', js: true do
before do
visit root_path
end
scenario 'valid' do
page.save_screenshot('test.png')
end
end
spec_helper.rb:
require 'capybara/poltergeist'
Capybara.register_driver :poltergeist do |app|
options = {
js_errors: false,
phantomjs_options: ['--load-images=false', '--ignore-ssl-errors=yes', '--ssl-protocol=any'],
timeout: 60,
debug: true
}
Capybara::Poltergeist::Driver.new(app, options)
end
Capybara.javascript_driver = :poltergeist
My current environment:
Ruby 2.3.3
Capybara 2.15.4
Poltergeist 1.9.0
PhantomJS 2.1.1
You've update Capybara, but not updated Poltergeist. There was an update in Capybara 2.7 where Capybara.save_and_open_page_path was deprecated in favor of Capybara.save_path with slightly different behavior around relative paths. Update Poltergeist as well and your issue will probably go away.
I have a rich frontend in my application. Some of my tests not works well with poltergeist, because of animations and AJAX requests, but works fine with selenium.
How can i use them together in one project and in one test session?
If you're using the standard RSpec configuration with Capybara (require 'capybara/rspec') then you can override the normal driver that would be used for a given test with :driver metadata
it "should do something", driver: :selenium do
# will use the selenium driver for this test
end
it "should do something else", driver: :poltergeist do
# will use the poltergeist driver for this test
end
that could also be specified on the enclosing feature if you want the whole feature to use a specific driver
feature "blah balh", driver: :selenium do
# all scenarios here would use the selenium driver unless overridden with their own :driver metadata
I found solution.
Created macros in spec/support/selenium_macros.rb:
module SeleniumMacros
def use_selenium_webdriver
before(:all) do
Capybara.javascript_driver = :selenium
Capybara.current_driver = :selenium
end
after(:all) do
Capybara.current_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
end
end
end
spec/rails_helper.rb
RSpec.configure do |config|
config.extend SeleniumMacros, type: :feature # add macros for acceptance tests
using example
spec/features/example_feature_spec.rb
feature 'Add files to question' do
use_selenium_webdriver
this feature will be work with selenium, after it will be executed it activates poltergeist webdriver.
P.S. Sorry for my english.
My setup is using poltergeist as the Capybara driver for all my tests, both JS and non-JS.
# spec/rails_helper.rb
require "capybara/poltergeist"
# ...
# ...
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_errors: true)
end
Capybara.configure do |config|
config.ignore_hidden_elements = true
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
end
I have some tests where I confirm that certain features on my app are still working even with javascript disabled. For those tests, I of course disable javascript with js: false.
describe "accessibility" do
describe "JavaScript disabled", js: false do
before(:each) { visit root_path }
it "user can still log in" do
# ...
end
end
end
However, I'm noticing that these js:false tests still use JavaScript. I can confirm this by printing debug statements to the console log in JavaScript.
Is there a way to disable JavaScript when using poltergeist? Or is it always enabled? Is it even valid to use poltergeist as a non-JS driver?
Thanks!
No, there doesn't seem to be a way to use poltergeist without Javascript (unless you modify poltergeist yourself). According to this Github issue it would require support in phantomjs, which is available in a patch but not in master.