Using Selenium Webdrivers to Browse Pages with Minimal Resource Loading

I'm attempting to restrict Javascript from altering the source code of the site while testing with Selenium. Disabling Javascript completely in the Webdriver is not an option as I require it for testing purposes. Below is my approach for the Firefox Webdriver:

        firefoxProfile.setPreference("permissions.default.image", 2);
        firefoxProfile.setPreference("permissions.default.script", 2);
        firefoxProfile.setPreference("permissions.default.stylesheet", 2);
        firefoxProfile.setPreference("permissions.default.subdocument", 2);

I have disabled Images, Scripts, and Stylesheets in Firefox. Is there a way to achieve this with the Internet Explorer Webdriver and Chrome Webdriver as well? I haven't come across similar preferences for these browsers. Alternatively, is there a more elegant method to prevent the webdrivers from loading JS Files on the site? Thank you!

Answer №1

To resolve this issue, the recommended solution is to utilize a proxy. A great option for integrating WebDriver with a proxy is browsermob proxy, which can be found at:

public WebDriver initializeBrowser() throws Exception {
    ProxyServer server = new ProxyServer(proxy_port); // Import from net.lightbody.bmp.proxy

    server.start();
    server.setCaptureHeaders(true);
    
    // Blacklisting google analytics requests
    server.blacklistRequests("https?://.*\\.google-analytics\\.com/.*", 410);

    // Whitelisting necessary requests
    server.whitelistRequests("https?://*.*.yoursite.com/.*, https://*.*.someOtherYourSite.*".split(","), 200);

    Proxy proxy = server.seleniumProxy(); // Proxy package from org.openqa.selenium.Proxy

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, proxy);

    Webdriver driver = new FirefoxDriver(capabilities); // Initialization of Firefox driver
    //WebDriver driver = new InternetExplorerDriver(); // Alternative initialization for Internet Explorer

    return driver;
}

Answer №2

To achieve your desired result in a consistent manner across all browsers, consider utilizing a proxy. With this approach, you can intercept resource requests and prevent them from loading. This method offers the benefit of using a single code base for all browsers, rather than having to create specific settings for each individual browser.

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

Remove user from firebase with Admin SDK

I need help understanding how to remove a user from my admin panel using the Firebase Admin SDK. When attempting to delete a user, I encountered this error: Uncaught (in promise) ReferenceError: uid is not defined at eval (ManageCustomer. ...

Convert an array into individual objects and include key-value pairs from a separate object

I am working with two arrays that have the same length: One is a simple array numbers = [4,5,6] The other is an array of objects objects = [ {type: "x", value: 7}, {type: "y", value: 8}, {type: "z", value: 9} ] My goal is to combine th ...

Steps for wrapping a class with a higher order component

Is it feasible to encapsulate a class component within a higher order component (HOC) that is also a class? import React, { Component } from "react"; import { View } from "react-native"; import { Toast } from "react-native-easy-toast"; const withToast = ...

Strengthening the core: Preserving a full set of data on a server that doesn't

After going through various discussions on how to save a Backbone collection using a non-RESTful server, I am still a bit puzzled. I have set up a collection where I've customized the data for posting to my API ("/api/entity/735/request/personDelete" ...

Error encountered while unmarshalling JSON with a root element in Jersey client

As I establish a connection to a remote server that is out of my control, I encounter an issue with the JSON response containing a root element. {"company":{"name":"Personal"}} Upon attempting to convert the string into a company object, an error occurs: ...

The current version of ChromeDriver is only compatible with Chrome version 99. However, the current browser version is at 98.0.4758

My current chrome version : 98.0.4758.102 I am currently using selenium version: 4.1.1, but I am unable to download chrome 99. My chrome 98 is up to date, so how can I obtain chrome 99 assuming it's a beta version? Trace log : ChromeDriver was start ...

A helpful guide on passing a component as a prop to a child class and incorporating it within the child component along with additional props

Within my parent class, I have loaded a child class component and passed another component to it as a prop. In the childclass component, I am trying to display that component. <EditRecordModal show={this.state.showEditModal} onHide={this.handle ...

In Typescript, we can streamline this code by assigning a default value of `true` to `this.active` if `data.active

I am curious if there is a better way to write the statement mentioned in the title. Could it be improved with this.active = data.active || true? ...

Remove model associated with tab on close in the PrimeFaces TabView

I am currently utilizing the Primefaces (version 3.0.1) p:tabView component, which is capable of displaying a dynamic number of tabs supported by a list in a model. These tabs are closable, and I aim to remove the corresponding list element when a tab is c ...

What is the manual way to initiate the onclicksubmit event in jqgrid?

Is there a way to manually trigger the onclicksubmit event in jqgrid? If a certain condition is met, I need to programmatically call the submit event. See the code snippet below for reference. afterShowForm: function (formid) { if (condition) { ...

Tips for fixing an error encountered when running a react native project for the first time

I am encountering some errors while attempting to run this project for the first time and I am unable to resolve them. Below is the content of the package.json file: { "scripts": { "start": "expo start", "andro ...

Generate pre-set components using fundamental building blocks

Can I predefine some props for components? In my Vuetify example, let's say I want to customize the v-btn component with specific props. This custom implementation would act as a child component while still providing all the functionalities of the par ...

How to use the sha512 hash function in Node.js for Angular2 and Ionic2 applications

I'm attempting to generate a SHA512 Hash in Angular2 (Ionic2) that matches the PHP function hash('sha512'). After trying out different modules like crypto-js, crypto, and js-sha512, I keep getting a different Hash compared to PHP. I even a ...

Problem encountered when Next.js and CSRF token interact on the server

I've integrated the next-csrf library (https://github.com/j0lv3r4/next-csrf) into my next.js app to safeguard api routes. Despite following the documentation, I'm encountering a 500 error with the following message: {"message":"Si ...

Creating a Selenium test class in Java to streamline the setup process for multiple tests

I recently came across this informative post that shed some light on my current situation. I have been following it as a guide but encountered a java.lang.NullPointerException, which I believe is due to the page not loading properly, although I'm unsu ...

AngularJS - Setting an initial delay for ng-bind

We have a span element with the following attributes: <span role="link" ng-show="showLink()" ng-bind="textLink"></span> (Just an fyi: we implemented a fade-in, fade-out animation for this link, hence the use of ng-show instead of ng-if) The ...

Showing a series of JavaScript countdowns consecutively

I am working on a project where I want to display a second countdown after the first one finishes using meteor. The initial timer code looks like this: sec = 5 @timer = setInterval((-> $('#timer').text sec-- if sec == -1 $('#time ...

Displaying a component following data retrieval in Vue.js

I am currently working on consuming an API using axios. Here is the code I have so far: <select> <option v-for="value in values"> value.name </option> </select> // js data(){ values: [], }, create ...

Help with enabling the recognition of backspace key in JavaScript?

My current JavaScript is almost perfect but missing one key element. The form has two fields that are disabled when the user fills it out. <label>Can you drive? </label> <input type="booleam" id="drive" disabled><br> <label>W ...

Creating new components within A-frame

I've been attempting to implement the bubble function into my scene to generate a sphere, but unfortunately nothing is showing up. Even when I try creating a sphere without using the bubble function, it still doesn't appear in the scene. func ...