Running several Angular requests simultaneously can trigger failures in test cases

I am facing an issue with my Angular 1.5 application and Rails 4.0 backend. During testing, when a Staff member logs in, three queries are simultaneously sent to the backend causing errors. Strangely, this issue does not occur in the development environment.

One such error example is:

NoMethodError at /entities/get_current_entity
=============================================

> undefined method `fields' for nil:NilClass

app/controllers/application_controller.rb, line 79
--------------------------------------------------

 ruby
   74   
   75     private
   76   
   77     def set_locale
   78       #I18n.locale = params[:locale] if params[:locale].present?
>  79       if current_staff.nil? or current_staff.locale.nil?
   80         I18n.locale = "en"
   81       else
   82         I18n.locale = current_staff.locale
   83       end
   84     end


App backtrace
-------------

 - app/controllers/application_controller.rb:79:in `set_locale'
<Snip>

The failing test scenario looks like this:

require "rails_helper"

RSpec.describe "Login", :type => :feature do
  describe "login with existing staff member" do
    before do 
      create_admin
    end

    it "logs in as admin" do
      visit "#/login"
      fill_in "Email", with: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7b6b3babeb997a3b2a4a3f9b4b8baf9b6a2">[email protected]</a>"
      fill_in "Password", with: "password"
      click_button "Login"

      within "#staff-status" do
        expect(page).to have_text('LOGOUT')    
      end

      within("#flash-messages") do 
        expect(page).to have_text('Successful login')
      end

      # Check session variables
      within("#selected-entity") do 
        expect(page).to have_text("LOGIN DEFAULT COMPANY")
      end

      within("#selected-ledger") do 
        expect(page).to have_text("My Ledger")
      end

      within("#selected-division") do 
        expect(page).to have_text("1-General")
      end
    </end>
  end>
end>

This problem occurs consistently throughout the application after upgrading to Rails 4 from Rails 3.2. When utilizing promises, the issue doesn't arise, but implementing promises for all data retrieval slows down the application significantly.

As a workaround for login issues, the following code snippet can be used:

Auth.currentUser().then((staff) ->
  FirmService.current_entity($rootScope).then ->
    DivisionService.current_division($rootScope).then ->
      LedgerService.current_ledger($rootScope)

To handle simultaneous requests during testing in Rails 4, you may try running the server in the test environment. However, updating gems such as capybara, selenium-webdriver, and chromedriver-helper did not resolve the issue.

If errors persist, further debugging through techniques like inserting 'binding.pry' into controllers and analyzing pry hanging instances should provide insight into the root cause of the problems.

You can access strace outputs and additional debugging information here.

Answer №1

After a brief discussion, it was discovered that a patch was being used to share the DB connection between multiple threads. This method can lead to various issues with concurrent requests and may cause tests to behave erratically. The solution was to remove the patch, which in turn resolved the testing issues.

Upon upgrading to Rails 5.1, Rails automatically manages the shared connection in the test environment using mutexes. This allows for transactional feature testing and eliminates the need for database_cleaner.

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

Inject the variable into location.href

I am dealing with a dynamic URL, and I want to achieve the following in my HTML: <a onclick="location.href='myVariable';"><span>Click here</span></a> The issue is that this approach displays the actual string 'myVar ...

Using Node modules in the browser: A beginner's guide

Once you've used npm to install packages such as: $ npm install bootstrap or $ npm install angular these packages are typically stored in the "node_modules" directory. The question now arises, how do you link to or access them from an HTML page? U ...

Utilizing Dropwizard for hosting static HTML files

I'm in the process of developing an application using dropwizard and angularjs. I have configured my AssetsBundle as follows: bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.html")); The challenge I am facing is that I want multiple page ...

Obtain the src attribute of iframes using Robot Framework Selenium and store it as a variable

On a webpage, I have an iframe element that displays an html document representing a generated form. My goal is to create an automated Selenium script to validate specific values within this document. Currently, I am manually copying the URL from the ifram ...

Code snippet for fetching JavaScript file using Angular's caching mechanism

Currently in my project, I am utilizing $.getScript to dynamically load specific sections of code. Here's a snippet of how it looks: var mainJs = "/main.js"; $.getScript( mainJs ) .then(function () { console.log("main.js loaded"); }); ...

ui-jq flot graph with lazy loading

I am working with this HTML code: <div id="test" ui-jq="plot" ui-options=" [ { data: {{line}}, points: { show: true, radius: 6}, splines: { show: true, tension: 0.45, lineWidth: 5, fill: 0 }, label: 'Akademi' }, ], { ...

The DOM is unable to locate the text attribute within a list object, resulting in an AttributeError

I attempted to locate an element within the DOM tree but encountered a problem. Here is my code: age = browser.find_elements_by_xpath("//div[@id='ads']/ul/li[6]/span[2]").text However, I keep getting the error message: AttributeError: 'li ...

What is the best way to ensure a DOM element exists before you can start manipulating it?

In my AngularJS application, I am utilizing Angular Material and md-select. When a user clicks on the md-select, a dropdown list appears and an overlay with the tag Name is added to the DOM. My goal is to add a class or style as soon as the user clicks on ...

Error: Unable to locate the module 'selenium' when using subprocess

Experiencing an issue when attempting to launch a program with a subprocess. The program runs smoothly when executed directly, without going through another file. Python 3.10 is being used on Windows 10. I have verified that the selenium module is up to d ...

Issue: unable to inject ngAnimate due to uncaught object error

Whenever I attempt to inject 'ngAnimate' into my app, I encounter issues with instantiation. Here is the code snippet in question: var app = angular.module('musicsa', [ 'ngCookies', 'ngResource', 'ngSanit ...

Discover how to efficiently locate the input box for the First Name using Selenium in Java with the new Relative Locator feature in Selenium 4

I am facing an issue where I cannot input text into the input field located to the right of the label "FirstName" while using Selenium 4 with toRightOf. WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.manage().windo ...

Combining Selenium Test Suite in Eclipse with TFS Integration

One of our developers has posed a question regarding integrating a selenium test suite with a TFS build in Visual Studio to the test server. They are wondering if it is possible for the regression test suite to be automatically triggered right after a buil ...

What are effective strategies for safeguarding my AngularJS application code, particularly from unauthorized access through the browser's source code?

I am currently working on an AngularJS application. I have encountered a challenge where the end user is able to view the app code from the browser's source code. I am seeking advice on how to address this issue effectively. Is there any recommended ...

Working with JSON data retrieved from a PHP and MySQL backend in an AngularJS application

Having difficulty handling the JSON response from PHP. I am using AngularJs to display the received JSON data. As a newcomer to Angular, I attempted a basic exercise and would appreciate some assistance. Thank you in advance. index.html <!DOCTYPE HTML ...

Passing a parameter to an AngularJS directive, which triggers the opening of an ngDialog, and subsequently updating the value to reflect changes in the root scope

Struggling with a persistent issue here; Essentially, I have a directive that triggers an ngDialog to open. This directive should be able to receive a variable from the root scope. The directive contains a click event that opens an ngDialog which then use ...

Best practice for Protractor testing: How to click a button based on text or class?

I wrote some code <div class="button-wrapper"> <button class="continue enabled">Continue</button> </div> Currently, I am working on an end-to-end test using Angular JS. I attempted to use element(by.buttonText('Continue& ...

OutdatedElementReferenceError

I've been researching the StaleElementReferenceException on the official Selenium documentation, but I'm still puzzled why this exception is being raised in my code. Does browser.get() create a new spider each time? class IndiegogoSpider(CrawlSp ...

Tips for moving and saving a file to a specific location before downloading it using Python and Selenium with the Chrome Webdriver

Recently, I started using the Selenium Chrome WebDriver. I have been navigating a webpage where I input my login details and then download a file in CSV format by clicking on a button (the file is saved directly to my downloads folder). However, I would p ...

Guide to organizing real-time event additions for a scheduler using Angular

Seeking guidance on structuring logic efficiently in Angular for rendering calendars with event objects. The Task at Hand In my current project, I need to display a visual calendar using HTML and dynamically insert event objects from a list of events int ...

An issue occurred while executing PhantomJS using Selenium's RemoteWebDriver

Launching a selenium grid hub using the default startup command: java -jar selenium-server-standalone-2.33.0.jar -role hub Also, initiating PhantomJS in its webdriver mode on the same machine with the following command: phantomjs --webdriver=8080 --web ...