Turn off JavaScript during the execution of Cucumber tests

When running integration tests using Capybara and Cucumber, I encountered a scenario where JavaScript needed to be disabled. While I can manually disable JS in Chrome developer tools, I am looking for a way to automate this process. Is there an option to start a browser up with JS disabled or enable/disable it during the test?

Capybara.register_driver :chrome do |app|
  chrome_binary = ENV["HENDRICKS_CHROME_BINARY"]

  if chrome_binary.nil?
    Capybara::Selenium::Driver.new(app, :browser => :chrome)
  else
    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
      "chromeOptions" => {
        "binary" => chrome_binary + "/Contents/MacOS/Chromium"
      }
    )
    Capybara::Selenium::Driver.new(app, :browser => :chrome, :desired_capabilities => capabilities)
  end
end

Is there a way to achieve this with Chrome as my browser?

Answer №1

I have implemented the following method to disable JavaScript:

Capybara.register_driver :js_disabled do |app|
  chrome_binary = ENV["HENDRICKS_CHROME_BINARY"]

  if chrome_binary.nil?
    Capybara::Selenium::Driver.new(app, :browser => :chrome)
  else
    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
      "chromeOptions" => {
        "prefs" => {
          'profile.managed_default_content_settings.javascript' => 2
        },
        "binary" => chrome_binary + "/Contents/MacOS/Chromium"
       }
    )
    Capybara::Selenium::Driver.new(app, :browser => :chrome, :desired_capabilities => capabilities)
  end
end

Pay attention to the prefs object being passed via the chromeOptions.

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

Caution: Reactjs detectable issue with unreached code

Have you encountered the warning about unreachable code (no-unreachable) in my ReactJS app? Can anyone help me with this issue? The error seems to be related to an if-else return statement. The problem lies within this section: If there is no Route path ...

Sending a JavaScript array as a parameter to a C# function

In my current project, I am working with an array in JavaScript that is created based on checked items. After this integer array is generated, I need to figure out how to pass it to C#. My initial thought was to use a hidden text box and store the intege ...

Tracking Conversions on Google AdWords: Monitoring Two Separate OnClick Conversions on a Single Page

I need to monitor 2 separate onclick conversions on a single page. The tracking code provided by Google is structured like this: <script type="text/javascript"> /* <![CDATA[ */ goog_snippet_vars = function() { var w = window; ...

Exclude certain C++ components

When using emscripten, I've noticed that even small C++ files can result in large JavaScript files. For example: #include <memory> int main(int argc, char** argv) { std::shared_ptr<int> sp(new int); } Compiling this with a recent emsdk ...

Oops! Looks like there's a type error in module "*.mdx" - it seems that it doesn't have the exported member "metadata". Maybe try using "import metadata from "*.mdx"" instead?

I am attempting to extract metadata from an mdx file. I have followed the guidelines outlined in NextJS Markdown Frontmatter, but encountered build errors. It is important to note that I am unable to utilize fs. Code Section Page.tsx File import Conte ...

Creating an array to store multiple ID values within a variable

const idArray = $scope.rep.Selected.id; I am working with this piece of code. I am wondering, if I have multiple ids in the $scope...Selected.id and then execute this (code), will all these ids be placed in separate arrays or combined into one array? ...

react validation for dropdown, react-datepicker, and hour input at intermittent intervals

I have integrated the following packages into my project :- react-datepicker library for selecting from time and to time date validation with date-fns, moment.js, additional validations using jQuery and lodash libraries/packages If you want to view my pr ...

Tips for implementing ajax and codeigniter to load additional comments on a web page

Is it possible to customize Codeigniter's default pagination to achieve a "viewMore" link style when loading more records using AJAX? The challenge lies in creating a div that automatically expands to handle large numbers of records, such as 10,000 a ...

Dynamic web page enlargement/magnification

Take a look at www.mediawiki.org using Chrome and experiment with adjusting the page zoom (Ctrl + +/-). Any tips on how to incorporate animated zoom handling like this? I've found that it doesn't function in every browser. Appreciate any insigh ...

User input determines the path of Iron Route in Meteor

A requirement is to execute a function that prompts the user for input and then navigates to that specified value. For instance, if the inserted value is: https://www.youtube.com/watch?v=_ZiN_NqT-Us The intended destination URL should be: download?u ...

Display a pop-up when the notification is clicked

Can a desktop notification trigger an extension popup to open upon click? Check out the code snippet provided below: var notification = window.webkitNotifications.createNotification(); notification.addEventListener('click', function() { not ...

Deleting a segment of content from a webpage

Currently, I'm in the final stages of completing a blackjack game. However, one aspect that I haven't implemented yet is the ability for the user to play again after finishing a round. My initial idea is to use a window.confirm dialog box, where ...

What is causing the image to not be centered in the canvas when using AngularJS?

Can you please help me troubleshoot why my image is not centered using the method below? function drawImage() { clear(); element.save(); element.scale(currentScale, currentScale); ...

Discovering the method for accessing a variable within jQuery from a regular JavaScript function

As someone new to jQuery, I am currently facing a challenge with accessing a variable defined inside a jQuery block from a regular function. Despite my attempts, I have been unsuccessful in accessing it. Can anyone guide me on how to do this? <script l ...

Encountering a "missing credentials" error when utilizing local passport in NodeJs

Having trouble implementing a password reset functionality for users. Despite troubleshooting with the code provided, the password remains the same without any errors being displayed. The User model file looks like this:- const mongoose = require('m ...

The implementation of jQuery show/hide functionality for a div based on select change is not functioning properly when the

Currently, I am streamlining an HTML web form by using scripts to populate select box options. While the show/hide functionality works perfectly for one select with hardcoded options, it fails for a dropdown filled via script and JSON data. Can anyone pinp ...

Testing with Saucelabs is hindered by the browser's popup dialog

While testing my web app on SauceLabs, I encountered an issue with IndexedDB. Previously, my tests ran smoothly but now they are getting blocked by a browser dialog message that states "" wants to: store files on this device", along with an Allow button. ...

Navigating from child to parent page in Next.js: how can it be done?

Forgive me if this question seems simplistic, I am new to the world of Next.js and React.js. From my understanding, in Next.js, we can organize pages hierarchically, with each xx_page.js file's path serving as its corresponding url route, as noted in ...

Assign a custom value to the ng-options and ensure that the first option is selected by default

Hey there, I'm currently working on the following: $scope.test = [ {"value" : 0, "text" : "00:00"}, {"value" : 900, "text" : "00:15"}, {"value" : 1800, "text" : "00:30"} ]; and in my select element, this is what I hav ...

Searching for location data within a string using JavaScript regex techniques

"I currently reside in Salt Lake City, Utah. I'm in a relationship and originally from Ho Chi Minh City, Vietnam." What I need is just Salt Lake City, Utah at near the beginning. I'm new to coding so any help with JavaScript regex would be grea ...