Accessing a document using protractor

It seems like every protractor example I come across online uses browser.get with a web URI.

browser.get('http://localhost:8000');

I want to use Selenium to navigate to a local file:// path without needing a web server running. Just a simple HTML page and some assets should suffice.

However, this doesn't seem to be working as expected.

browser.get('file:///Users/myusername/dev/mpproject/spec/support/index.html');

When I enter that URI into my browser, the HTML page loads fine. But when I try to access it with protractor, I encounter a timeout error.

Is there a way to run tests on this page using protractor? Ideally, the solution would involve a relative file path from the myproject root directory.

Answer №1

I want to share the solution I discovered here that helped me successfully run Protractor using a file protocol.

Protractor by default uses

data:text/html,<html></html>
as the resetUrl. However, there may be restrictions when transitioning from the data: to the file: protocol, resulting in a "not allowed local resource" error. To address this, we need to replace the resetUrl with one using the file: protocol:

exports.config = {
    // ...

    baseUrl: 'file:///absolute/path/to/your/project/index.html',

    onPrepare: function() {

        // By default, Protractor uses data:text/html,<html></html> as resetUrl, but 
        // location.replace from the data: to the file: protocol is not allowed
        // (we'll get ‘not allowed local resource’ error), so we replace resetUrl with one
        // with the file: protocol (this particular one will open system's root folder)
        browser.resetUrl = 'file://';
    }

    // ...
};

If you wish to use a relative path to your project folder, Node.js tools can be helpful since Protractor operates within a Node.js environment. For instance, using __dirname will provide an absolute path to the directory where your Protractor config file is located. Consequently, you can use:

exports.config = {
    // ...

    baseUrl: 'file://' + __dirname + '/spec/support/index.html'

    // ...
};

In cases where your application makes XHR requests to endpoints that are restricted under the file: protocol, running your test browser with custom flags might be necessary. For example, in my scenario with Chrome:

exports.config = {
    // ...

    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            // --allow-file-access-from-files - allow XHR from file://
            args: ['allow-file-access-from-files']
        }
    }

    // ...
}

Answer №2

After encountering the same error, I followed Michael Radionov's fix but made a modification by removing the baseUrl. This is how I set up my configuration:

protractor.config.js:

exports.config = {

  capabilities: {
    browserName: 'chrome'
  },

  specs: [
    '*.js'
  ],

  onPrepare: function() {
    // Protractor usually uses data:text/html,<html></html> as resetUrl, but 
    // replacing it with file: protocol is necessary to prevent ‘not allowed local resource’ error
    // Replace default resetUrl with one using file: protocol (opening system's root folder)
    browser.ignoreSynchronization = true;
    browser.waitForAngular();
    browser.sleep(500); 
    browser.resetUrl = 'file:///';
  }

};

e2etest.js:

'use strict';

describe("Starting Buttons' tests", function() {

    it('Verify retrieval of 20 records into table', function() {

        browser.get('file:///C:/Users/path_to_file/index.html');

        /* Implement test codes here */

    });

});

Answer №3

Can you explain the concept of an error log?

A possible solution to issues with 'Loading' angular is setting browser.driver.ignoreSynchronization = true;

Consulting the error log can provide valuable insights into troubleshooting the problem.

Answer №4

It appears there may be a typo error in the code snippet provided. When using the "get" method, it is important to enclose the URL within double quotes "".

To rectify this issue, I recommend modifying the code as shown below:

WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/Programming%20Samples/HTML%20Samples/First%20Program.html");

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

Error encountered in Selenium with Python when trying to locate Chrome binary for outdated versions of the browser: WebDriverException - Chrome binary not found

Due to compatibility reasons, I have opted to use Chrome version 55.0.2883.75 with Chromedriver v. 2.26. To download the older version of Chrome, I visited , and for Chromedriver 2.26, I got it from https://chromedriver.storage.googleapis.com/index.html?pa ...

Struggling to dynamically append additional textboxes to a <div> element using JavaScript

After spending over 12 hours on this problem, I am completely stuck and frustrated. I have tried countless variations and sought out other solutions to no avail. It should be a simple task. My project involves using JQueryMobile 1.2 along with its dependen ...

When using Inertia.js with Laravel, a blank page is displayed on mobile devices

Recently, I've been working on a project using Laravel with React and Inertia.js. While everything runs smoothly on my computer and even when served on my network (192.168.x.x), I encountered an issue when trying to access the app on my mobile phone. ...

Updating data in a SQL database using PHP when a button is clicked

This Question was created to address a previous issue where I had multiple questions instead of focusing on one specific question Objective When the user selects three variables to access data, they should be able to click a button to modify one aspect o ...

Having trouble retrieving the image using BufferedImage in Selenium Webdriver Java when reading from a URL with ImageIO.read()

I am currently working on a project where I need to compare the page logo by downloading it first and then taking a screenshot of it. The logo I am focusing on is the LUMA logo, which I am attempting to validate using Selenium WebDriver in Java with Cucumb ...

Different option for positioning elements in CSS besides using the float

I am currently working on developing a new application that involves serializing the topbar and sidebar and surrounding them with a form tag, while displaying the sidebar and results side by side. My initial attempt involved using flex layout, but I have ...

Tips for preventing tiny separation lines from appearing above and below unordered list elements

I am attempting to utilize twitter bootstrap to create a select-option style list. How can I eliminate the thin separation lines above and below the list of items? Refer to the screenshot: Below is the visible code snippet. It would be best to view the j ...

active option in Opencart 2.1 is set to "selected"

I've implemented the AJAX module d_quickcheckout to speed up the checkout process on opencart 2.1 (not the default one). However, I'm encountering an issue with a field in the payment address section that is always pre-selected with the region/st ...

How do you incorporate ScrollTop animations using @angular/animations?

I'm attempting to recreate the animation showcased on Material.io: https://i.stack.imgur.com/OUTdL.gif It's relatively easy to animate just the height when clicking on the first card in the example above. The challenge arises when you click on ...

GTM - monitoring the most recent clicked element's input data

Custom Script function() { var inputs = document.getElementsByTagName("input"), selectedRadios = []; for (var i = 0;i < inputs.length;i++) { if(inputs[i].type==="checkbox" && inputs[i].checked) { selectedRadios.push(inputs[i].value); ...

Having trouble with running Selenium tests

I've recently embarked on my Selenium journey, starting off by learning it through freecodecamp and acclimating myself with the tools. I downloaded Selenium using this command in Python: pip install selenium In addition to that, I manually installed ...

Discovering webElements on Facebook using Selenium with Java

I'm currently working on a test script to post on my Facebook wall, but I have encountered an issue - I am unable to locate the element (getting an NoSuchElementException) even though I know it exists. I tried using FirePath to find a cssSelector, but ...

Incorporating dynamic images into Laravel using Angular

I am currently trying to dynamically input the src value of an image using Angular. Below is the code I have written : HTML : <div ng-app="myApp" ng-controller="MyCtrl"> <img src="{{asset('assets/img/'.'/'. @{{ item.name ...

apply a visible border to the item that is clicked or selected by utilizing css and vue

I have a list of items that I want to display as image cards with an active blue border when clicked. Only one item can be selected at a time. Below is the code snippet: Template Code <div class="container"> <div v-for="(obj ...

What are the steps to enable ajax communication with a database for posting, retrieving, and removing data?

Is there a way to utilize ajax for posting, deleting, and getting data from a database? I am looking to post content and then be able to delete it through the following link: (this is part of an assignment) However, I must use /ajax/addrecord.php and /a ...

After a single click, the functionality of jquery.nav.js seems to be malfunctioning

Encountering an error message: Uncaught TypeError: Cannot read property 'top' of undefined(…) jquery.nav.js:183 In an effort to convert my web app into a Single Page Application (SPA) using the jquery.nav.js library (available at https://githu ...

Save a complete webpage with all HTML assets directly without pesky save as pop up using Python and Selenium

I'm in the process of scraping a website and saving all the webpages as .html files (along with all HTML assets) to be able to open them locally just like they appear on the server. At the moment, I am using Selenium, Chrome Webdriver, and Python. M ...

What could be causing the template UI to not display the Vue data?

As someone new to Vue, I have defined my Vue code in the following way: import Vue from "vue"; export default Vue.extend({ data: () => { message: "show message"; }, }); <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js ...

The transition from using Selenium to sending requests

I am currently exploring the requests module as an alternative to Selenium for web scraping. Below is the code snippet I have been working on that extracts a table from a webpage. I'm struggling to optimize this code using requests in a more efficie ...

Selenium Web Driver: Confirming extracted data from various modules

Here's an intriguing question I have. On page A within module 1, there is a drop-down field labeled 'Service Provider'. Along with this field, the address details (Address, Location, Zip, Country, etc.) are also present on page A. Each ser ...