Enabling Capybara's :js option can lead to test failures

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.

Answer №1

When working with JS-capable drivers, the click_button function simply clicks the button and moves on without waiting for any changes on the page to occur. This lack of anticipation can lead to issues if not managed properly. It is possible that your test scenario involving the registration process abruptly proceeds to clicking the 'Account' link on the same page as the form, causing the sign-up submission to be cancelled and redirecting to a page without a Logout option. In order to address this, consider modifying the final line of the register_with_form method to include an expectation such as

expect(page).to have_content('Welcome!')
, ensuring the completion of the signup process before proceeding.

Furthermore, it is advisable to implement the recommended database_cleaner setup as outlined in https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example (notably the use of append_after) instead of alternative configurations to avoid potential complications where database cleaning occurs prematurely before requests are fully processed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to receive data in an array with Vuejs

Here is the code snippet I am working with: let data = res.data.data; console.log('data: ', data) const list = []; for(let i = 0; i < data.length; i++){ console.log('data i: ', data[i]) //this line is not being printed in the ...

The initial function within node-lambda is executed only when using node async

When utilizing a simple vanilla node script like the one shown below, the node async function performs perfectly: async = require('async'); async.waterfall([ function (c) { console.log(1); c(null); }, functio ...

Troubles with retrieving Array data for a JavaScript column chart

I am currently developing a Flask app in Python and utilizing render_template to send back 2 arrays, "names" and "deals", to my HTML file. I have confirmed that these arrays are working correctly based on the code snippet below that I tested, which display ...

Use JavaScript to find any text enclosed within [quote] and [/quote] tags and then replace it with <div class="quote-text">foo text</div>

Hey there! I have a specific text format that looks like this: [quote]foo text[/quote] and I would like to convert it to the following: <div class="quote-text">foo text</div> Do you have any idea how I can achieve this using JavaScript? Cu ...

Using an AngularJS ng-repeat alias expression with multiple filters

As stated in the Angular ngRepeat documentation, the alias expression can only be used at the end of the ngRepeat: It's important to note that `as [variable name]` is not an operator, but rather a part of the ngRepeat micro-syntax and must be place ...

Optimizing performance: Making the most of mongoose updateMany middleware

PROBLEM SOLVED: SOLUTION PROVIDED BELOW I have a piece of code where I am updating elements one by one: //registerCustomers.js const CustomerRegistrationCode = require("../models/CustomerRegistrationCode"); const setRegCodesToUsed = async (regC ...

Achieve a sticky y-axis while scrolling horizontally with chartJS and Angular

Is there a way to keep the y-axis fixed while scrolling horizontally? Below is an example that achieves this functionality without Angular: $(document).ready(function () { function generateLabels() { var chartLabels = []; for (x = ...

Having trouble integrating Backbone-relational with AMD (RequireJS)?

I'm struggling with my Backbone router and module definitions in CoffeeScript. Can someone help me troubleshoot? // appointments_router.js.coffee define ["app", "appointment"], (App) -> class Snip.Routers.AppointmentsRouter extends Backbone.Rout ...

Having difficulty applying capitalization to the initial word in an input using JavaScript/jQuery

I've read through several other discussions on this forum regarding the same issue. My problem lies in capitalizing the first letter of an input. Here is the link to my code on JSFiddle Despite my efforts, I can't seem to get the substr() funct ...

Tips for receiving live updates of active users online with the help of Mongoose and socket.io

I have been working on developing an app that covers various topics. Each topic has online users, and I am using express/socket.io/mongoose for the backend and flutter for the frontend. Currently, I am facing a challenge in displaying real-time informatio ...

Exploring the Depths of Multidimensional JSON Arrays in PHP

I am currently working on developing a web-based file manager that allows me to organize, view, create, edit, and delete folders and files. In order to store information about these folders, files, and subfolders, I am in need of an appropriate data struct ...

What is the process for transferring a web project to my local server?

I'm having trouble figuring out how to connect my web application to the server. I attempted to use the following code: res.sendFile(path.resolve('./public/index.html')); However, it seems to be not linking the components that are written i ...

Issue: EACCES error encountered while attempting to execute bower install operation

After running bower install css-circle-menu, I encountered the following error: /usr/local/lib/node_modules/bower/lib/node_modules/configstore/index.js:54 throw err; ^ Error: EACCES: permission denied, open '/Users/ja ...

Spinner remains visible even after a successful execution of jQuery ajax request

After successfully making an ajax request and saving data in the database, I am facing a problem when trying to remove the active class from the button. Upon clicking, a spinner is displayed, but it never stops even after the ajax call finishes. To debug t ...

Adjusting the inline styling of an element to enhance its appearance

Currently, the code is functioning as follows: var ball = document.getElementById('ball'); //some code let bml = parseInt(ball.style.marginLeft,10); //if *conditional statement* is true, do: ball.setAttribute("style","margin-left:& ...

What is the best way to insert a div element within another div element using dynamic

I need to cycle through the products and display them all within a div based on the data I receive in the API response. Here's the div where I want to place my product div: <div class="row" id=""> <div ...

What is preventing the return type of document.createElement from being designated to a specific generic that is limited to HTMLElement?

Currently, I am in the process of developing a function called clone(el). This function needs to accept an HTMLElement or any subtype of it and return the same type that is provided. Can someone assist me in correctly structuring the generic signature for ...

Automated file uploading with AJAX on Firefox

After inserting a row into the database, I want to automatically upload a PDF/XML file with no user involvement. The script should be able to identify the filename and location of the file. Is there a method to achieve this task? Share your ideas in the a ...

Reverse a filter within an ng-repeat loop - AngularJS

I have a question that I haven't found the answer to yet. Here is the issue I'm facing: The JSON data I am working with has this structure: [{ "batch_queue_name": "Batch One", "start_date": "12/01/2016 10:18 P.M.", "end_date": "12/03/2016 ...

Integrating the frontend (HTML, CSS, and JavaScript) with the backend (Express server)

I've been trying to figure out the best way to connect the frontend and backend of a website, but I can't seem to find any clear answers online. Let's say you have a backend API with an endpoint that deletes a user's account when req ...