Selenium::WebDriver::Error::ElementNotVisibleError Exception: element not visible - ruby-on-rails

Selenium::WebDriver::Error::ElementNotVisibleError Exception: element
not visible
I am trying to access an element in my cucumber with headless chrome.and in my .env.rb I have this,
Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new(
args: %w[headless disable-gpu no-sandbox]
)
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.javascript_driver = :chrome
and in my set definition, I have
Then(/^I should see the "([^"]*)" link on the page$/) do |link_text|
expect(page).to have_css("a", text: link_text)
end
but the element is not visible on my test if I run it without headless chrome mode the element is visible. Can anyoin point me on the right direction? Thanks in advance

Related

How to fix "Selenium::WebDriver::Error::WebDriverError: chrome not reachable" in WSL2

I am trying to use WSL2 to rails application and had this error when RSpec is running:
Selenium::WebDriver::Error::WebDriverError:
chrome not reachable
(Session info: headless chrome=77.0.3865.75)
Someone know to fix it?
My capybara.rb:
require "capybara/rails"
require "selenium-webdriver"
Capybara.default_max_wait_time = 10
Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new(
args: %w[headless no-sandbox]
)
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.javascript_driver = :chrome
Capybara.server = :puma, { Silent: true }
I put the path in env variables to reach my chromewebdriver binary, following this article "https://ngauthier.com/2017/09/rails-system-tests-with-headless-chrome-on-windows-bash-wsl.html" and nothing!
I just fix it with the following code
Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[
headless no-sandbox disable-gpu window-size=1920x1080
])
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: {
"chromeOptions" => {
w3c: false
}
}
)
end
Please let me know if this is resolve your issue

Selenium_chrome not visible; always headless

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

Rspec and Chrome/headless flag

I've got headless Chrome and Chrome working for my Rspec tests. I want a flag to switch between the two so I can see the tests happen when I want and hide them when I don't. How can I implement something like:
rspec --headless
Right now I just have this secret tied to a .env var:
Capybara.javascript_driver = Rails.application.secrets.headless ? :headless_chrome : :chrome
in your rails_helper.rb you should create a statement like that:
RSpec.configure do |config|
config.before(:each) do
if ENV['HEADLESS'] == 'true'
Capybara.current_driver = :selenium_chrome_headless
else
Capybara.current_driver = :selenium_chrome
end
end
end
then send a variable when running specs
$ HEADLESS=true rspec ./spec/features
Well, overriding the env var works so that's something.
HEADLESS=true rspec
Hmm, I personally use this code to register driver:
Capybara.register_driver :chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
args: [
('headless' if ENV.fetch('HEADLESS', '1') == '1')
].compact
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: capabilities
)
end
then in .env you can set the variable to be HEADLESS or not by default and then if you want to overwrite it just type HEADLESS=0 rspec

capybara chrome headless confirm dialog

I'm trying to use headless mode of chrome with capybara/selenium in rspec Ruby on Rails and getting a error when try to click on confirm dialog
Capybara.register_driver(:headless_chrome) do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w[headless disable-gpu test-type window-size=1920x1080] }
)
driver = Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: capabilities
)
driver
end
Capybara.server = :puma
Capybara.javascript_driver = :headless_chrome
scenario 'delete movie' do
login_as user
visit edit_public_movie_path(movie)
expect(page).to have_selector('span[ng-click="ctrl.deleteMovie()"]')
find('span[ng-click="ctrl.deleteMovie()"]').click
page.driver.browser.switch_to.alert.accept
wait_for_ajax(wait_after: 1)
expect(page).to have_content('Click here to upload movie (Max: 500 MB)')
end
And got this error
Event movie success delete movie
Failure/Error: page.driver.browser.switch_to.alert.accept
Selenium::WebDriver::Error::NoSuchAlertError:
no alert open
(Session info: headless chrome=59.0.3071.115)
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.5 x86_64)
With common mode it works without errors. Seems the selenium don't support headless mode or need to other approach to do it. Any ideas?
Update(resolved)
So, I spent a lot of time and find working environments.
You need to last build of chromium because preview versions have a bug. You can get it by these scripts linux or Mac
Here is my capybara config
Capybara.register_driver(:headless_chrome) do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
args: %w[headless disable-gpu disable-popup-blocking no-sandbox window-size=1920x1920],
binary: "#{Dir.home}/chromium-latest-#{platrofm}/latest/#{chrome_file}"
})
driver = Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: capabilities)
driver
end
Capybara.server = :puma
Capybara.javascript_driver = :headless_chrome
Capybara.default_max_wait_time = 20
Capybara.server_port = 55305
def platform
if /linux/ =~ RUBY_PLATFORM
"linux"
else
"macosx"
end
end
def platform_linux?
platform == "linux"
end
def chrome_file
if platform_linux?
"chrome"
else
"Chromium.app/Contents/MacOS/Chromium"
end
end
And you should use page.driver.browser.switch_to.alert.accept instead of page.accept_alert
Had the same problem. Please use the disable-popup-blocking in chromeOptions.

Capybara cannot find element without js:true

Why does Capybara not find an element if I remove js: true (because it's very slow) from my test?
My test:
require 'spec_helper'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
feature 'signal trading platform', integration: true do
scenario 'check context' do
visit '/'
find('a', text: 'SIGNAL TRADER').click
end
end
=>
Capybara::ElementNotFound:
Unable to find css "a" with text
Removing the js: true metadata means (most likely) your test is running with the rack_test driver. The rack_test driver doesn't process JS at all and doesn't process most CSS. Therefore if the test worked before you removed the js: true metadata, either your page loading is dependent on javascript or the text was being affected by CSS which is no longer processed ( text-transform, etc). Without seeing the actual HTML it's impossible to give a more specific answer.

Resources