Recently, I delved into using Capybara for testing purposes without any prior experience with Selenium. In order to assess the performance of my tests in a browser, I decided to utilize the gem selenium-webdriver
. Below is an example of one of my Capybara tests:
RSpec.feature 'Authentication', :type => :feature do
def register_with_form
visit '/'
click_on 'Account'
click_on 'Register'
fill_in 'Email', :with => '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bacedfc9cefadfc2dbd7cad6df94d9d5d7">[email protected]</a>'
fill_in 'Name', :with => 'John'
fill_in 'Last name', :with => 'Smith'
fill_in 'Password', :with => '1234567890'
fill_in 'Password confirmation', :with => '1234567890'
find('.testing-sign-up-class')
click_button 'Sign up'
expect(page).to have_css '.hd-title', 'Get Started'
end
scenario 'User registers' do
register_with_form
expect(page).to have_css '.hd-title', 'Get Started'
expect(page).to have_css '.alert-success', 'Welcome! You have signed up successfully.'
expect(page).to have_css '.panel-title', 'What would you like to use GridHub for?'
end
scenario 'User sign out', :js => true do
register_with_form
click_on 'Account'
click_on 'Logout'
expect(page).to have_css '.alert-success', 'Signed out successfully.'
</div>
Introducing :js => true
to any scenario
seems to be causing test failures. The console error message states:
1) Authentication When not registered User sign out
Failure/Error: click_button 'Sign up'
Selenium::WebDriver::Error::UnknownError:
Element is not clickable at point (606, 23.73333740234375). Other element would receive the click: <input autocomplete="off" name="term" id="term" class="form-control nav-search-field main-ac autocomplete ui-autocomplete-input" placeholder="search items" data-url="/search" type="text">
To address this issue, I included the gem database_cleaner in my spec_helper.rb
file with the following code:
config.before(:each, type: :feature) do
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
DatabaseCleaner.strategy = :truncation
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
Additionally, make sure that the bin/rspec
file has the line:
config.use_transactional_fixtures = false
Thank you.
UPDATE
I suspect that the issue may lie within the browser or other factors. There are instances where certain elements may appear below the browser window when the test opens it (I tried firefox and chrome), causing the tests to fail as the necessary button cannot be found. If time permits, scrolling down the browser's window could potentially lead to successful test outcomes.