New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Capybara with headless chrome #1860
Comments
Could you please elaborate on how I set up and run with headless chrome? I am curious to how you tested, and I'm eager to test myself |
@jesperronn The setup Capybara uses for it's headless chrome tests is https://github.com/teamcapybara/capybara/blob/master/spec/selenium_spec_chrome.rb#L6 You should just need to pass |
Hi, I'm not involved in / don't use capybara (although I've heard great things) :-) but I did run into the same issues with headless Chrome while using Selenium using Python (alerts not being supported & driver.close() causing an issue) Notes on how I resolved both:
One difference is that we used a random variable name/value (in our single page app, setting a global "alert() has been called" variable could be a false positive as the 'true' value could be read a 2nd, 3rd, etc. time) The second difference is that we ended up setting a cookie in the window.alert handler instead of setting a global variable -- the reason being, if you have an alert() call closely followed by a location.href change, the variable used to track if the alert happened or not will be lost; this won't happen with a cookie (we did use a random value for the cookie, for reasons explained above).
Cheers! |
@gregsadetsky Thanks for the info. The alert/prompt/confirm workaround was meant to be an easy solution until Chrome/chromedriver fixed the issue, however it looks like I will need to make it more robust since Chrome 59 has released with the issue still there. |
@gregsadetsky Hmmm, I still see the window errors on MacOS with Chrome 59.0.3071.86 and chromedriver 2.29.461585 so it may be fixed in linux, but it's not fully fixed. |
@gregsadetsky and on linux (travis) we're seeing a different error now - Selenium::WebDriver::Error::UnknownError: Any experience with that error? |
Unfortunately, no. It seems like that version (59.0.3071.86) will be rolling out to the stable channel (we're successfully using 59.0.3071.83 on the beta channel). Ugh. :-) |
@gregsadetsky "Ugh", yeah. Wrt your comments about random variable names, after taking a quick look at my implementation again, I don't think it applies to Capybara. I create a new "modal handler" instance every time the user tells us there is going to be an alert/prompt/confirm and then remove it from the queue when the status is checked so it's not really possible for the same status to be used multiple times. The issue with a page change is valid, but swapping to cookies could also have an issue if cookies are cleared during the page change (or the new url is a different sub/domain) so I think I'll stay with the small risk of a failure if a page change occurs, for now, and hope chrome/chromedriver fix the issue soon. |
Here is the new chromedriver 2.30. It doesn't have release notes yet. |
Note that window sizing and positioning do not work with headless as of chromedriver 2.30 + Chrome 59. From this chromedriver issue:
If you're doing something like: driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(1400, 1400) Then you'll get an error like:
Comment out the window size/position setting to work around it. |
chromedriver 2.30 fixed the issues around window closing, but all content in extra windows opened is reported as not displayed by selenium, so multiple windows are still not really usable with headless. |
capybara/lib/capybara/selenium/driver.rb Line 311 in 3879c8c
This line makes a lot of assumptions about the hash structure of the Capabilities object. I'm on a project where we have this driver defined: Capybara.register_driver :chrome_headless do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
args: %w[ no-sandbox headless disable-gpu ]
}
)
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)
end And But Chrome does launch headlessly so it's clear that the browser launcher is being more lenient in its hash parsing. |
@nertzy Yes it does, and if you'd like to propose a clean way of detecting it a PR would be appreciated. The fact that we even have to care whether it's headless or not is a hack at the moment, and hopefully modals and window interactions will actually be supported by Chrome in the near future, so we don't have to care. |
|
@iggant That would be a Chrome issue, not a Capybara issue. |
This one, maybe: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1772 |
@deivid-rodriguez Exactly that one :) |
Looks like this has been fixed and is just waiting for a new chromedriver release. |
I'm able to run it and wrote a blog post about it: How to run your feature specs using Capybara and Headless Chrome |
@lucascaton You've fixed what? Without the next release of chromedriver (2.31) it's not possible to run without an X server installed on linux, anything to do with multiple windows or window resizing is still pretty broken until a future release of chrome and/or chromedriver, and we're still hacking around the lack of JS modal support. |
@twalpole I've been using it with chromedriver 2.30 and works perfectly, even on Circle CI, running the same version 😄 |
@lucascaton Yes, because Circle CI installs an X server, your tests aren't resizing windows or opening multiple windows, and Capybara is hacking around the JS modals. So it's working perfectly for you because you're not using any of the currently broken parts. That's not fixing things, that's just avoiding the cracks :) Capybara has been running its own tests with headless chrome on travis for a few weeks now, and as long as we skip all the broken tests then it's perfect. |
We are using Chromedriver 2.30 and the only issue we are facing is the resizing. Since our test suite does lot of resizing ( desktop, mobile, tablet sizes ) in a single feature spec, we are currently blocked in using headless feature. |
@himankarbn Since there is no connection to send random DevTools commands over I believe this isn't possible to do at the moment. Basically, it's a waiting game until chromedriver/chrome implement/fix support. |
If you need to resize just once you can set a flag for the window size instead of resizing the window:
|
we have sacrificed alerts functionality by disabling them with the following code, that is injected during tests: window.alert = function() { return true; }
window.confirm = function() { return true; } for everything else headless chrome works well. |
Will try upgrading to Chrome 60... |
@bbuchalter What version of seleniuv-webdriver are you using (make sure a recent one) and what are you specifying in your register driver block? Also show the code you're using with |
Will update |
@bbuchalter and the code that calls |
OK, after upgrading all the things (versions below), I now get failures for both headed and headless versions, but different errors. Headed Chrome alert error
Headless Chrome alert error
I know the docs for Versions
|
🤦 I failed to understand the way the block is supposed to work:
My apologies. Things working as expected now. |
So I am having another, yet similar issue. For me, in headless mode, it appears that js alerts are not even being rendered. If I run the code:
In headed chrome, it passes, because the modal is rendered. In headless, it fails with Has anyone else had the same problem, or have any ideas? Version Info:
|
@NoHesHere Chrome in headless mode doesn't support system modals, so Capybara has to patch in some code to handle them. Unfortunately there is no nice way to detect that Chrome is in fact running in headless mode through selenium so we have to inspect the driver config to determine when we need to patch window.alert/confirm/prompt. The downside to this is there are many ways to configure selenium so it runs chrome headless, if you're specifying it in a way we don't detect then the patching doesn't occur. Look at https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selenium/driver.rb#L322 and see whether the way you're configuring selenium would match that. |
@twalpole You are correct, we were not tripping the conditional. For anyone else with the same issue, we had defined our
Changing it to:
fixed the issue for us. |
Found out that rails 5.1 can use headless chrome with a one liner:
Gives only a 'small' deprecation warning ('args' vs 'add_argument'). |
I still get the error Version info:
|
@rachel-carvalho Then you’re either using it incorrectly or you’re running into the same issue @NoHesHere had a couple of posts up |
@rachel-carvalho @NoHesHere This works:
This doesn't
|
@Petercopter auto-accepting alerts shouldn't work -- the fact that is does in some drivers/setups is technically a bug - you should be specifying to accept or dismiss system modals. Assuming by "auto-accept" that you mean without using code like
If that's not what you mean then please provide an example of the code that is failing for you and the exact error message returned. |
@twapole sorry for the confusion. I meant using the workaround code to auto accept the confirmations in the meantime, while we wait for chrome to work with the dialogs. I'm busy converting from poltergeist to headless chrome, I was just trying to work past the alert problem for now. Nothing to see here, carry on! 😀 |
@Petercopter - using the default :selenium_chrome_headless it should work correctly with code like
is that working for you? If not, please post your code so I can try and figure out why - we don't like having things not work when using the Capybara provided defaults :). If you've modified the :selenium_chrome_headless registration then it's possible Capybara isn't detecting that it's headless (due to the specific way you have specified headless) and isn't patching the JS alert/confirm/prompt methods as needed. |
@twalpole Alright, I feel ridiculous. It turns out that we were not using the Capybara I've changed to using |
@Petercopter No problem, glad it's working (just to note - if it is actually a |
I figured what the problem was, thanks @Petercopter! My tests were like so: click button 'Will confirm'
accept_alert And that was working with But obviously my problem was that the action that was causing the confirm to appear happened before accepting, but not within a block. Anyway, now it's all working 🎉 |
Shouldn’t we put out a new release that raises if you call accept_alert without a block? Happy to do a PR if so, if only so I stop seeing the same question and resolution pop up on this thread...
… On 29 Sep 2017, at 22:00, Rachel Carvalho ***@***.***> wrote:
I figured what the problem was, thanks @Petercopter! My tests were like so:
click button 'Will confirm'
accept_alert
And that was working with :selenium_chrome and firefox before that. It seems that using accept_alert or accept_confirm doesn't make much of a difference, but I changed it to accept_confirm anyway because it's the right type of dialog.
But obviously my problem was that the action that was causing the confirm to appear happened before accepting, but not within a block. Anyway, now it's all working 🎉
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@jdelStrother It's already in master - ec4d32f - it has been a while since a release so I'll see if I can get to one this weekend, just need to decide if it can be 2.15.2 or needs to be 2.16 |
@maschwenk Have you managed to figure out why hover wasn't working? Seems like the question got lost in this thread. For me it's working in regular chrome, but not in headless. |
I figured you need to click anything on the page before hovering. Maybe, otherwise the window is not active. |
There are currently 2 issues with using Capybara with headless chrome -
(Session info: headless chrome=60.0.3080.5)
(Driver info: chromedriver=2.29.461585
Headless chrome appears not to support JS system modals ( alert, confirm, prompt)
There is a workaround for this currently in testing
Attempting to close a window raises a timeout error "failed to close window in 20 seconds" and doesn't close the window
I can't think of any way to work around this issue, so window management won't really work until this is fixed in either chromedriver or chrome.
The text was updated successfully, but these errors were encountered: